Spawn outgoing handler independently so sends flush immediately#281
Open
kofany wants to merge 1 commit intoaatxe:developfrom
Open
Spawn outgoing handler independently so sends flush immediately#281kofany wants to merge 1 commit intoaatxe:developfrom
kofany wants to merge 1 commit intoaatxe:developfrom
Conversation
Previously, the Outgoing future was only polled inside ClientStream::poll_next(), which meant sent messages would not be written to the socket until the next incoming message was awaited. This caused surprising behavior where sleep() between sends had no effect — all messages were batched and flushed at once on the next stream poll. Now, Client::stream() spawns the Outgoing handler as an independent tokio task, so messages are flushed to the socket as soon as the runtime can schedule the task. This makes send timing behave as users expect. Closes aatxe#246
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes the surprising behavior where
send_privmsg()(and allsend_*methods) would buffer messages and only flush them when the incoming stream was polled. This made it impossible to usesleep()between sends to space out messages — they would all be sent at once on the nextstream.next().await.Before:
Outgoingwas polled insideClientStream::poll_next(), coupling send flushing to receive polling.After:
Client::stream()spawnsOutgoingas an independent tokio task viatokio::spawn(), so messages are written to the socket as soon as the runtime schedules the task.The existing
client.outgoing()API for manual control is preserved — if the user already took the outgoing future before callingstream(), there is nothing to spawn.Changes
Cargo.toml: Addrtto tokio features (required fortokio::spawn, already needed in practice by all users)src/client/mod.rs:Client::stream(): SpawnOutgoingviatokio::spawninstead of bundling it intoClientStreamoutgoingfield fromClientStreamand outgoing polling frompoll_next()get_client_value()from blockingthread::sleepto asynctokio::time::sleepso the spawned task can flush on the current-thread test runtimeCloses #246
Test plan
cargo test— 67 tests + 8 doc-tests passcargo test --no-default-features— 45 tests + 7 doc-tests passcargo clippy --all-targets --all-features— no new warningsgit diff --check— no whitespace issues