Skip to content

Commit 28e4c2e

Browse files
authored
Add note on retry policies for ctx.run (#39)
1 parent de819f5 commit 28e4c2e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/context/mod.rs

+33
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,39 @@ pub trait ContextSideEffects<'ctx>: private::SealedContext<'ctx> {
676676
///
677677
/// For more info about serialization of the return values, see [crate::serde].
678678
///
679+
/// You can configure the retry policy for the `ctx.run` block:
680+
/// ```rust,no_run
681+
/// # use std::time::Duration;
682+
/// # use restate_sdk::prelude::*;
683+
/// # async fn handle(ctx: Context<'_>) -> Result<(), HandlerError> {
684+
/// let my_run_retry_policy = RunRetryPolicy::default()
685+
/// .initial_delay(Duration::from_millis(100))
686+
/// .exponentiation_factor(2.0)
687+
/// .max_delay(Duration::from_millis(1000))
688+
/// .max_attempts(10)
689+
/// .max_duration(Duration::from_secs(10));
690+
/// ctx.run(|| write_to_other_system())
691+
/// .retry_policy(my_run_retry_policy)
692+
/// .await
693+
/// .map_err(|e| {
694+
/// // Handle the terminal error after retries exhausted
695+
/// // For example, undo previous actions (see sagas guide) and
696+
/// // propagate the error back to the caller
697+
/// e
698+
/// })?;
699+
/// # Ok(())
700+
/// # }
701+
/// # async fn write_to_other_system() -> Result<String, HandlerError>{
702+
/// # Ok("Hello".to_string())
703+
/// # }
704+
/// ```
705+
///
706+
/// This way you can override the default retry behavior of your Restate service for specific operations.
707+
/// Have a look at [`RunFuture::retry_policy`] for more information.
708+
///
709+
/// If you set a maximum number of attempts, then the `ctx.run` block will fail with a [TerminalError] once the retries are exhausted.
710+
/// 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.
711+
///
679712
/// **Caution: Immediately await journaled actions:**
680713
/// Always immediately await `ctx.run`, before doing any other context calls.
681714
/// If not, you might bump into non-determinism errors during replay,

0 commit comments

Comments
 (0)