Skip to content

Commit eeb28a3

Browse files
authored
fix max limit for connect timeout (#31)
This adds an upper limit to the connect timeout value that can be specified, to limit it to `typemax(Clong) ÷ 1000`. An `ArgumentError` is thrown if the value is out of range. This matches what libcurl expects [here](https://github.com/curl/curl/blob/4a8f6869db3a4ba7cd1bcb153c3d5b4298728afa/lib/setopt.c#L1376). Originally reported at JuliaLang/Downloads.jl#193 and #23.
1 parent 904493e commit eeb28a3

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/curl.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,10 @@ function recv_data(easy::Curl.Easy, output::Channel{T}, max_recv_message_length:
215215
end
216216

217217
function set_connect_timeout(easy::Curl.Easy, timeout::Real)
218-
timeout >= 0 ||
218+
(0 timeout (typemax(Clong) ÷ 1000)) ||
219219
throw(ArgumentError("timeout must be positive, got $timeout"))
220-
if timeout typemax(Clong) ÷ 1000
221-
timeout_ms = round(Clong, timeout * 1000)
222-
Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, timeout_ms)
223-
else
224-
timeout = timeout  typemax(Clong) ? round(Clong, timeout) : Clong(0)
225-
Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT, timeout)
226-
end
220+
timeout_ms = round(Clong, timeout * 1000)
221+
Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, timeout_ms)
227222
end
228223

229224
# Prevent reuse of this handle

src/grpc.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ Contains settings to control the behavior of gRPC requests.
9090
tls), or `:http2` (http2 upgrade)
9191
- `revocation`: whether to check for certificate recovation (default is true)
9292
- `request_timeout`: request timeout (seconds)
93-
- `connect_timeout`: connect timeout (seconds) (default is 300 seconds, same
94-
as setting this to 0)
93+
- `connect_timeout`: connect timeout (seconds) (must be ≤ typemax(Clong)÷1000,
94+
default is 300 seconds, same as setting this to 0)
9595
- `max_message_length`: maximum message length (default is 4MB)
9696
- `max_recv_message_length`: maximum message length to receive (default is
9797
`max_message_length`, same as setting this to 0)
@@ -126,7 +126,8 @@ struct gRPCController <: ProtoRpcController
126126
enable_shared_locks::Bool = false,
127127
verbose::Bool = false
128128
)
129-
if maxage < 0 || keepalive < 0 || request_timeout < 0 || connect_timeout < 0 ||
129+
if maxage < 0 || keepalive < 0 || request_timeout < 0 ||
130+
connect_timeout < 0 || connect_timeout > (typemax(Clong) ÷ 1000) ||
130131
max_message_length < 0 || max_recv_message_length < 0 || max_send_message_length < 0
131132
throw(ArgumentError("Invalid gRPCController parameter"))
132133
end

test/test_grpcerrors.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,13 @@ end
144144

145145
function test_connect_timeout()
146146
timeout_server_endpoint = "http://10.255.255.1/" # a non routable IP
147-
timeout_secs = 5
148-
client = GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=timeout_secs)
147+
149148
@testset "connect timeout" begin
149+
@test_throws ArgumentError GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=typemax(Clong))
150+
@test_throws ArgumentError GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=-1)
151+
152+
timeout_secs = 5
153+
client = GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=timeout_secs)
150154
data = GrpcerrorsClients.Data(; mode=1, param=0)
151155
t1 = time()
152156
try
@@ -166,4 +170,4 @@ end
166170
function test_clients(server_endpoint::String)
167171
@info("testing blocking client")
168172
test_blocking_client(server_endpoint)
169-
end
173+
end

0 commit comments

Comments
 (0)