Module coop
Utilities for improved cooperative scheduling.
Cooperative scheduling
A single call to poll on a top-level task may potentially do a lot of
work before it returns Poll::Pending. If a task runs for a long period of
time without yielding back to the executor, it can starve other tasks
waiting on that executor to execute them, or drive underlying resources.
Since Rust does not have a runtime, it is difficult to forcibly preempt a
long-running task. Instead, this module provides an opt-in mechanism for
futures to collaborate with the executor to avoid starvation.
Consider a future like this one:
# use ;
async
It may look harmless, but consider what happens under heavy load if the
input stream is always ready. If we spawn drop_all, the task will never
yield, and will starve other tasks and resources on the same executor.
To account for this, Tokio has explicit yield points in a number of library functions, which force tasks to return to the executor periodically.
unconstrained
If necessary, task::unconstrained lets you opt a future out of Tokio's cooperative
scheduling. When a future is wrapped with unconstrained, it will never be forced to yield to
Tokio. For example:
#
# async
Structs
-
Coop
Future wrapper to ensure cooperative scheduling created by
cooperative. -
RestoreOnPending
Value returned by the
poll_proceedmethod. -
Unconstrained
Future for the
unconstrainedmethod.
Functions
- consume_budget Consumes a unit of budget and returns the execution back to the Tokio runtime if the task's coop budget was exhausted.
- cooperative Creates a wrapper future that makes the inner future cooperate with the Tokio scheduler.
-
has_budget_remaining
Returns
trueif there is still budget left on the task. -
poll_proceed
Decrements the task budget and returns
Poll::Pendingif the budget is depleted. This indicates that the task should yield to the scheduler. Otherwise, returnsRestoreOnPendingwhich can be used to commit the budget consumption. - unconstrained Turn off cooperative scheduling for a future. The future will never be forced to yield by Tokio. Using this exposes your service to starvation if the unconstrained future never yields otherwise.