sift_rs 0.8.1

General Rust client library for the Sift API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Generic retry extension for gRPC wrapper services.
//!
//! This module provides a retry mechanism that can be applied to any wrapper service
//! without modifying the wrapper traits themselves. The retry logic intelligently
//! extracts `tonic::Status` from `sift_error::Error` types to make retry decisions.
//!
//! ## Usage
//!
//! The retry mechanism uses a closure pattern to work around Rust's borrow checker
//! and the `&mut self` requirement of tonic clients. Each retry attempt clones the
//! wrapper and calls the closure, allowing the closure to use `&mut self` internally.
//!
//! ```no_run
//! use sift_rs::retry::{RetryExt, RetryConfig};
//! use sift_rs::wrappers::assets::new_asset_service;
//! use sift_rs::wrappers::assets::AssetServiceWrapper;
//! use std::time::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let channel = todo!();
//! let wrapper = new_asset_service(channel);
//! let cfg = RetryConfig {
//!     max_attempts: 3,
//!     base_delay: Duration::from_millis(100),
//!     max_delay: Duration::from_secs(5),
//!     backoff_multiplier: 2.0,
//! };
//!
//! let svc = wrapper.retrying(cfg);
//! let asset = svc.call(|mut w| async move {
//!     w.try_get_asset_by_id("asset-123").await
//! }).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Important Notes
//!
//! - **Idempotency**: Only use retries for idempotent operations. Non-idempotent
//!   operations may be executed multiple times if retries occur.
//! - **Streaming RPCs**: This retry mechanism does not support streaming RPCs.
//!   Streaming calls require recreating the stream and may have side effects.

use std::error::Error as StdError;
use std::future::Future;
use std::result::Result as StdResult;
use std::time::Duration;

use sift_error::prelude::*;
use tonic;

/// Configuration for retry behavior.
#[derive(Debug, Clone)]
pub struct RetryConfig {
    /// Maximum number of retry attempts (including the initial attempt).
    pub max_attempts: usize,
    /// Base delay for exponential backoff.
    pub base_delay: Duration,
    /// Maximum delay cap for exponential backoff.
    pub max_delay: Duration,
    /// Multiplier for exponential backoff (e.g., 2.0 for doubling each attempt).
    pub backoff_multiplier: f64,
}

impl Default for RetryConfig {
    /// Creates a default retry configuration with conservative settings:
    /// - 3 attempts total
    /// - 100ms base delay
    /// - 5s maximum delay
    /// - 2.0 backoff multiplier (exponential)
    fn default() -> Self {
        Self {
            max_attempts: 3,
            base_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(5),
            backoff_multiplier: 2.0,
        }
    }
}

impl RetryConfig {
    /// Calculates the backoff delay for a given attempt number.
    ///
    /// The delay is calculated as: `base_delay * (backoff_multiplier ^ (attempt - 1))`
    /// and is capped at `max_delay`.
    ///
    /// # Arguments
    ///
    /// * `attempt` - The attempt number (1-indexed). For attempt 1, returns `base_delay`.
    pub fn backoff(&self, attempt: usize) -> Duration {
        if attempt <= 1 {
            return self.base_delay;
        }

        let exponent = (attempt - 1) as f64;
        let delay_ms = self.base_delay.as_millis() as f64 * self.backoff_multiplier.powf(exponent);
        let delay = Duration::from_millis(delay_ms as u64);

        delay.min(self.max_delay)
    }
}

/// Trait for determining whether an error should trigger a retry.
pub trait RetryDecider<E> {
    /// Returns `true` if the error should trigger a retry attempt.
    fn should_retry(&self, err: &E) -> bool;
}

/// Default retry decider for gRPC errors wrapped in `sift_error::Error`.
///
/// This decider uses a two-strategy approach:
/// 1. First, attempts to extract `tonic::Status` from the error's source chain
/// 2. Falls back to `ErrorKind`-based heuristics if no `tonic::Status` is found
pub struct DefaultGrpcRetry;

impl RetryDecider<sift_error::Error> for DefaultGrpcRetry {
    fn should_retry(&self, err: &sift_error::Error) -> bool {
        // Strategy 1: Try to extract tonic::Status from error source chain
        let mut source = err.source();
        while let Some(err_ref) = source {
            if let Some(status) = err_ref.downcast_ref::<tonic::Status>() {
                return matches!(
                    status.code(),
                    tonic::Code::Unavailable
                        | tonic::Code::ResourceExhausted
                        | tonic::Code::DeadlineExceeded
                );
            }
            source = err_ref.source();
        }

        // Strategy 2: Fallback to ErrorKind-based heuristics
        matches!(
            err.kind(),
            ErrorKind::GrpcConnectError
                | ErrorKind::RetrieveAssetError
                | ErrorKind::RetrieveIngestionConfigError
                | ErrorKind::RetrieveRunError
        )
    }
}

/// Adapter that wraps a type and provides retry functionality.
#[derive(Clone, Debug)]
pub struct Retrying<T, D = DefaultGrpcRetry> {
    inner: T,
    cfg: RetryConfig,
    decider: D,
}

impl<T> Retrying<T> {
    /// Creates a new `Retrying` adapter with the default gRPC retry decider.
    pub fn new(inner: T, cfg: RetryConfig) -> Self {
        Self {
            inner,
            cfg,
            decider: DefaultGrpcRetry,
        }
    }
}

impl<T, D> Retrying<T, D> {
    /// Replaces the retry decider with a custom one.
    pub fn with_decider<D2>(self, decider: D2) -> Retrying<T, D2> {
        Retrying {
            inner: self.inner,
            cfg: self.cfg,
            decider,
        }
    }

    /// Returns a reference to the inner wrapped value.
    pub fn inner(&self) -> &T {
        &self.inner
    }
}

impl<T, D> Retrying<T, D>
where
    T: Clone,
{
    /// Executes a closure with retry logic.
    ///
    /// The closure is called up to `max_attempts` times. If it returns an error
    /// and the decider indicates the error is retryable, the function waits for
    /// the calculated backoff delay before retrying.
    ///
    /// # Arguments
    ///
    /// * `f` - A closure that takes a cloned wrapper and returns a future
    ///   that produces a `Result`. The closure can use `&mut self` internally
    ///   since each attempt gets a fresh clone.
    ///
    /// # Returns
    ///
    /// Returns `Ok(result)` if any attempt succeeds, or `Err(error)` if all
    /// attempts fail or the error is not retryable.
    pub async fn call<F, Fut, R, E>(&self, mut f: F) -> StdResult<R, E>
    where
        F: FnMut(T) -> Fut,
        Fut: Future<Output = StdResult<R, E>>,
        D: RetryDecider<E>,
    {
        let mut last_err = None;

        for attempt in 1..=self.cfg.max_attempts {
            let wrapper = self.inner.clone();
            match f(wrapper).await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    last_err = Some(e);
                    if attempt < self.cfg.max_attempts
                        && self.decider.should_retry(last_err.as_ref().unwrap())
                    {
                        let delay = self.cfg.backoff(attempt);
                        tokio::time::sleep(delay).await;
                        continue;
                    }
                    break;
                }
            }
        }

        Err(last_err.expect("retry loop invariant violated"))
    }
}

/// Extension trait that provides the `.retrying()` method for any type.
pub trait RetryExt: Sized {
    /// Wraps `self` in a `Retrying` adapter with the given configuration.
    fn retrying(self, cfg: RetryConfig) -> Retrying<Self> {
        Retrying::new(self, cfg)
    }
}

impl<T> RetryExt for T {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    #[test]
    fn test_backoff_calculation() {
        let cfg = RetryConfig::default();

        // First attempt should return base delay
        assert_eq!(cfg.backoff(1), Duration::from_millis(100));

        // Second attempt: 100ms * 2^1 = 200ms
        assert_eq!(cfg.backoff(2), Duration::from_millis(200));

        // Third attempt: 100ms * 2^2 = 400ms
        assert_eq!(cfg.backoff(3), Duration::from_millis(400));

        // Fourth attempt: 100ms * 2^3 = 800ms
        assert_eq!(cfg.backoff(4), Duration::from_millis(800));
    }

    #[test]
    fn test_backoff_caps_at_max() {
        let cfg = RetryConfig {
            max_attempts: 10,
            base_delay: Duration::from_millis(100),
            max_delay: Duration::from_millis(500),
            backoff_multiplier: 2.0,
        };

        // Should cap at max_delay
        let delay = cfg.backoff(10);
        assert_eq!(delay, Duration::from_millis(500));
    }

    #[tokio::test]
    async fn test_retry_loop_succeeds_after_failures() {
        let counter = Arc::new(AtomicUsize::new(0));

        let cfg = RetryConfig {
            max_attempts: 3,
            base_delay: Duration::from_millis(10),
            max_delay: Duration::from_secs(5),
            backoff_multiplier: 2.0,
        };

        let retrying = Retrying::new((), cfg);

        let result = retrying
            .call(|_| {
                let counter = counter.clone();
                async move {
                    let attempts = counter.fetch_add(1, Ordering::SeqCst) + 1;
                    if attempts < 3 {
                        Err::<(), sift_error::Error>(Error::new_msg(
                            ErrorKind::RetrieveAssetError,
                            "temporary failure",
                        ))
                    } else {
                        Ok(())
                    }
                }
            })
            .await;

        assert!(result.is_ok());
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_loop_exhausts_attempts() {
        let counter = Arc::new(AtomicUsize::new(0));

        let cfg = RetryConfig {
            max_attempts: 3,
            base_delay: Duration::from_millis(10),
            max_delay: Duration::from_secs(5),
            backoff_multiplier: 2.0,
        };

        let retrying = Retrying::new((), cfg);

        let result = retrying
            .call(|_| {
                let counter = counter.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    Err::<(), sift_error::Error>(Error::new_msg(
                        ErrorKind::RetrieveAssetError,
                        "persistent failure",
                    ))
                }
            })
            .await;

        assert!(result.is_err());
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[test]
    fn test_default_grpc_retry_with_tonic_status() {
        let decider = DefaultGrpcRetry;

        // Test retryable status codes
        let unavailable = Error::new(
            ErrorKind::RetrieveAssetError,
            tonic::Status::unavailable("service unavailable"),
        );
        assert!(decider.should_retry(&unavailable));

        let resource_exhausted = Error::new(
            ErrorKind::RetrieveAssetError,
            tonic::Status::resource_exhausted("resource exhausted"),
        );
        assert!(decider.should_retry(&resource_exhausted));

        let deadline_exceeded = Error::new(
            ErrorKind::RetrieveAssetError,
            tonic::Status::deadline_exceeded("deadline exceeded"),
        );
        assert!(decider.should_retry(&deadline_exceeded));

        // Test non-retryable status codes
        let invalid_argument = Error::new(
            ErrorKind::ArgumentValidationError,
            tonic::Status::invalid_argument("invalid argument"),
        );
        assert!(!decider.should_retry(&invalid_argument));

        let not_found = Error::new(
            ErrorKind::NotFoundError,
            tonic::Status::not_found("not found"),
        );
        assert!(!decider.should_retry(&not_found));
    }

    #[test]
    fn test_default_grpc_retry_with_error_kind_fallback() {
        let decider = DefaultGrpcRetry;

        // Test retryable error kinds (without tonic::Status)
        let grpc_connect_error = Error::new_msg(ErrorKind::GrpcConnectError, "connection failed");
        assert!(decider.should_retry(&grpc_connect_error));

        let retrieve_asset_error =
            Error::new_msg(ErrorKind::RetrieveAssetError, "retrieval failed");
        assert!(decider.should_retry(&retrieve_asset_error));

        let retrieve_ingestion_config_error =
            Error::new_msg(ErrorKind::RetrieveIngestionConfigError, "retrieval failed");
        assert!(decider.should_retry(&retrieve_ingestion_config_error));

        let retrieve_run_error = Error::new_msg(ErrorKind::RetrieveRunError, "retrieval failed");
        assert!(decider.should_retry(&retrieve_run_error));

        // Test non-retryable error kinds
        let argument_error = Error::new_msg(ErrorKind::ArgumentValidationError, "bad argument");
        assert!(!decider.should_retry(&argument_error));

        let not_found_error = Error::new_msg(ErrorKind::NotFoundError, "not found");
        assert!(!decider.should_retry(&not_found_error));
    }

    #[tokio::test]
    async fn test_no_retry_on_non_retryable_error() {
        let counter = Arc::new(AtomicUsize::new(0));

        let cfg = RetryConfig {
            max_attempts: 3,
            base_delay: Duration::from_millis(10),
            max_delay: Duration::from_secs(5),
            backoff_multiplier: 2.0,
        };

        let retrying = Retrying::new((), cfg);

        let result = retrying
            .call(|_| {
                let counter = counter.clone();
                async move {
                    counter.fetch_add(1, Ordering::SeqCst);
                    // InvalidArgument is not retryable
                    Err::<(), sift_error::Error>(Error::new(
                        ErrorKind::ArgumentValidationError,
                        tonic::Status::invalid_argument("invalid argument"),
                    ))
                }
            })
            .await;

        assert!(result.is_err());
        // Should only attempt once since error is not retryable
        assert_eq!(counter.load(Ordering::SeqCst), 1);
    }
}