scylla/policies/retry/retry_policy.rs
1//! Request retries configurations\
2//! To decide when to retry a request the `Session` can use any object which implements
3//! the `RetryPolicy` trait
4
5use crate::errors::RequestAttemptError;
6use crate::frame::types::Consistency;
7
8/// Information about a failed request
9#[non_exhaustive]
10pub struct RequestInfo<'a> {
11 /// The error with which the request failed
12 pub error: &'a RequestAttemptError,
13 /// A request is idempotent if it can be applied multiple times without changing the result of the initial application\
14 /// If set to `true` we can be sure that it is idempotent\
15 /// If set to `false` it is unknown whether it is idempotent
16 pub is_idempotent: bool,
17 /// Consistency with which the request failed
18 pub consistency: Consistency,
19}
20
21/// Returned by implementations of RetryPolicy. Instructs the driver on what
22/// to do about the request after it failed.
23#[derive(Clone, Debug, PartialEq, Eq)]
24#[non_exhaustive]
25pub enum RetryDecision {
26 /// Request will be sent to the same shard on the same host.
27 RetrySameTarget(Option<Consistency>), // None means that the same consistency should be used as before
28 /// Request will be sent to the next target generated by load balancing policy.
29 RetryNextTarget(Option<Consistency>), // ditto
30 /// Fails the whole request.
31 DontRetry,
32 /// Will cause the driver to return an empty successful response.
33 IgnoreWriteError,
34}
35
36/// Specifies a policy used to decide when to retry a request
37pub trait RetryPolicy: std::fmt::Debug + Send + Sync {
38 /// Called for each new request, starts a session of deciding about retries
39 fn new_session(&self) -> Box<dyn RetrySession>;
40}
41
42/// Used throughout a single request to decide when to retry it
43/// After this request is finished it is destroyed or reset
44pub trait RetrySession: Send + Sync {
45 /// Called after the request failed - decide what to do next
46 fn decide_should_retry(&mut self, request_info: RequestInfo) -> RetryDecision;
47
48 /// Reset before using for a new request
49 fn reset(&mut self);
50}