Trait ContextTimers

Source
pub trait ContextTimers<'ctx>: SealedContext<'ctx> {
    // Provided method
    fn sleep(
        &self,
        duration: Duration,
    ) -> impl DurableFuture<Output = Result<(), TerminalError>> + 'ctx { ... }
}
Expand description

§Scheduling & Timers

The Restate SDK includes durable timers. You can use these to let handlers sleep for a specified time, or to schedule a handler to be called at a later time. These timers are resilient to failures and restarts. Restate stores and keeps track of the timers and triggers them on time, even across failures and restarts.

§Scheduling Async Tasks

To schedule a handler to be called at a later time, have a look at the documentation on delayed calls.

§Durable sleep

To sleep in a Restate application for ten seconds, do the following:

ctx.sleep(Duration::from_secs(10)).await?;

Cost savings on FaaS: Restate suspends the handler while it is sleeping, to free up resources. This is beneficial for AWS Lambda deployments, since you don’t pay for the time the handler is sleeping.

Sleeping in Virtual Objects: Virtual Objects only process a single invocation at a time, so the Virtual Object will be blocked while sleeping.

Clock synchronization Restate Server vs. SDK

The Restate SDK calculates the wake-up time based on the delay you specify. The Restate Server then uses this calculated time to wake up the handler. If the Restate Server and the SDK have different system clocks, the sleep duration might not be accurate. So make sure that the system clock of the Restate Server and the SDK have the same timezone and are synchronized.

Provided Methods§

Source

fn sleep( &self, duration: Duration, ) -> impl DurableFuture<Output = Result<(), TerminalError>> + 'ctx

Sleep using Restate

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'ctx, CTX: SealedContext<'ctx>> ContextTimers<'ctx> for CTX