pub struct BackgroundRuntime<AR> {
pub async_runtime: AR,
/* private fields */
}Expand description
BackgroundRuntime has two goals:
- Allow executing different background tasks concurrently on a single tokio runtime.
- Handle graceful shutdown.
The first goal is achieved by holding a (non-owning) handle to an existing tokio
runtime. BackgroundRuntime does not actually create/own/drive the tokio runtime and that needs
to be done by someone else (see BackgroundThread for example). This
is to make BackgroundRuntime more flexible as not all environments support multi-threading
(e.g., wasm) and some may already have a global runtime users might want to reuse.
The graceful shutdown is achieved by combining CancellationToken and TaskTracker. Their
usage is described in tokio tutorial.
Because graceful shutdown requires cooperation with the task (it needs to watch and react to cancellation token) and most tasks don’t actually have any cleanup requirements, we distinguish between “tracked” and “untracked” tasks. Tracked tasks are the tasks that have cleanup requirements (e.g., to ensure files are fsync’ed) and are waited by the runtime before exiting. Untracked tasks are everything else and may get stopped and dropped during runtime shutdown.
When BackgroundRuntime is dropped, all background activities are commanded to stop.
Fields§
§async_runtime: ARImplementations§
Source§impl<AR: AsyncRuntime> BackgroundRuntime<AR>
impl<AR: AsyncRuntime> BackgroundRuntime<AR>
Sourcepub fn new(runtime: AR) -> BackgroundRuntime<AR>
pub fn new(runtime: AR) -> BackgroundRuntime<AR>
Creates a new BackgroundRuntime that runs on the given tokio runtime.
The background runtime is active until stop() is called.