From 721462c0e0fa2b9270fe8c74e36b077b52653880 Mon Sep 17 00:00:00 2001 From: Giselle van Dongen Date: Thu, 16 Jan 2025 12:38:41 +0100 Subject: [PATCH 1/3] Add note on retrying ctx.run --- src/context/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/context/mod.rs b/src/context/mod.rs index bf9da1e..9cdc4bc 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -10,6 +10,7 @@ mod request; mod run; pub use request::{Request, RequestTarget}; pub use run::{RunClosure, RunFuture, RunRetryPolicy}; +use crate::errors; pub type HeaderMap = http::HeaderMap; @@ -676,6 +677,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{ + /// # 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, From 51ca1b790a4d84b839352d71b2350e5ea89cd546 Mon Sep 17 00:00:00 2001 From: Giselle van Dongen Date: Thu, 16 Jan 2025 12:48:34 +0100 Subject: [PATCH 2/3] Cargo fmt --- src/context/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 9cdc4bc..5e00321 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -8,9 +8,9 @@ use std::time::Duration; mod request; mod run; +use crate::errors; pub use request::{Request, RequestTarget}; pub use run::{RunClosure, RunFuture, RunRetryPolicy}; -use crate::errors; pub type HeaderMap = http::HeaderMap; From c4d59f5f42cbab8800cbae311dcad7737234a008 Mon Sep 17 00:00:00 2001 From: Giselle van Dongen Date: Thu, 16 Jan 2025 13:05:13 +0100 Subject: [PATCH 3/3] Remove unused import --- src/context/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/context/mod.rs b/src/context/mod.rs index 5e00321..08fa37a 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -8,7 +8,6 @@ use std::time::Duration; mod request; mod run; -use crate::errors; pub use request::{Request, RequestTarget}; pub use run::{RunClosure, RunFuture, RunRetryPolicy};