Skip to content

Commit

Permalink
Summary of changes in this PR:
Browse files Browse the repository at this point in the history
  * Added a new global `set_connection_limit!` function for controlling the global connection limit that will be applied to all requests
    This is one way to resolve #1033. I added a deprecation warning when passing `connect_limit` to individual requests. So usage would be:
    calling `HTTP.set_connection_limit!` and any time this is called, it changes the global value.
  * Add a try-finally in keepalive! around our global IO lock usage just for good house-keeping
  * Refactored `try_with_timeout` to use a `Channel` instead of the non-threaded `@async`; it's much simpler, seems cleaner,
    and allows us to avoid the usage of `@async` when not needed. Note that I included a change in StreamRequest.jl however that wraps
    all the actual write/read IO operations in a `fetch(@async dostuff())` because this will currently prevent code in this task from
    migrating across threads, which is important for OpenSSL usage where error handling is done per-thread. I don't love the solution,
    but it seems ok for now.
  * I refactored a few of the stream IO functions so that we always know the number of bytes downloaded, whether in memory or written to
    an IO, so we can log them and use them in verbose logging to give bit-rate calculations
  * Ok, the big one: I rewrote the internal implementation of ConnectionPool.ConnectionPools.Pod `acquire`/`release` functions; under really
    heavy workloads, there was a ton of contention on the Pod lock. I also observed at least one "hang" where GDB backtraces seemed to indicate
    that somehow a task failed/died/hung while trying to make a new connection _while holding the Pod lock_, which then meant that no other
    requests could ever make progress. The new implementation includes a lock-free "fastpath" where an existing connection that can be re-used
    doesn't require any lock-taking. It uses a lock-free concurrent Stack implementation copied from JuliaConcurrent/ConcurrentCollections.jl (
    doesn't seem actively maintained and it's not much code, so just copied). The rest of the `acquire`/`release` code is now modeled after
    Base.Event in how releasing always acquires the lock and slow-path acquires also take the lock to ensure fairness and no deadlocks.
    I've included some benchmark results on a variety of heavy workloads [here](https://everlasting-mahogany-a5f.notion.site/Issue-heavy-load-perf-degradation-1cd275c75037481a9cd6378b8303cfb3)
    that show some great improvements, a bulk of which are attributable to reducing contention when acquiring/releasing connections during requests.
    The other key change included in this rewrite is that we ensure we _do not_ hold any locks while _making new connections_ to avoid the
    possibility of the lock ever getting "stuck", and because it's not necessary: the pod is in charge of just keeping track of numbers and
    doesn't need to worry about whether the connection was actually made yet or not (if it fails, it will be immediately released back and retried).
    Overall, the code is also _much_ simpler, which I think is a huge win, because the old code was always pretty scary to have to dig into.
  * Added a new `logerrors::Bool=false` keyword arg that allows doing `@error` logs on errors that may otherwise be "swallowed" when doing retries;
    it can be helpful to sometimes be able to at least see what kinds of errors are happening
  * Added lots of metrics around various time spent in various layers, read vs. write durations, etc.
  • Loading branch information
quinnj committed Apr 20, 2023
1 parent 75c1b25 commit 272d575
Show file tree
Hide file tree
Showing 31 changed files with 428 additions and 651 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "1.7.4"
[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
ConcurrentUtilities = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
LoggingExtras = "e6f89c97-d47a-5376-807f-9c37f3926c36"
Expand Down
12 changes: 11 additions & 1 deletion docs/src/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Many remote web services/APIs have rate limits or throttling in place to avoid b

#### `readtimeout`

After a connection is established and a request is sent, a response is expected. If a non-zero value is passed to the `readtimeout` keyword argument, `HTTP.request` will wait to receive a response that many seconds before throwing an error. Note that for chunked or streaming responses, each chunk/packet of bytes received causes the timeout to reset. Passing `readtimeout = 0` disables any timeout checking and is the default.
After a connection is established and a request is sent, a response is expected. If a non-zero value is passed to the `readtimeout` keyword argument, `HTTP.request` will wait to receive a response that many seconds before throwing an error. Passing `readtimeout = 0` disables any timeout checking and is the default.

### `status_exception`

Expand Down Expand Up @@ -150,6 +150,16 @@ Controls the total number of retries that will be attempted. Can also disable al

By default, this keyword argument is `false`, which controls whether non-idempotent requests will be retried (POST or PATCH requests).

#### `retry_delays`

Allows providing a custom `ExponentialBackOff` object to control the delay between retries.
Default is `ExponentialBackOff(n = retries)`.

#### `retry_check`

Allows providing a custom function to control whether a retry should be attempted.
The function should accept 5 arguments: the delay state, exception, request, response (an `HTTP.Response` object *if* a request was successfully made, otherwise `nothing`), and `resp_body` response body (which may be `nothing` if there is no response yet, otherwise a `Vector{UInt8}`), and return `true` if a retry should be attempted. So in traditional nomenclature, the function would have the form `f(s, ex, req, resp, resp_body) -> Bool`.

### Redirect Arguments

#### `redirect`
Expand Down
8 changes: 2 additions & 6 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ HTTP.Messages.isidempotent
HTTP.Messages.retryable
HTTP.Messages.defaultheader!
HTTP.Messages.readheaders
HTTP.DefaultHeadersRequest.setuseragent!
HTTP.HeadersRequest.setuseragent!
HTTP.Messages.readchunksize
HTTP.Messages.headerscomplete(::HTTP.Messages.Response)
HTTP.Messages.writestartline
Expand Down Expand Up @@ -167,17 +167,13 @@ HTTP.poplayer!
HTTP.popfirstlayer!
HTTP.MessageRequest.messagelayer
HTTP.RedirectRequest.redirectlayer
HTTP.DefaultHeadersRequest.defaultheaderslayer
HTTP.BasicAuthRequest.basicauthlayer
HTTP.HeadersRequest.headerslayer
HTTP.CookieRequest.cookielayer
HTTP.CanonicalizeRequest.canonicalizelayer
HTTP.TimeoutRequest.timeoutlayer
HTTP.ExceptionRequest.exceptionlayer
HTTP.RetryRequest.retrylayer
HTTP.ConnectionRequest.connectionlayer
HTTP.DebugRequest.debuglayer
HTTP.StreamRequest.streamlayer
HTTP.ContentTypeDetection.contenttypedetectionlayer
```

### Raw Request Connection
Expand Down
Loading

0 comments on commit 272d575

Please sign in to comment.