pub trait TaskScheduler:
Send
+ Sync
+ Debug {
// Required method
fn acquire_execution_slot<'life0, 'async_trait>(
&'life0 self,
cancel_token: CancellationToken,
) -> Pin<Box<dyn Future<Output = SchedulingResult<Box<dyn ResourceGuard>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}
Expand description
Trait for implementing task scheduling policies
This trait enforces proper cancellation semantics by splitting resource acquisition (which can be cancelled) from task execution (which cannot).
§Design Philosophy
Tasks may or may not support cancellation (depending on whether they were
created with spawn_cancellable
or regular spawn
). This split design ensures:
- Resource acquisition: Can respect cancellation tokens to avoid unnecessary allocation
- Task execution: Always runs to completion; tasks handle their own cancellation
This makes it impossible to accidentally interrupt task execution with tokio::select!
.
Required Methods§
Sourcefn acquire_execution_slot<'life0, 'async_trait>(
&'life0 self,
cancel_token: CancellationToken,
) -> Pin<Box<dyn Future<Output = SchedulingResult<Box<dyn ResourceGuard>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn acquire_execution_slot<'life0, 'async_trait>(
&'life0 self,
cancel_token: CancellationToken,
) -> Pin<Box<dyn Future<Output = SchedulingResult<Box<dyn ResourceGuard>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Acquire resources needed for task execution and return a guard
This method handles resource allocation (permits, queue slots, etc.) and can respect cancellation tokens to avoid unnecessary resource consumption.
§Cancellation Behavior
The cancel_token
is used for scheduler-level cancellation (e.g., “don’t start new work”).
If cancellation is requested before or during resource acquisition, this method
should return SchedulingResult::Cancelled
.
§Arguments
cancel_token
-CancellationToken
for scheduler-level cancellation
§Returns
SchedulingResult::Execute(guard)
- Resources acquired, ready to executeSchedulingResult::Cancelled
- Cancelled before or during resource acquisitionSchedulingResult::Rejected(reason)
- Resources unavailable or policy violation