shinseongsu opened a new pull request, #943:
URL: https://github.com/apache/httpcomponents-client/pull/943

   `future.cancel()` on an async request returns `true`, but the exchange keeps 
running when the pool already has a usable connection, which is the normal case 
for a keep-alive client. The connection stays leased until the server answers.
   
   In `AsyncConnectExec#execute`, when the lease completes inside 
`acquireEndpoint()`, the callback has already set the connect / execute step as 
the request's dependency. The outer 
`setDependency(execRuntime.acquireEndpoint(...))` then replaces it with the 
lease cancellable, which is already done. `ComplexFuture#setDependency` just 
overwrites, so the cancel goes to the finished lease.
   
   The change registers a `ComplexCancellable` holder before the lease starts 
and puts the lease into it, so the later step can't be overwritten.
   
   ### How I tested it
   
   Async client with 10 threads and a pool of max 20, against a local classic 
server. 10% of the requests go to an endpoint that takes 3 s, the rest answer 
right away. Every request waits with `future.get(300 ms)` and calls `cancel()` 
on timeout. 150 s per run, only the httpclient5 jar swapped (master 4e109b0e4 
vs this branch), scraped by Prometheus every 2 s.
   
   | | master | patched |
   |---|---|---|
   | successful requests / s | 93 | 259 |
   | requests to the fast endpoint that missed the 300 ms deadline | 1,890 | 0 |
   | leased connections (mean) | 19 of 20 | 10 |
   | pending lease requests (mean) | 26 | 0 |
   | client → server ESTABLISHED sockets after 2 min (`netstat`) | 54, still 
growing | 10 |
   
   On master the cancelled slow requests keep their connections for the full 3 
s, the pool fills up, and requests to the fast endpoint wait for a connection 
and miss the deadline as well. Total deadline misses are a bit higher with the 
patch only because it sends about 3x more requests, so more of them hit the 
slow endpoint.
   
   
![grafana](https://raw.githubusercontent.com/shinseongsu/httpcomponents-client/c4e5d7426e01e481d3b07668cedb893298492bfc/grafana.png)
   
   The socket count going above the pool max on master surprised me. I haven't 
pinned down which path leaves those open, but they're gone with this change 
(flat at 10 for 4 minutes).
   
   Single request on a keep-alive connection, `/slow` takes 3 s, cancel after 
300 ms:
   
   - master: nothing goes out when `cancel()` is called; the response still 
comes back on that connection at 3.03 s
   
   
![master](https://raw.githubusercontent.com/shinseongsu/httpcomponents-client/c4e5d7426e01e481d3b07668cedb893298492bfc/wireshark-master.png)
   
   - patched: RST at 0.34 s
   
   
![patched](https://raw.githubusercontent.com/shinseongsu/httpcomponents-client/c4e5d7426e01e481d3b07668cedb893298492bfc/wireshark-patched.png)
   
   `TestAsyncConnectExec#testCancellationReachesConnectAfterSynchronousLease` 
fails on master ("Wanted but not invoked: connectCancellable.cancel()") and 
passes with the change. httpclient5 `verify`: 1158 tests, 0 failures.
   
   Is the holder approach OK with you, or would you rather handle this in 
`ComplexFuture`? While testing I also found two related cases, which I can send 
as separate PRs if you agree: cancelling during the TLS handshake doesn't close 
the session in `DefaultAsyncClientConnectionOperator`, and 
`MinimalHttpAsyncClient#leaseEndpoint` has the same overwrite.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to