Skip to content

Commit 309f8dd

Browse files
committed
Address PR review
1 parent 6fed5e3 commit 309f8dd

File tree

5 files changed

+20
-15
lines changed

5 files changed

+20
-15
lines changed

docs/src/client.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ If `true`, `HTTP.StatusError`, `HTTP.TimeoutError`, `HTTP.IOError`, and `HTTP.Co
109109

110110
### `observelayers`
111111

112-
If `true`, enables the `HTTP.observe_layer` to wrap each client-side "layer" to track the amount of time spent in each layer as a request is processed. This can be useful for debugging performance issues. Note that when retries or redirects happen, the time spent in each layer is cumulative, as noted by the `[layer]_count`. The metrics are stored in the `Request.context` dictionary, and can be accessed like `HTTP.get(...).request.context`.
112+
If `true`, enables the `HTTP.observelayer` to wrap each client-side "layer" to track the amount of time spent in each layer as a request is processed. This can be useful for debugging performance issues. Note that when retries or redirects happen, the time spent in each layer is cumulative, as noted by the `[layer]_count`. The metrics are stored in the `Request.context` dictionary, and can be accessed like `HTTP.get(...).request.context`.
113113

114114
### `basicauth`
115115

src/Connections.jl

+12-7
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ const nolimit = typemax(Int)
3030

3131
taskid(t=current_task()) = string(hash(t) & 0xffff, base=16, pad=4)
3232

33-
const default_connection_limit = Ref(16)
33+
const default_connection_limit = Ref{Int}()
3434

3535
function __init__()
36-
default_connection_limit[] = Threads.nthreads() * 4
36+
# default connection limit is 4x the number of threads
37+
# this was chosen after some empircal benchmarking on aws/azure machines
38+
# where, for non-data-intensive workloads, having at least 4x ensured
39+
# there was no artificial restriction on overall throughput
40+
default_connection_limit[] = max(16, Threads.nthreads() * 4)
3741
nosslcontext[] = OpenSSL.SSLContext(OpenSSL.TLSClientMethod())
3842
TCP_POOL[] = CPool{Sockets.TCPSocket}(default_connection_limit[])
3943
MBEDTLS_POOL[] = CPool{MbedTLS.SSLContext}(default_connection_limit[])
@@ -53,6 +57,7 @@ Fields:
5357
- `port::String`, exactly as specified in the URI (i.e. may be empty).
5458
- `idle_timeout`, No. of seconds to maintain connection after last request/response.
5559
- `require_ssl_verification`, whether ssl verification is required for an ssl connection
60+
- `keepalive`, whether the tcp socket should have keepalive enabled
5661
- `peerip`, remote IP adress (used for debug/log messages).
5762
- `peerport`, remote TCP port number (used for debug/log messages).
5863
- `localport`, local TCP port number (used for debug messages).
@@ -89,8 +94,8 @@ end
8994
9095
Used for "hashing" a Connection object on just the key properties necessary for determining
9196
connection re-useability. That is, when a new request calls `newconnection`, we take the
92-
request parameters of what socket type, the host and port, and if ssl
93-
verification is required, and if an existing Connection was already created with the exact
97+
request parameters of host and port, and if ssl verification is required, if keepalive is enabled,
98+
and if an existing Connection was already created with the exact
9499
same parameters, we can re-use it (as long as it's not already being used, obviously).
95100
"""
96101
connectionkey(x::Connection) = (x.host, x.port, x.require_ssl_verification, x.keepalive, x.clientconnection)
@@ -346,7 +351,7 @@ and defaults to the `HTTP.default_connection_limit` value.
346351
347352
A pool can be passed to any of the `HTTP.request` methods via the `pool` keyword argument.
348353
"""
349-
mutable struct Pool
354+
struct Pool
350355
lock::ReentrantLock
351356
tcp::CPool{Sockets.TCPSocket}
352357
mbedtls::CPool{MbedTLS.SSLContext}
@@ -413,7 +418,7 @@ function closeall(pool::Union{Nothing, Pool}=nothing)
413418
end
414419

415420
function connection_isvalid(c, idle_timeout)
416-
check = isopen(c) && (time() - c.timestamp) <= idle_timeout
421+
check = isopen(c) && inactiveseconds(c) <= idle_timeout
417422
check || close(c)
418423
return check
419424
end
@@ -628,7 +633,7 @@ function sslupgrade(::Type{IOType}, c::Connection{T},
628633
# success, now we turn it into a new Connection
629634
conn = Connection(host, "", 0, require_ssl_verification, keepalive, tls)
630635
# release the "old" one, but don't return the connection since we're hijacking the socket
631-
release(getpool(pool, T), connectionkey(c), nothing)
636+
release(getpool(pool, T), connectionkey(c))
632637
# and return the new one
633638
return acquire(() -> conn, getpool(pool, IOType), connectionkey(conn); forcenew=true)
634639
end

src/HTTP.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ getrequest(r::Request) = r
4949
getrequest(s::Stream) = s.message.request
5050

5151
# Wraps client-side "layer" to track the amount of time spent in it as a request is processed.
52-
function observe_layer(f)
53-
function observelayer(req_or_stream; kw...)
52+
function observelayer(f)
53+
function observation(req_or_stream; kw...)
5454
req = getrequest(req_or_stream)
5555
nm = nameof(f)
5656
cntnm = Symbol(nm, "_count")
@@ -158,7 +158,7 @@ Supported optional keyword arguments:
158158
- `logerrors = false`, if `true`, `HTTP.StatusError`, `HTTP.TimeoutError`, `HTTP.IOError`, and `HTTP.ConnectError` will be
159159
logged via `@error` as they happen, regardless of whether the request is then retried or not. Useful for debugging or
160160
monitoring requests where there's worry of certain errors happening but ignored because of retries.
161-
- `observelayers = false`, if `true`, enables the `HTTP.observe_layer` to wrap each client-side "layer" to track the amount of
161+
- `observelayers = false`, if `true`, enables the `HTTP.observelayer` to wrap each client-side "layer" to track the amount of
162162
time spent in each layer as a request is processed. This can be useful for debugging performance issues. Note that when retries
163163
or redirects happen, the time spent in each layer is cumulative, as noted by the `[layer]_count`. The metrics are stored
164164
in the `Request.context` dictionary, and can be accessed like `HTTP.get(...).request.context`
@@ -420,7 +420,7 @@ function stack(
420420
requestlayers=(),
421421
streamlayers=())
422422

423-
obs = observelayers ? observe_layer : identity
423+
obs = observelayers ? observelayer : identity
424424
# stream layers
425425
if streamlayers isa NamedTuple
426426
inner_stream_layers = haskey(streamlayers, :last) ? streamlayers.last : ()
@@ -602,7 +602,7 @@ write(socket, frame)
602602
"""
603603
function openraw(method::Union{String,Symbol}, url, headers=Header[]; kw...)::Tuple{IO, Response}
604604
socketready = Channel{Tuple{IO, Response}}(0)
605-
@async HTTP.open(method, url, headers; kw...) do http
605+
Threads.@spawn HTTP.open(method, url, headers; kw...) do http
606606
HTTP.startread(http)
607607
socket = http.stream
608608
put!(socketready, (socket, http.message))

src/Servers.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ Whatever you type on the client will be displayed on the server and vis-versa.
282282
using HTTP
283283
284284
function chat(io::HTTP.Stream)
285-
@async while !eof(io)
285+
Threads.@spawn while !eof(io)
286286
write(stdout, readavailable(io), "\\n")
287287
end
288288
while isopen(io)

src/clientlayers/MessageRequest.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Construct a [`Request`](@ref) object from method, url, headers, and body.
2121
Hard-coded as the first layer in the request pipeline.
2222
"""
2323
function messagelayer(handler)
24-
return function(method::String, url::URI, headers, body; copyheaders::Bool=true, response_stream=nothing, http_version=HTTPVersion(1, 1), verbose=DEBUG_LEVEL[], kw...)
24+
return function makerequest(method::String, url::URI, headers, body; copyheaders::Bool=true, response_stream=nothing, http_version=HTTPVersion(1, 1), verbose=DEBUG_LEVEL[], kw...)
2525
req = Request(method, resource(url), mkreqheaders(headers, copyheaders), body; url=url, version=http_version, responsebody=response_stream)
2626
local resp
2727
start_time = time()

0 commit comments

Comments
 (0)