Support drain into blocking#61
Open
acking-you wants to merge 3 commits intofereidani:mainfrom
Open
Conversation
Author
|
@fereidani Would appreciate a review when you have time. Thanks! |
Author
|
I've applied the implementation of this API to a locally-used geo-location-based smart proxy channel API, which is utilized by around 100 people (as described in the issue). A background task handles batch requests to the geo API to avoid triggering rate limits on IP-based geo APIs caused by multi-threaded access. This is an MPSC scenario, and the solution has worked very well—running for over a week now without any error! |
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.
Closes #60
Summary
Add
drain_into_blockingmethod for both sync and async receivers. This method drains all available messages into a vector, waiting until at least one message is received (unless the channel is closed).API
Note: For the async version, "blocking" refers to the semantic behavior (waiting for data), not thread blocking. The method is fully async and yields to the runtime.
Implementation
Why
drain_intouses shared macro butdrain_into_blockingdoesn'tdrain_intois non-blocking - it only retrieves currently available data and returns immediately. No waiting mechanism needed, so sync/async receivers share the same code viashared_recv_impl!()macro.drain_into_blockingneeds to wait when no data is available. Sync and async waiting mechanisms are fundamentally different:SyncSignalAsyncSignalthread::park()Poll::Pending+Wakerthread::unpark()waker.wake()This is the same pattern as
recv()vsReceiveFuture.Core Logic
drain_into)Files Changed
src/lib.rs: AddReceiver::drain_into_blocking+AsyncReceiver::drain_into_blockingwith doctest examplessrc/future.rs: AddDrainIntoBlockingFuturewithFutureandDropimpltests/sync_test.rs: Add tests (basic, wait behavior, MPSC)tests/async_test.rs: Add tests (basic, wait behavior, MPSC)