-
Notifications
You must be signed in to change notification settings - Fork 9
Fix early drop stateful nodes #159
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
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use futures::Stream; | ||
use pin_project::{pin_project, pinned_drop}; | ||
use std::fmt::Display; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
/// The reason why the stream ended: | ||
/// - [CallbackStreamEndReason::Finished] if it finished gracefully | ||
/// - [CallbackStreamEndReason::Aborted] if it was abandoned. | ||
#[derive(Debug)] | ||
pub enum CallbackStreamEndReason { | ||
/// The stream finished gracefully. | ||
Finished, | ||
/// The stream was abandoned. | ||
Aborted, | ||
} | ||
|
||
impl Display for CallbackStreamEndReason { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{:?}", self) | ||
} | ||
} | ||
|
||
/// Stream that executes a callback when it is fully consumed or gets cancelled. | ||
#[pin_project(PinnedDrop)] | ||
pub struct CallbackStream<S, F> | ||
where | ||
S: Stream, | ||
F: FnOnce(CallbackStreamEndReason), | ||
{ | ||
#[pin] | ||
stream: S, | ||
callback: Option<F>, | ||
} | ||
|
||
impl<S, F> Stream for CallbackStream<S, F> | ||
where | ||
S: Stream, | ||
F: FnOnce(CallbackStreamEndReason), | ||
{ | ||
type Item = S::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let this = self.project(); | ||
|
||
match this.stream.poll_next(cx) { | ||
Poll::Ready(None) => { | ||
// Stream is fully consumed, execute the callback | ||
if let Some(callback) = this.callback.take() { | ||
callback(CallbackStreamEndReason::Finished); | ||
} | ||
Poll::Ready(None) | ||
} | ||
other => other, | ||
} | ||
} | ||
} | ||
|
||
#[pinned_drop] | ||
impl<S, F> PinnedDrop for CallbackStream<S, F> | ||
where | ||
S: Stream, | ||
F: FnOnce(CallbackStreamEndReason), | ||
{ | ||
fn drop(self: Pin<&mut Self>) { | ||
let this = self.project(); | ||
if let Some(callback) = this.callback.take() { | ||
callback(CallbackStreamEndReason::Aborted); | ||
} | ||
} | ||
} | ||
|
||
/// Wrap a stream with a callback that will be executed when the stream is fully | ||
/// consumed or gets canceled. | ||
pub fn with_callback<S, F>(stream: S, callback: F) -> CallbackStream<S, F> | ||
where | ||
S: Stream, | ||
F: FnOnce(CallbackStreamEndReason) + Send + 'static, | ||
{ | ||
CallbackStream { | ||
stream, | ||
callback: Some(callback), | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod callback_stream; | ||
mod composed_extension_codec; | ||
mod partitioning; | ||
#[allow(unused)] | ||
pub mod ttl_map; | ||
|
||
pub(crate) use callback_stream::with_callback; | ||
pub(crate) use composed_extension_codec::ComposedPhysicalExtensionCodec; | ||
pub(crate) use partitioning::{scale_partitioning, scale_partitioning_props}; |
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
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 read through the implementation and usage. I makes sense and a very nice fix.
My only question is about the name. Is it
CallBack
a common name for this kind of thing? The name does not spark immediate meaning of the work but if it is only me, it is fine to keep the name as-isThere 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.
Yeah, I'd say "callback" is pretty common for this in the software industry. A callback is a function whose execution gets deferred until a certain event happens. In this case, when the stream finishes.