-
Notifications
You must be signed in to change notification settings - Fork 63
bitreq: Evict dead entries from async connection pool
#564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tnull
wants to merge
3
commits into
rust-bitcoin:master
Choose a base branch
from
tnull:2026-04-bitreq-async-pool-fix
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| //! Regression test for the async [`Client`](bitreq::Client) pool's LRU | ||
| //! bookkeeping: a cache hit must move the entry to the most-recently-used | ||
| //! slot, otherwise capacity-driven eviction drops still-warm keys. | ||
|
|
||
| #![cfg(feature = "async")] | ||
|
|
||
| use std::sync::atomic::{AtomicUsize, Ordering}; | ||
| use std::sync::Arc; | ||
|
|
||
| use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
| use tokio::net::{TcpListener, TcpStream}; | ||
|
|
||
| async fn bind_ephemeral() -> (TcpListener, String) { | ||
| let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
| let port = listener.local_addr().unwrap().port(); | ||
| let base_url = format!("http://127.0.0.1:{}", port); | ||
| (listener, base_url) | ||
| } | ||
|
|
||
| /// Reads bytes from `stream` until the HTTP header terminator `\r\n\r\n` | ||
| /// is seen. Returns the accumulated buffer. Assumes no request body, which | ||
| /// is true for the GETs issued by this test. | ||
| async fn read_request_headers(stream: &mut TcpStream) -> std::io::Result<Vec<u8>> { | ||
| let mut buf = Vec::with_capacity(512); | ||
| let mut chunk = [0u8; 256]; | ||
| loop { | ||
| let n = stream.read(&mut chunk).await?; | ||
| if n == 0 { | ||
| return Err(std::io::ErrorKind::UnexpectedEof.into()); | ||
| } | ||
| buf.extend_from_slice(&chunk[..n]); | ||
| if buf.windows(4).any(|w| w == b"\r\n\r\n") { | ||
| return Ok(buf); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const KEEP_ALIVE_RESPONSE: &[u8] = | ||
| b"HTTP/1.1 200 OK\r\nContent-Length: 3\r\nConnection: keep-alive\r\nKeep-Alive: timeout=60\r\n\r\nok\n"; | ||
|
|
||
| #[tokio::test] | ||
| async fn pool_hit_refreshes_lru_position() { | ||
| // Capacity = 2, three distinct hosts (= three distinct `ConnectionKey`s | ||
| // because the port differs). Request order: a, b, a, c, a. | ||
| // | ||
| // A correct LRU refresh-on-hit moves `a` to the most-recent slot at | ||
| // step 3, so step 4's capacity-driven eviction drops `b`, and step 5 | ||
| // is a cache hit on `a` — three TCP accepts total. A pool that does | ||
| // not refresh LRU on hit still has `a` as the oldest entry after | ||
| // step 3, so step 4 evicts `a` instead, and step 5 is a miss — | ||
| // four TCP accepts total. | ||
| async fn run_server(listener: TcpListener, accepts: Arc<AtomicUsize>) { | ||
| loop { | ||
| let (mut stream, _) = match listener.accept().await { | ||
| Ok(s) => s, | ||
| Err(_) => return, | ||
| }; | ||
| accepts.fetch_add(1, Ordering::SeqCst); | ||
| tokio::spawn(async move { | ||
| loop { | ||
| if read_request_headers(&mut stream).await.is_err() { | ||
| return; | ||
| } | ||
| if stream.write_all(KEEP_ALIVE_RESPONSE).await.is_err() { | ||
| return; | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| let (listener_a, url_a) = bind_ephemeral().await; | ||
| let (listener_b, url_b) = bind_ephemeral().await; | ||
| let (listener_c, url_c) = bind_ephemeral().await; | ||
|
|
||
| let accepts_a = Arc::new(AtomicUsize::new(0)); | ||
| let accepts_b = Arc::new(AtomicUsize::new(0)); | ||
| let accepts_c = Arc::new(AtomicUsize::new(0)); | ||
|
|
||
| let srv_a = tokio::spawn(run_server(listener_a, Arc::clone(&accepts_a))); | ||
| let srv_b = tokio::spawn(run_server(listener_b, Arc::clone(&accepts_b))); | ||
| let srv_c = tokio::spawn(run_server(listener_c, Arc::clone(&accepts_c))); | ||
|
|
||
| let client = bitreq::Client::new(2); | ||
| for url in [&url_a, &url_b, &url_a, &url_c, &url_a] { | ||
| let response = client.send_async(bitreq::get(format!("{}/x", url))).await.unwrap(); | ||
| assert_eq!(response.status_code, 200); | ||
| assert_eq!(response.as_bytes(), b"ok\n"); | ||
| } | ||
|
|
||
| srv_a.abort(); | ||
| srv_b.abort(); | ||
| srv_c.abort(); | ||
| let _ = tokio::join!(srv_a, srv_b, srv_c); | ||
|
|
||
| let total = accepts_a.load(Ordering::SeqCst) | ||
| + accepts_b.load(Ordering::SeqCst) | ||
| + accepts_c.load(Ordering::SeqCst); | ||
| assert_eq!( | ||
| total, 3, | ||
| "request sequence a,b,a,c,a with capacity=2 must refresh a's LRU \ | ||
| position on the second hit, keeping it warm past the c-driven \ | ||
| eviction — expected 3 accepts (miss a, miss b, miss c), got {}", | ||
| total, | ||
| ); | ||
| assert_eq!(accepts_a.load(Ordering::SeqCst), 1, "a must be reused, not re-opened"); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should evict by identity, not just by
ConnectionKey.There is a race where an older poisoned
Arccan finish later and remove a newer healthy entry for the same key:KKremove(key)drops C's fresh entryThat doesn't break in-flight requests, but it does create avoidable reconnect churn under load. I think
evictneeds to compare the currently pooledArcagainst the one that just finished and only remove onArc::ptr_eq.Permalinks:
send_asynccleanup: https://github.com/tnull/corepc/blob/708d9c388aa148eb3f11a2b3e5d72d361ab116bf/bitreq/src/client.rs#L87-L95evict: https://github.com/tnull/corepc/blob/708d9c388aa148eb3f11a2b3e5d72d361ab116bf/bitreq/src/client.rs#L146-L153