tokio-context
Provides two different methods for cancelling futures with a provided handle for cancelling all
related futures, with a fallback timeout mechanism. This is accomplished either with the
Context API, or with the TaskController API depending on a users needs.
Context
Provides Golang like Context functionality. A Context in this respect is an object that is passed around, primarily to async functions, that is used to determine if long running asynchronous tasks should continue to run, or terminate.
You build a new Context by calling its new constructor, which returns the new
Context along with a Handle. The Handle can either have its cancel
method called, or it can simply be dropped to cancel the context.
Please note that dropping the Handle will cancel the context.
If you would like to create a Context that automatically cancels after a given duration has
passed, use the with_timeout constructor. Using this constructor will still
give you a handle that can be used to immediately cancel the context as well.
Examples
use time;
use Context;
use Duration;
async
async
While this may look no different than simply using tokio::time::timeout, we have retained a
handle that we can use to explicitly cancel the context, and any additionally spawned
contexts.
use Duration;
use time;
use task;
use Context;
async
async
Contexts may also be chained by using the with_parent constructor in conjunction with
RefContexts. Chaining a context means that the context will be cancelled if a parent context is
cancelled. A RefContext is simple a wrapper type around an Arc<Mutex<Context>> with an
identical API to Context. Here are a few examples to demonstrate how chainable contexts work:
use Duration;
use time;
use task;
use RefContext;
async
async
async
The Context pattern is useful if your child future needs to know about the cancel signal. This is highly useful in many situations where a child future needs to perform graceful termination.
In instances where graceful termination of child futures is not needed, the API provided by
TaskController is much nicer to use. It doesn't pollute children with an extra
function argument of the context. It will however perform abrupt future
termination, which may not always be desired.
TaskController
Handles spawning tasks which can also be cancelled by calling cancel on the
task controller. If a std::time::Duration is supplied using the with_timeout
constructor, then any tasks spawned by the TaskController will automatically be
cancelled after the supplied duration has elapsed.
This provides a different API from Context for the same end result. It's nicer to use when you don't need child futures to gracefully shutdown. In cases that you do require graceful shutdown of child futures, you will need to pass a Context down, and incorporate the context into normal program flow for the child function so that they can react to it as needed and perform custom asynchronous cleanup logic.
Examples
use Duration;
use time;
use TaskController;
async
async
License: MIT