From 12cb6864e359a65f3edbac1852f11af9861d4b55 Mon Sep 17 00:00:00 2001 From: tan Date: Wed, 20 Jul 2022 14:35:09 +0530 Subject: [PATCH] fix max limit for connect timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/JuliaLang/Downloads.jl/issues/193 and https://github.com/JuliaComputing/gRPCClient.jl/pull/23. --- src/curl.jl | 11 +++-------- src/grpc.jl | 7 ++++--- test/test_grpcerrors.jl | 10 +++++++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/curl.jl b/src/curl.jl index 371f1a9..06b92a5 100644 --- a/src/curl.jl +++ b/src/curl.jl @@ -215,15 +215,10 @@ function recv_data(easy::Curl.Easy, output::Channel{T}, max_recv_message_length: end function set_connect_timeout(easy::Curl.Easy, timeout::Real) - timeout >= 0 || + (0 ≤ timeout ≤ (typemax(Clong) ÷ 1000)) || throw(ArgumentError("timeout must be positive, got $timeout")) - if timeout ≤ typemax(Clong) ÷ 1000 - timeout_ms = round(Clong, timeout * 1000) - Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, timeout_ms) - else - timeout = timeout ≤ typemax(Clong) ? round(Clong, timeout) : Clong(0) - Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT, timeout) - end + timeout_ms = round(Clong, timeout * 1000) + Curl.setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, timeout_ms) end # Prevent reuse of this handle diff --git a/src/grpc.jl b/src/grpc.jl index 7d940fe..ba28a02 100644 --- a/src/grpc.jl +++ b/src/grpc.jl @@ -90,8 +90,8 @@ Contains settings to control the behavior of gRPC requests. tls), or `:http2` (http2 upgrade) - `revocation`: whether to check for certificate recovation (default is true) - `request_timeout`: request timeout (seconds) -- `connect_timeout`: connect timeout (seconds) (default is 300 seconds, same - as setting this to 0) +- `connect_timeout`: connect timeout (seconds) (must be ≤ typemax(Clong)÷1000, + default is 300 seconds, same as setting this to 0) - `max_message_length`: maximum message length (default is 4MB) - `max_recv_message_length`: maximum message length to receive (default is `max_message_length`, same as setting this to 0) @@ -126,7 +126,8 @@ struct gRPCController <: ProtoRpcController enable_shared_locks::Bool = false, verbose::Bool = false ) - if maxage < 0 || keepalive < 0 || request_timeout < 0 || connect_timeout < 0 || + if maxage < 0 || keepalive < 0 || request_timeout < 0 || + connect_timeout < 0 || connect_timeout > (typemax(Clong) ÷ 1000) || max_message_length < 0 || max_recv_message_length < 0 || max_send_message_length < 0 throw(ArgumentError("Invalid gRPCController parameter")) end diff --git a/test/test_grpcerrors.jl b/test/test_grpcerrors.jl index 23ca88b..f8899a3 100644 --- a/test/test_grpcerrors.jl +++ b/test/test_grpcerrors.jl @@ -144,9 +144,13 @@ end function test_connect_timeout() timeout_server_endpoint = "http://10.255.255.1/" # a non routable IP - timeout_secs = 5 - client = GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=timeout_secs) + @testset "connect timeout" begin + @test_throws ArgumentError GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=typemax(Clong)) + @test_throws ArgumentError GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=-1) + + timeout_secs = 5 + client = GRPCErrorsBlockingClient(timeout_server_endpoint; verbose=false, connect_timeout=timeout_secs) data = GrpcerrorsClients.Data(; mode=1, param=0) t1 = time() try @@ -166,4 +170,4 @@ end function test_clients(server_endpoint::String) @info("testing blocking client") test_blocking_client(server_endpoint) -end \ No newline at end of file +end