-
Notifications
You must be signed in to change notification settings - Fork 39
feat(spmc): add competing queues #305
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
mxsm
wants to merge
1
commit into
apache:main
Choose a base branch
from
mxsm:mxsm/212-spmc
base: main
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
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
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
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
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,145 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use std::fmt; | ||
| use std::sync::Arc; | ||
|
|
||
| use super::RecvError; | ||
| use super::SendError; | ||
| use super::TryRecvError; | ||
| use super::TrySendError; | ||
| use crate::internal::competing_queue::Shared; | ||
|
|
||
| /// Creates a bounded single-producer, multi-consumer queue. | ||
| /// | ||
| /// The queue stores at most `capacity` values. Sending waits for a receiver to free capacity when | ||
| /// the queue is full. | ||
| /// | ||
| /// Operations briefly acquire internal mutexes. No lock is held across an await point, while | ||
| /// waking tasks, or while dropping messages. The `try_*` methods do not wait for capacity or | ||
| /// messages, but may wait to acquire a mutex. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics if `capacity` is zero. | ||
| #[track_caller] | ||
| pub fn bounded<T>(capacity: usize) -> (BoundedSender<T>, BoundedReceiver<T>) { | ||
| assert!(capacity > 0, "spmc bounded queue requires capacity > 0"); | ||
| let shared = Arc::new(Shared::bounded(capacity)); | ||
| ( | ||
| BoundedSender { | ||
| shared: shared.clone(), | ||
| }, | ||
| BoundedReceiver { shared }, | ||
| ) | ||
| } | ||
|
|
||
| /// Sends values to the associated [`BoundedReceiver`] handles. | ||
| /// | ||
| /// Instances are created by [`bounded`] and cannot be cloned. Sending requires exclusive access to | ||
| /// this endpoint. | ||
| pub struct BoundedSender<T> { | ||
| shared: Arc<Shared<T>>, | ||
| } | ||
|
|
||
| impl<T> fmt::Debug for BoundedSender<T> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("BoundedSender").finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| impl<T> Drop for BoundedSender<T> { | ||
| fn drop(&mut self) { | ||
| self.shared.drop_sender(); | ||
| } | ||
| } | ||
|
|
||
| impl<T> BoundedSender<T> { | ||
| /// Sends a value, waiting until capacity is available if the queue is full. | ||
| /// | ||
| /// If all receivers have been dropped, the value is returned in [`SendError`]. | ||
| /// | ||
| /// # Cancel safety | ||
| /// | ||
| /// Dropping a pending `send` removes it from the wait queue and drops `value`; a call that has | ||
| /// returned `Pending` has not sent the value. Cancelling releases the exclusive sender borrow | ||
| /// and leaves available capacity usable by the next send. Use [`try_send`](Self::try_send) when | ||
| /// the caller must retain ownership if capacity is unavailable. | ||
| pub async fn send(&mut self, value: T) -> Result<(), SendError<T>> { | ||
| self.shared.send(value).await | ||
| } | ||
|
|
||
| /// Attempts to send a value without waiting for capacity. | ||
| /// | ||
| /// Returns [`TrySendError::Full`] when the queue has reached its exact capacity and | ||
| /// [`TrySendError::Disconnected`] when all receivers have been dropped. | ||
| pub fn try_send(&mut self, value: T) -> Result<(), TrySendError<T>> { | ||
| self.shared.try_send(value) | ||
| } | ||
| } | ||
|
|
||
| /// Receives values from the associated [`BoundedSender`] handles. | ||
| /// | ||
| /// Cloned receivers compete for values, and every accepted value is returned by exactly one | ||
| /// receiver while a receiver remains. Dropping the final receiver releases buffered values. | ||
| pub struct BoundedReceiver<T> { | ||
| shared: Arc<Shared<T>>, | ||
| } | ||
|
|
||
| impl<T> Clone for BoundedReceiver<T> { | ||
| fn clone(&self) -> Self { | ||
| self.shared.clone_receiver(); | ||
| Self { | ||
| shared: self.shared.clone(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T> fmt::Debug for BoundedReceiver<T> { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.debug_struct("BoundedReceiver").finish_non_exhaustive() | ||
| } | ||
| } | ||
|
|
||
| impl<T> Drop for BoundedReceiver<T> { | ||
| fn drop(&mut self) { | ||
| self.shared.drop_receiver(); | ||
| } | ||
| } | ||
|
|
||
| impl<T> BoundedReceiver<T> { | ||
| /// Receives the next available value. | ||
| /// | ||
| /// Buffered values remain available after the sender is dropped. Once they are drained, | ||
| /// this method returns [`RecvError::Disconnected`]. | ||
| /// | ||
| /// # Cancel safety | ||
| /// | ||
| /// Dropping a pending `recv` does not consume a value. Any selected value notification is | ||
| /// passed to another waiting receiver, so cancellation does not prevent it from receiving. | ||
| pub async fn recv(&self) -> Result<T, RecvError> { | ||
| self.shared.recv().await | ||
| } | ||
|
|
||
| /// Attempts to receive the next available value without waiting for a message. | ||
| /// | ||
| /// Returns [`TryRecvError::Empty`] while the queue is empty and a sender remains, or | ||
| /// [`TryRecvError::Disconnected`] once the queue is empty and the sender has been dropped. | ||
| pub fn try_recv(&self) -> Result<T, TryRecvError> { | ||
| self.shared.try_recv() | ||
| } | ||
| } |
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 disagree this style just for "reusing code".
Duplicate the code a bit is helpful to decouple concepts for evolution.
At least we should never public an
internalstruct but have another place to hold it. Or else we don't do software engineering but putting code randomly.