-
Notifications
You must be signed in to change notification settings - Fork 0
Implement connection actor with prioritised write loop #129
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
leynos
merged 8 commits into
main
from
codex/implement-connection-actor-with-biased-select-loop
Jun 25, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
10398f8
Add connection actor with biased select loop
leynos cd61a43
Expand connection actor tests
leynos 8e0dd0a
Refine graceful shutdown in ConnectionActor
leynos 5a96ae4
Refactor ConnectionActor and update docs
leynos 30a7b43
Clarify shutdown semantics
leynos 50b5062
Refactor connection actor run loop
leynos 1aca14d
Encapsulate queue cleanup and actor state
leynos 8dd96c4
Add accessors for ConnectionActor fields
leynos 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,172 @@ | ||
| //! Connection actor responsible for outbound frames. | ||
| //! | ||
| //! The actor polls a shutdown token, high- and low-priority push queues, | ||
| //! and an optional response stream using a `tokio::select!` loop. The | ||
| //! `biased` keyword ensures high-priority messages are processed before | ||
| //! low-priority ones, with streamed responses handled last. | ||
|
|
||
| use futures::StreamExt; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| use crate::{ | ||
| push::{FrameLike, PushQueues}, | ||
| response::{FrameStream, WireframeError}, | ||
| }; | ||
|
|
||
| /// Actor driving outbound frame delivery for a connection. | ||
| pub struct ConnectionActor<F, E> { | ||
| queues: PushQueues<F>, | ||
| response: Option<FrameStream<F, E>>, // current streaming response | ||
| shutdown: CancellationToken, | ||
| } | ||
|
|
||
| impl<F, E> ConnectionActor<F, E> | ||
| where | ||
| F: FrameLike, | ||
| { | ||
| /// Create a new `ConnectionActor` from the provided components. | ||
| #[must_use] | ||
| pub fn new( | ||
| queues: PushQueues<F>, | ||
| response: Option<FrameStream<F, E>>, | ||
| shutdown: CancellationToken, | ||
| ) -> Self { | ||
| Self { | ||
| queues, | ||
| response, | ||
| shutdown, | ||
| } | ||
| } | ||
|
|
||
| /// Access the underlying push queues. | ||
| /// | ||
| /// This is mainly used in tests to close the queues when no actor is | ||
| /// draining them. | ||
| #[must_use] | ||
| pub fn queues_mut(&mut self) -> &mut PushQueues<F> { &mut self.queues } | ||
|
|
||
| /// Set or replace the current streaming response. | ||
| pub fn set_response(&mut self, stream: Option<FrameStream<F, E>>) { self.response = stream; } | ||
|
|
||
| /// Get a clone of the shutdown token used by the actor. | ||
| #[must_use] | ||
| pub fn shutdown_token(&self) -> CancellationToken { self.shutdown.clone() } | ||
|
|
||
| /// Drive the actor until all sources are exhausted or shutdown is triggered. | ||
| /// | ||
| /// Frames are appended to `out` in the order they are processed. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns a [`WireframeError`] if the response stream yields an error. | ||
| pub async fn run(&mut self, out: &mut Vec<F>) -> Result<(), WireframeError<E>> { | ||
| // If cancellation has already been requested, exit immediately. Nothing | ||
| // will be drained and any streaming response is abandoned. This mirrors | ||
| // a hard shutdown and is required for the tests. | ||
| if self.shutdown.is_cancelled() { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let mut state = ActorState::new(self.response.is_none()); | ||
|
|
||
| while !state.is_done() { | ||
| self.poll_sources(&mut state, out).await?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn poll_sources( | ||
| &mut self, | ||
| state: &mut ActorState, | ||
| out: &mut Vec<F>, | ||
| ) -> Result<(), WireframeError<E>> { | ||
| tokio::select! { | ||
| biased; | ||
|
|
||
| () = self.shutdown.cancelled(), if !state.shutting_down => { | ||
| state.shutting_down = true; | ||
| self.start_shutdown(&mut state.resp_closed); | ||
| } | ||
|
|
||
| res = self.queues.high_priority_rx.recv(), if !state.push.high => { | ||
| Self::handle_push(res, &mut state.push.high, out); | ||
| } | ||
|
|
||
| res = self.queues.low_priority_rx.recv(), if !state.push.low => { | ||
| Self::handle_push(res, &mut state.push.low, out); | ||
| } | ||
|
|
||
| res = async { | ||
| if let Some(stream) = &mut self.response { | ||
| stream.next().await | ||
| } else { | ||
| None | ||
| } | ||
| }, if !state.shutting_down && !state.resp_closed => { | ||
| Self::handle_response(res, &mut state.resp_closed, out)?; | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn start_shutdown(&mut self, resp_closed: &mut bool) { | ||
| self.queues.high_priority_rx.close(); | ||
| self.queues.low_priority_rx.close(); | ||
| // Drop any streaming response so shutdown is prompt. Queued frames are | ||
| // still drained, but streamed responses may be truncated. | ||
| self.response = None; | ||
| *resp_closed = true; | ||
| } | ||
|
|
||
| fn handle_push(res: Option<F>, closed: &mut bool, out: &mut Vec<F>) { | ||
| match res { | ||
| Some(frame) => out.push(frame), | ||
| None => *closed = true, | ||
| } | ||
| } | ||
|
|
||
| fn handle_response( | ||
| res: Option<Result<F, WireframeError<E>>>, | ||
| closed: &mut bool, | ||
| out: &mut Vec<F>, | ||
| ) -> Result<(), WireframeError<E>> { | ||
| match res { | ||
| Some(Ok(frame)) => out.push(frame), | ||
| Some(Err(e)) => return Err(e), | ||
| None => *closed = true, | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| struct PushClosed { | ||
| high: bool, | ||
| low: bool, | ||
| } | ||
|
|
||
| struct ActorState { | ||
| push: PushClosed, | ||
| resp_closed: bool, | ||
| shutting_down: bool, | ||
| } | ||
|
|
||
| impl ActorState { | ||
| fn new(resp_closed: bool) -> Self { | ||
| Self { | ||
| push: PushClosed { | ||
| high: false, | ||
| low: false, | ||
| }, | ||
| resp_closed, | ||
| shutting_down: false, | ||
| } | ||
| } | ||
|
|
||
| fn is_done(&self) -> bool { | ||
| let push_drained = self.push.high && self.push.low; | ||
| push_drained && (self.resp_closed || self.shutting_down) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.