Skip to content

Commit

Permalink
Add global refs to use for default keyword args for connect_timeout a…
Browse files Browse the repository at this point in the history
…nd 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
  • Loading branch information
quinnj committed May 9, 2024
1 parent a420907 commit ef90fba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down
6 changes: 4 additions & 2 deletions src/clientlayers/ConnectionRequest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ end

export connectionlayer

const CLOSE_IMMEDIATELY = Ref{Bool}(false)

"""
connectionlayer(handler) -> handler
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ef90fba

Please sign in to comment.