Skip to content

Add note on retry policies for ctx.run #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,39 @@ pub trait ContextSideEffects<'ctx>: private::SealedContext<'ctx> {
///
/// For more info about serialization of the return values, see [crate::serde].
///
/// You can configure the retry policy for the `ctx.run` block:
/// ```rust,no_run
/// # use std::time::Duration;
/// # use restate_sdk::prelude::*;
/// # async fn handle(ctx: Context<'_>) -> Result<(), HandlerError> {
/// let my_run_retry_policy = RunRetryPolicy::default()
/// .initial_delay(Duration::from_millis(100))
/// .exponentiation_factor(2.0)
/// .max_delay(Duration::from_millis(1000))
/// .max_attempts(10)
/// .max_duration(Duration::from_secs(10));
/// ctx.run(|| write_to_other_system())
/// .retry_policy(my_run_retry_policy)
/// .await
/// .map_err(|e| {
/// // Handle the terminal error after retries exhausted
/// // For example, undo previous actions (see sagas guide) and
/// // propagate the error back to the caller
/// e
/// })?;
/// # Ok(())
/// # }
/// # async fn write_to_other_system() -> Result<String, HandlerError>{
/// # Ok("Hello".to_string())
/// # }
/// ```
///
/// This way you can override the default retry behavior of your Restate service for specific operations.
/// Have a look at [`RunFuture::retry_policy`] for more information.
///
/// If you set a maximum number of attempts, then the `ctx.run` block will fail with a [TerminalError] once the retries are exhausted.
/// Have a look at the [Sagas guide](https://docs.restate.dev/guides/sagas) to learn how to undo previous actions of the handler to keep the system in a consistent state.
///
/// **Caution: Immediately await journaled actions:**
/// Always immediately await `ctx.run`, before doing any other context calls.
/// If not, you might bump into non-determinism errors during replay,
Expand Down
Loading