-
-
Notifications
You must be signed in to change notification settings - Fork 4
Task runner (mid-level API), part 1 #172
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
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02c5780
feat: init Scheduler::run_task fn
soywod 882f0c1
ci: fix check
soywod 320e073
feat: ensure that Scheduler is Send
soywod 90d04af
fix: removed task utilities from Scheduler
soywod 376d800
fix: adjust code after review
soywod 59e3e66
feat: move run task to a dedicated Resolver
soywod e64fb13
fix: adjust resolver-related namings
soywod 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use imap_flow::{ | ||
| client::{ClientFlow, ClientFlowOptions}, | ||
| stream::Stream, | ||
| }; | ||
| use tasks::{ | ||
| resolver::Resolver, | ||
| tasks::{authenticate::AuthenticateTask, capability::CapabilityTask, logout::LogoutTask}, | ||
| }; | ||
| use tokio::net::TcpStream; | ||
|
|
||
| #[tokio::main] | ||
| async fn main() { | ||
| let stream = TcpStream::connect("127.0.0.1:12345").await.unwrap(); | ||
| let mut stream = Stream::insecure(stream); | ||
| let client = ClientFlow::new(ClientFlowOptions::default()); | ||
| let mut resolver = Resolver::new(client); | ||
|
|
||
| let capability = stream | ||
| .progress(resolver.resolve(CapabilityTask::new())) | ||
| .await | ||
| .unwrap() | ||
| .unwrap(); | ||
|
|
||
| println!("pre-auth capability: {capability:?}"); | ||
|
|
||
| let capability = stream | ||
| .progress(resolver.resolve(AuthenticateTask::plain("alice", "pa²²w0rd", true))) | ||
| .await | ||
| .unwrap() | ||
| .unwrap(); | ||
|
|
||
| println!("maybe post-auth capability: {capability:?}"); | ||
|
|
||
| let capability = stream | ||
| .progress(resolver.resolve(CapabilityTask::new())) | ||
| .await | ||
| .unwrap() | ||
| .unwrap(); | ||
|
|
||
| println!("post-auth capability: {capability:?}"); | ||
|
|
||
| stream | ||
| .progress(resolver.resolve(LogoutTask::default())) | ||
| .await | ||
| .unwrap() | ||
| .unwrap(); | ||
| } |
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,87 @@ | ||
| use imap_flow::{client::ClientFlow, Flow, FlowInterrupt}; | ||
| use imap_types::response::{Response, Status}; | ||
| use tracing::warn; | ||
|
|
||
| use crate::{Scheduler, SchedulerError, SchedulerEvent, Task, TaskHandle}; | ||
|
|
||
| /// The resolver is a scheduler than manages one task at a time. | ||
| pub struct Resolver { | ||
| scheduler: Scheduler, | ||
| } | ||
|
|
||
| impl Flow for Resolver { | ||
| type Event = SchedulerEvent; | ||
| type Error = SchedulerError; | ||
|
|
||
| fn enqueue_input(&mut self, bytes: &[u8]) { | ||
| self.scheduler.enqueue_input(bytes); | ||
| } | ||
|
|
||
| fn progress(&mut self) -> Result<Self::Event, FlowInterrupt<Self::Error>> { | ||
| self.scheduler.progress() | ||
| } | ||
| } | ||
|
|
||
| impl Resolver { | ||
| /// Create a new resolver. | ||
| pub fn new(flow: ClientFlow) -> Self { | ||
jakoschiko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Self { | ||
| scheduler: Scheduler::new(flow), | ||
| } | ||
| } | ||
|
|
||
| /// Enqueue a [`Task`] for immediate resolution. | ||
| pub fn resolve<T: Task>(&mut self, task: T) -> ResolvingTask<T> { | ||
| let handle = self.scheduler.enqueue_task(task); | ||
|
|
||
| ResolvingTask { | ||
| resolver: self, | ||
| handle, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub struct ResolvingTask<'a, T: Task> { | ||
| resolver: &'a mut Resolver, | ||
| handle: TaskHandle<T>, | ||
| } | ||
|
|
||
| impl<T: Task> Flow for ResolvingTask<'_, T> { | ||
| type Event = T::Output; | ||
| type Error = SchedulerError; | ||
|
|
||
| fn enqueue_input(&mut self, bytes: &[u8]) { | ||
| self.resolver.enqueue_input(bytes); | ||
| } | ||
|
|
||
| fn progress(&mut self) -> Result<Self::Event, FlowInterrupt<Self::Error>> { | ||
| loop { | ||
| match self.resolver.progress()? { | ||
| SchedulerEvent::TaskFinished(mut token) => { | ||
| if let Some(output) = self.handle.resolve(&mut token) { | ||
| break Ok(output); | ||
| } else { | ||
| warn!(?token, "received unexpected task token") | ||
| } | ||
| } | ||
| SchedulerEvent::Unsolicited(unsolicited) => { | ||
| if let Response::Status(Status::Bye(bye)) = unsolicited { | ||
| let err = SchedulerError::UnexpectedByeResponse(bye); | ||
| break Err(FlowInterrupt::Error(err)); | ||
| } else { | ||
| warn!(?unsolicited, "received unsolicited"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use static_assertions::assert_impl_all; | ||
|
|
||
| use super::Resolver; | ||
|
|
||
| assert_impl_all!(Resolver: Send); | ||
| } | ||
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.