Skip to main content

Crate lambda_runtime_context

Crate lambda_runtime_context 

Source
Expand description

Task-local runtime deadline helpers for async Rust workers.

lambda_runtime_context stores an invocation deadline in a Tokio task-local and exposes small helpers for checking whether enough execution time remains to start more work. It is useful for AWS Lambda handlers, queue consumers, and other bounded async runtimes where a worker should stop early and re-enqueue remaining work before the platform terminates the process.

§Example

use std::time::Duration;

lambda_runtime_context::scope_deadline(deadline_ms, async {
    while lambda_runtime_context::is_duration_available(Duration::from_secs(30)) {
        process_next_item().await;
    }

    enqueue_more_work().await;
})
.await;

§Tokio task-local propagation

Tokio task-locals are scoped to the current task. If you spawn a new task, capture current_deadline_ms and call scope_deadline inside the spawned future.

if let Some(deadline_ms) = lambda_runtime_context::current_deadline_ms() {
    tokio::spawn(async move {
        lambda_runtime_context::scope_deadline(deadline_ms, async {
            do_spawned_work().await;
        })
        .await;
    });
}

Functions§

current_deadline_ms
Returns the raw deadline timestamp (milliseconds since Unix epoch) for the current invocation.
is_duration_available
Returns true if the given time is still available before the deadline.
scope_deadline
Execute the provided future with the Lambda deadline stored in a task-local slot.
time_remaining
Returns how much time is left before the Lambda runtime terminates the invocation.
total_time_limit
Returns the total time limit for the current invocation.