From ef90fbae9f06aa6e574c7c9b91b8793136017d33 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Wed, 8 May 2024 18:48:10 -0600 Subject: [PATCH] Add global refs to use for default keyword args for connect_timeout and a new closeimmediately keyword arg that controls whether a connection should be immediately closed after a client request is finished. This is an internal-only kw for now as I test out some precompile things --- src/Connections.jl | 4 +++- src/clientlayers/ConnectionRequest.jl | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Connections.jl b/src/Connections.jl index dd716340..9537059b 100644 --- a/src/Connections.jl +++ b/src/Connections.jl @@ -433,6 +433,8 @@ end @noinline connection_limit_warning(cl) = cl === nothing || @warn "connection_limit no longer supported as a keyword argument; use `HTTP.set_default_connection_limit!($cl)` before any requests are made or construct a shared pool via `POOL = HTTP.Pool($cl)` and pass to each request like `pool=POOL` instead." +const DEFAULT_CONNECT_TIMEOUT = Ref{Int}(30) + """ newconnection(type, host, port) -> Connection @@ -446,7 +448,7 @@ function newconnection(::Type{T}, connection_limit=nothing, forcenew::Bool=false, idle_timeout=typemax(Int), - connect_timeout::Int=30, + connect_timeout::Int=DEFAULT_CONNECT_TIMEOUT[], require_ssl_verification::Bool=NetworkOptions.verify_host(host, "SSL"), keepalive::Bool=true, kw...) where {T <: IO} diff --git a/src/clientlayers/ConnectionRequest.jl b/src/clientlayers/ConnectionRequest.jl index 23881bf9..e63c1134 100644 --- a/src/clientlayers/ConnectionRequest.jl +++ b/src/clientlayers/ConnectionRequest.jl @@ -46,6 +46,8 @@ end export connectionlayer +const CLOSE_IMMEDIATELY = Ref{Bool}(false) + """ connectionlayer(handler) -> handler @@ -55,7 +57,7 @@ Close the connection if the request throws an exception. Otherwise leave it open so that it can be reused. """ function connectionlayer(handler) - return function connections(req; proxy=getproxy(req.url.scheme, req.url.host), socket_type::Type=TCPSocket, socket_type_tls::Union{Nothing, Type}=nothing, readtimeout::Int=0, connect_timeout::Int=30, logerrors::Bool=false, logtag=nothing, kw...) + return function connections(req; proxy=getproxy(req.url.scheme, req.url.host), socket_type::Type=TCPSocket, socket_type_tls::Union{Nothing, Type}=nothing, readtimeout::Int=0, connect_timeout::Int=30, logerrors::Bool=false, logtag=nothing, closeimmediately::Bool=CLOSE_IMMEDIATELY[], kw...) local io, stream if proxy !== nothing target_url = req.url @@ -88,7 +90,7 @@ function connectionlayer(handler) req.context[:connect_duration_ms] = get(req.context, :connect_duration_ms, 0.0) + (time() - start_time) * 1000 end - shouldreuse = !(target_url.scheme in ("ws", "wss")) + shouldreuse = !(target_url.scheme in ("ws", "wss")) && !closeimmediately try if proxy !== nothing && target_url.scheme in ("https", "wss", "ws") shouldreuse = false