-
Notifications
You must be signed in to change notification settings - Fork 0
Add LocalServiceManager with start_local for LocalSet-driven services #1
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c36d189
add LocalServiceManager with start_local for LocalSet-driven services
mcharytoniuk 69e9302
document cooperative cancellation contract and add ticker example to …
mcharytoniuk dbda687
re-export RegisteredService and LocalRegisteredService in lib.rs
mcharytoniuk e832e0c
split trzcina into trzcina-service, trzcina-sendable-service, and trz…
mcharytoniuk e20ad54
gate all service crates at 100% code coverage
mcharytoniuk 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
Some comments aren't visible on the classic Files Changed page.
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,24 @@ | ||
| [package] | ||
| name = "trzcina-local-service" | ||
| version = "0.2.0" | ||
| edition = "2024" | ||
| license = "Apache-2.0" | ||
| description = "Local (!Send) services for trzcina: cooperative async lifecycle on a tokio LocalSet." | ||
| repository = "https://github.com/intentee/trzcina" | ||
| homepage = "https://github.com/intentee/trzcina" | ||
| readme = "../README.md" | ||
| authors = ["Intentee"] | ||
| keywords = ["async", "service", "lifecycle", "cancellation", "tokio"] | ||
| categories = ["asynchronous", "concurrency"] | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| async-trait = { workspace = true } | ||
| futures-util = { workspace = true } | ||
| log = { workspace = true } | ||
| tokio = { workspace = true } | ||
| tokio-util = { workspace = true } | ||
| trzcina-service = { version = "0.2.0", path = "../trzcina-service" } | ||
|
|
||
| [lints] | ||
| workspace = true |
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,11 @@ | ||
| mod local_registered_service; | ||
| mod local_running_service_collection; | ||
| mod local_service; | ||
| mod local_service_bundle; | ||
| mod local_service_manager; | ||
|
|
||
| pub use crate::local_registered_service::LocalRegisteredService; | ||
| pub use crate::local_running_service_collection::LocalRunningServiceCollection; | ||
| pub use crate::local_service::LocalService; | ||
| pub use crate::local_service_bundle::LocalServiceBundle; | ||
| pub use crate::local_service_manager::LocalServiceManager; |
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,6 @@ | ||
| use crate::local_service::LocalService; | ||
|
|
||
| pub struct LocalRegisteredService { | ||
| pub name: &'static str, | ||
| pub service: Box<dyn LocalService>, | ||
| } |
95 changes: 95 additions & 0 deletions
95
trzcina-local-service/src/local_running_service_collection.rs
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,95 @@ | ||
| use tokio::sync::oneshot; | ||
| use tokio::task::JoinSet; | ||
| use tokio::task::LocalSet; | ||
| use tokio_util::sync::CancellationToken; | ||
| use trzcina_service::RunToCompletionOptions; | ||
| use trzcina_service::RunningCollection; | ||
| use trzcina_service::RunningService; | ||
| use trzcina_service::ServiceShutdownOutcome; | ||
| use trzcina_service::ServiceShutdownOutcomeCollection; | ||
| use trzcina_service::ServiceShutdownOutcomeWithServiceName; | ||
| use trzcina_service::SiblingCancellationGuard; | ||
| use trzcina_service::classify_future_outcome; | ||
| use trzcina_service::drain_to_completion; | ||
|
|
||
| use crate::local_registered_service::LocalRegisteredService; | ||
|
|
||
| pub struct LocalRunningServiceCollection { | ||
| cancellation_token: CancellationToken, | ||
| local_set: LocalSet, | ||
| running_services: Vec<RunningService>, | ||
| task_set: JoinSet<()>, | ||
| } | ||
|
|
||
| impl LocalRunningServiceCollection { | ||
| #[must_use] | ||
| pub fn start( | ||
|
mcharytoniuk marked this conversation as resolved.
|
||
| registered: Vec<LocalRegisteredService>, | ||
| cancellation_token: CancellationToken, | ||
| ) -> Self { | ||
| let mut running_services: Vec<RunningService> = Vec::with_capacity(registered.len()); | ||
| let mut task_set: JoinSet<()> = JoinSet::new(); | ||
| let local_set = LocalSet::new(); | ||
| let internal_cancellation_token = cancellation_token.child_token(); | ||
|
|
||
| for LocalRegisteredService { name, service } in registered { | ||
| let (outcome_sender, outcome_receiver) = oneshot::channel::<ServiceShutdownOutcome>(); | ||
| let service_cancellation_token = internal_cancellation_token.clone(); | ||
|
|
||
| task_set.spawn_local_on( | ||
| async move { | ||
| let _sibling_cancellation_guard = | ||
| SiblingCancellationGuard::new(service_cancellation_token.clone()); | ||
| let mut service = service; | ||
| let outcome = | ||
| classify_future_outcome(name, service.run(service_cancellation_token)) | ||
| .await; | ||
| let _ = outcome_sender.send(outcome); | ||
| }, | ||
| &local_set, | ||
| ); | ||
|
|
||
| running_services.push(RunningService::new(name, outcome_receiver)); | ||
| } | ||
|
|
||
| Self { | ||
| cancellation_token: internal_cancellation_token, | ||
| local_set, | ||
| running_services, | ||
| task_set, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl RunningCollection for LocalRunningServiceCollection { | ||
| async fn run_to_completion( | ||
| self, | ||
| options: RunToCompletionOptions, | ||
| ) -> ServiceShutdownOutcomeCollection { | ||
| let Self { | ||
| cancellation_token, | ||
| local_set, | ||
| running_services, | ||
| mut task_set, | ||
| } = self; | ||
|
|
||
| let has_running_services = !running_services.is_empty(); | ||
|
|
||
| local_set | ||
| .run_until(async { | ||
| drain_to_completion( | ||
| &mut task_set, | ||
| &cancellation_token, | ||
| has_running_services, | ||
| options.shutdown_deadline, | ||
| ) | ||
| .await; | ||
| }) | ||
| .await; | ||
|
|
||
| let outcomes: Vec<ServiceShutdownOutcomeWithServiceName> = | ||
| running_services.into_iter().map(Into::into).collect(); | ||
|
|
||
| ServiceShutdownOutcomeCollection::new(outcomes) | ||
| } | ||
| } | ||
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,12 @@ | ||
| use anyhow::Result; | ||
| use async_trait::async_trait; | ||
| use tokio_util::sync::CancellationToken; | ||
|
|
||
| #[async_trait(?Send)] | ||
| pub trait LocalService: 'static { | ||
| fn name(&self) -> &'static str { | ||
| std::any::type_name::<Self>() | ||
| } | ||
|
|
||
| async fn run(&mut self, cancellation_token: CancellationToken) -> Result<()>; | ||
| } |
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,9 @@ | ||
| use anyhow::Result; | ||
| use async_trait::async_trait; | ||
|
|
||
| use crate::local_service::LocalService; | ||
|
|
||
| #[async_trait(?Send)] | ||
| pub trait LocalServiceBundle { | ||
| async fn services(self) -> Result<Vec<Box<dyn LocalService>>>; | ||
| } |
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,43 @@ | ||
| use anyhow::Result; | ||
| use tokio_util::sync::CancellationToken; | ||
| use trzcina_service::Manager; | ||
|
|
||
| use crate::local_registered_service::LocalRegisteredService; | ||
| use crate::local_running_service_collection::LocalRunningServiceCollection; | ||
| use crate::local_service::LocalService; | ||
| use crate::local_service_bundle::LocalServiceBundle; | ||
|
|
||
| #[derive(Default)] | ||
| pub struct LocalServiceManager { | ||
| services: Vec<LocalRegisteredService>, | ||
| } | ||
|
|
||
| impl LocalServiceManager { | ||
| pub async fn register_bundle<TLocalServiceBundle: LocalServiceBundle>( | ||
| &mut self, | ||
| bundle: TLocalServiceBundle, | ||
| ) -> Result<()> { | ||
| for service in bundle.services().await? { | ||
| let name = service.name(); | ||
| self.services.push(LocalRegisteredService { name, service }); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn register_service(&mut self, service: impl LocalService) { | ||
| let name = service.name(); | ||
| self.services.push(LocalRegisteredService { | ||
| name, | ||
| service: Box::new(service), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| impl Manager for LocalServiceManager { | ||
| type Running = LocalRunningServiceCollection; | ||
|
|
||
| fn start(self, cancellation_token: CancellationToken) -> LocalRunningServiceCollection { | ||
| LocalRunningServiceCollection::start(self.services, cancellation_token) | ||
| } | ||
|
Comment on lines
+37
to
+42
|
||
| } | ||
Oops, something went wrong.
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.