talos-api-rs 0.2.0

A typed, async, idiomatic Rust client for the Talos Linux gRPC 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Retry policies and backoff strategies for resilient API calls.
//!
//! This module provides configurable retry behavior for handling transient failures
//! in gRPC communication with Talos nodes.
//!
//! # Example
//!
//! ```
//! use talos_api_rs::runtime::{RetryConfig, ExponentialBackoff};
//! use std::time::Duration;
//!
//! let retry = RetryConfig::builder()
//!     .max_retries(3)
//!     .backoff(ExponentialBackoff::new(Duration::from_millis(100)))
//!     .build();
//! ```

use std::time::Duration;

/// Defines a backoff strategy for retry delays.
pub trait BackoffStrategy: Clone + Send + Sync + 'static {
    /// Calculate the delay before the next retry attempt.
    ///
    /// # Arguments
    /// * `attempt` - The current attempt number (0-indexed)
    fn delay(&self, attempt: u32) -> Duration;
}

// =============================================================================
// No Backoff
// =============================================================================

/// No delay between retries.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoBackoff;

impl NoBackoff {
    /// Create a new no-backoff strategy.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl BackoffStrategy for NoBackoff {
    fn delay(&self, _attempt: u32) -> Duration {
        Duration::ZERO
    }
}

// =============================================================================
// Fixed Backoff
// =============================================================================

/// Fixed delay between retries.
#[derive(Debug, Clone, Copy)]
pub struct FixedBackoff {
    delay: Duration,
}

impl FixedBackoff {
    /// Create a new fixed backoff strategy.
    #[must_use]
    pub fn new(delay: Duration) -> Self {
        Self { delay }
    }

    /// Create a fixed backoff with delay in milliseconds.
    #[must_use]
    pub fn from_millis(millis: u64) -> Self {
        Self::new(Duration::from_millis(millis))
    }

    /// Create a fixed backoff with delay in seconds.
    #[must_use]
    pub fn from_secs(secs: u64) -> Self {
        Self::new(Duration::from_secs(secs))
    }
}

impl Default for FixedBackoff {
    fn default() -> Self {
        Self::new(Duration::from_millis(100))
    }
}

impl BackoffStrategy for FixedBackoff {
    fn delay(&self, _attempt: u32) -> Duration {
        self.delay
    }
}

// =============================================================================
// Linear Backoff
// =============================================================================

/// Linear backoff - delay increases linearly with each attempt.
#[derive(Debug, Clone, Copy)]
pub struct LinearBackoff {
    initial_delay: Duration,
    increment: Duration,
    max_delay: Duration,
}

impl LinearBackoff {
    /// Create a new linear backoff strategy.
    #[must_use]
    pub fn new(initial_delay: Duration) -> Self {
        Self {
            initial_delay,
            increment: initial_delay,
            max_delay: Duration::from_secs(30),
        }
    }

    /// Set the increment for each retry.
    #[must_use]
    pub fn with_increment(mut self, increment: Duration) -> Self {
        self.increment = increment;
        self
    }

    /// Set the maximum delay cap.
    #[must_use]
    pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
        self.max_delay = max_delay;
        self
    }
}

impl Default for LinearBackoff {
    fn default() -> Self {
        Self::new(Duration::from_millis(100))
    }
}

impl BackoffStrategy for LinearBackoff {
    fn delay(&self, attempt: u32) -> Duration {
        let delay = self.initial_delay + self.increment * attempt;
        delay.min(self.max_delay)
    }
}

// =============================================================================
// Exponential Backoff
// =============================================================================

/// Exponential backoff - delay doubles with each attempt.
///
/// Optionally includes jitter to prevent thundering herd.
#[derive(Debug, Clone, Copy)]
pub struct ExponentialBackoff {
    initial_delay: Duration,
    max_delay: Duration,
    multiplier: f64,
    jitter: bool,
}

impl ExponentialBackoff {
    /// Create a new exponential backoff strategy.
    #[must_use]
    pub fn new(initial_delay: Duration) -> Self {
        Self {
            initial_delay,
            max_delay: Duration::from_secs(30),
            multiplier: 2.0,
            jitter: true,
        }
    }

    /// Set the maximum delay cap.
    #[must_use]
    pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
        self.max_delay = max_delay;
        self
    }

    /// Set the multiplier for exponential growth.
    #[must_use]
    pub fn with_multiplier(mut self, multiplier: f64) -> Self {
        self.multiplier = multiplier;
        self
    }

    /// Enable or disable jitter.
    #[must_use]
    pub fn with_jitter(mut self, jitter: bool) -> Self {
        self.jitter = jitter;
        self
    }
}

impl Default for ExponentialBackoff {
    fn default() -> Self {
        Self::new(Duration::from_millis(100))
    }
}

impl BackoffStrategy for ExponentialBackoff {
    fn delay(&self, attempt: u32) -> Duration {
        let base_delay =
            self.initial_delay.as_millis() as f64 * self.multiplier.powi(attempt as i32);
        let capped_delay = base_delay.min(self.max_delay.as_millis() as f64);

        let final_delay = if self.jitter {
            // Add up to 25% jitter
            let jitter_range = capped_delay * 0.25;
            // Simple deterministic jitter based on attempt number
            let jitter = (attempt as f64 * 0.1).sin().abs() * jitter_range;
            capped_delay + jitter
        } else {
            capped_delay
        };

        Duration::from_millis(final_delay as u64)
    }
}

// =============================================================================
// Retry Policy
// =============================================================================

/// Determines whether a gRPC error should be retried.
pub trait RetryPolicy: Clone + Send + Sync + 'static {
    /// Returns `true` if the operation should be retried for this error.
    fn should_retry(&self, code: tonic::Code) -> bool;
}

/// Default retry policy - retries on transient errors.
#[derive(Debug, Clone, Copy, Default)]
pub struct DefaultRetryPolicy;

impl RetryPolicy for DefaultRetryPolicy {
    fn should_retry(&self, code: tonic::Code) -> bool {
        matches!(
            code,
            tonic::Code::Unavailable
                | tonic::Code::Unknown
                | tonic::Code::DeadlineExceeded
                | tonic::Code::ResourceExhausted
                | tonic::Code::Aborted
        )
    }
}

/// Never retry - fail immediately.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoRetryPolicy;

impl RetryPolicy for NoRetryPolicy {
    fn should_retry(&self, _code: tonic::Code) -> bool {
        false
    }
}

/// Custom retry policy based on a list of codes.
#[derive(Debug, Clone)]
pub struct CustomRetryPolicy {
    retry_codes: Vec<tonic::Code>,
}

impl CustomRetryPolicy {
    /// Create a policy that retries on specific codes.
    #[must_use]
    pub fn new(retry_codes: Vec<tonic::Code>) -> Self {
        Self { retry_codes }
    }

    /// Create a policy for network-level errors only.
    #[must_use]
    pub fn network_errors() -> Self {
        Self::new(vec![tonic::Code::Unavailable, tonic::Code::Unknown])
    }
}

impl RetryPolicy for CustomRetryPolicy {
    fn should_retry(&self, code: tonic::Code) -> bool {
        self.retry_codes.contains(&code)
    }
}

// =============================================================================
// Retry Configuration
// =============================================================================

/// Complete retry configuration combining policy and backoff.
#[derive(Debug, Clone)]
pub struct RetryConfig<P: RetryPolicy = DefaultRetryPolicy, B: BackoffStrategy = ExponentialBackoff>
{
    /// Maximum number of retry attempts.
    pub max_retries: u32,
    /// Policy determining which errors to retry.
    pub policy: P,
    /// Backoff strategy for calculating delays.
    pub backoff: B,
    /// Maximum total time for all retries.
    pub total_timeout: Option<Duration>,
}

impl Default for RetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            policy: DefaultRetryPolicy,
            backoff: ExponentialBackoff::default(),
            total_timeout: Some(Duration::from_secs(30)),
        }
    }
}

impl RetryConfig {
    /// Create a new retry configuration with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a configuration builder.
    #[must_use]
    pub fn builder() -> RetryConfigBuilder<DefaultRetryPolicy, ExponentialBackoff> {
        RetryConfigBuilder::new()
    }

    /// Disable retries.
    #[must_use]
    pub fn disabled() -> RetryConfig<NoRetryPolicy, NoBackoff> {
        RetryConfig {
            max_retries: 0,
            policy: NoRetryPolicy,
            backoff: NoBackoff,
            total_timeout: None,
        }
    }
}

impl<P: RetryPolicy, B: BackoffStrategy> RetryConfig<P, B> {
    /// Execute an async operation with retry logic.
    pub async fn execute<T, E, F, Fut>(&self, mut operation: F) -> Result<T, E>
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = Result<T, E>>,
        E: AsGrpcStatus,
    {
        let start = std::time::Instant::now();
        let mut attempt = 0;

        loop {
            match operation().await {
                Ok(result) => return Ok(result),
                Err(e) => {
                    let code = e.grpc_code();

                    // Check if we should retry
                    if !self.policy.should_retry(code) {
                        return Err(e);
                    }

                    // Check if we've exceeded max retries
                    if attempt >= self.max_retries {
                        return Err(e);
                    }

                    // Check total timeout
                    if let Some(timeout) = self.total_timeout {
                        if start.elapsed() >= timeout {
                            return Err(e);
                        }
                    }

                    // Calculate delay and sleep
                    let delay = self.backoff.delay(attempt);
                    tokio::time::sleep(delay).await;

                    attempt += 1;
                }
            }
        }
    }
}

/// Builder for `RetryConfig`.
#[derive(Debug, Clone)]
pub struct RetryConfigBuilder<P: RetryPolicy, B: BackoffStrategy> {
    max_retries: u32,
    policy: P,
    backoff: B,
    total_timeout: Option<Duration>,
}

impl RetryConfigBuilder<DefaultRetryPolicy, ExponentialBackoff> {
    /// Create a new builder with defaults.
    #[must_use]
    pub fn new() -> Self {
        Self {
            max_retries: 3,
            policy: DefaultRetryPolicy,
            backoff: ExponentialBackoff::default(),
            total_timeout: Some(Duration::from_secs(30)),
        }
    }
}

impl Default for RetryConfigBuilder<DefaultRetryPolicy, ExponentialBackoff> {
    fn default() -> Self {
        Self::new()
    }
}

impl<P: RetryPolicy, B: BackoffStrategy> RetryConfigBuilder<P, B> {
    /// Set maximum retry attempts.
    #[must_use]
    pub fn max_retries(mut self, max: u32) -> Self {
        self.max_retries = max;
        self
    }

    /// Set the retry policy.
    #[must_use]
    pub fn policy<P2: RetryPolicy>(self, policy: P2) -> RetryConfigBuilder<P2, B> {
        RetryConfigBuilder {
            max_retries: self.max_retries,
            policy,
            backoff: self.backoff,
            total_timeout: self.total_timeout,
        }
    }

    /// Set the backoff strategy.
    #[must_use]
    pub fn backoff<B2: BackoffStrategy>(self, backoff: B2) -> RetryConfigBuilder<P, B2> {
        RetryConfigBuilder {
            max_retries: self.max_retries,
            policy: self.policy,
            backoff,
            total_timeout: self.total_timeout,
        }
    }

    /// Set the total timeout for all retries.
    #[must_use]
    pub fn total_timeout(mut self, timeout: Duration) -> Self {
        self.total_timeout = Some(timeout);
        self
    }

    /// Disable total timeout.
    #[must_use]
    pub fn no_total_timeout(mut self) -> Self {
        self.total_timeout = None;
        self
    }

    /// Build the configuration.
    #[must_use]
    pub fn build(self) -> RetryConfig<P, B> {
        RetryConfig {
            max_retries: self.max_retries,
            policy: self.policy,
            backoff: self.backoff,
            total_timeout: self.total_timeout,
        }
    }
}

/// Trait for extracting gRPC status codes from errors.
pub trait AsGrpcStatus {
    /// Extract the gRPC status code.
    fn grpc_code(&self) -> tonic::Code;
}

impl AsGrpcStatus for tonic::Status {
    fn grpc_code(&self) -> tonic::Code {
        self.code()
    }
}

impl<T> AsGrpcStatus for Result<T, tonic::Status> {
    fn grpc_code(&self) -> tonic::Code {
        match self {
            Ok(_) => tonic::Code::Ok,
            Err(e) => e.code(),
        }
    }
}

// Implement for our error type
impl AsGrpcStatus for crate::error::TalosError {
    fn grpc_code(&self) -> tonic::Code {
        match self {
            crate::error::TalosError::Api(status) => status.code(),
            crate::error::TalosError::Transport(_) => tonic::Code::Unavailable,
            crate::error::TalosError::Config(_) => tonic::Code::InvalidArgument,
            crate::error::TalosError::Validation(_) => tonic::Code::InvalidArgument,
            crate::error::TalosError::Connection(_) => tonic::Code::Unavailable,
            crate::error::TalosError::CircuitOpen(_) => tonic::Code::Unavailable,
            crate::error::TalosError::Unknown(_) => tonic::Code::Internal,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_no_backoff() {
        let backoff = NoBackoff::new();
        assert_eq!(backoff.delay(0), Duration::ZERO);
        assert_eq!(backoff.delay(5), Duration::ZERO);
        assert_eq!(backoff.delay(100), Duration::ZERO);
    }

    #[test]
    fn test_fixed_backoff() {
        let backoff = FixedBackoff::from_millis(100);
        assert_eq!(backoff.delay(0), Duration::from_millis(100));
        assert_eq!(backoff.delay(5), Duration::from_millis(100));
        assert_eq!(backoff.delay(100), Duration::from_millis(100));
    }

    #[test]
    fn test_linear_backoff() {
        let backoff = LinearBackoff::new(Duration::from_millis(100))
            .with_increment(Duration::from_millis(50))
            .with_max_delay(Duration::from_millis(500));

        assert_eq!(backoff.delay(0), Duration::from_millis(100));
        assert_eq!(backoff.delay(1), Duration::from_millis(150));
        assert_eq!(backoff.delay(2), Duration::from_millis(200));
        assert_eq!(backoff.delay(10), Duration::from_millis(500)); // Capped
    }

    #[test]
    fn test_exponential_backoff() {
        let backoff = ExponentialBackoff::new(Duration::from_millis(100))
            .with_max_delay(Duration::from_secs(10))
            .with_jitter(false);

        assert_eq!(backoff.delay(0), Duration::from_millis(100));
        assert_eq!(backoff.delay(1), Duration::from_millis(200));
        assert_eq!(backoff.delay(2), Duration::from_millis(400));
        assert_eq!(backoff.delay(3), Duration::from_millis(800));
    }

    #[test]
    fn test_exponential_backoff_cap() {
        let backoff = ExponentialBackoff::new(Duration::from_millis(100))
            .with_max_delay(Duration::from_millis(500))
            .with_jitter(false);

        assert_eq!(backoff.delay(5), Duration::from_millis(500)); // Capped at 500ms
    }

    #[test]
    fn test_default_retry_policy() {
        let policy = DefaultRetryPolicy;

        assert!(policy.should_retry(tonic::Code::Unavailable));
        assert!(policy.should_retry(tonic::Code::DeadlineExceeded));
        assert!(policy.should_retry(tonic::Code::ResourceExhausted));
        assert!(policy.should_retry(tonic::Code::Aborted));

        assert!(!policy.should_retry(tonic::Code::InvalidArgument));
        assert!(!policy.should_retry(tonic::Code::NotFound));
        assert!(!policy.should_retry(tonic::Code::PermissionDenied));
        assert!(!policy.should_retry(tonic::Code::AlreadyExists));
    }

    #[test]
    fn test_no_retry_policy() {
        let policy = NoRetryPolicy;

        assert!(!policy.should_retry(tonic::Code::Unavailable));
        assert!(!policy.should_retry(tonic::Code::Unknown));
    }

    #[test]
    fn test_custom_retry_policy() {
        let policy = CustomRetryPolicy::network_errors();

        assert!(policy.should_retry(tonic::Code::Unavailable));
        assert!(policy.should_retry(tonic::Code::Unknown));
        assert!(!policy.should_retry(tonic::Code::DeadlineExceeded));
    }

    #[test]
    fn test_retry_config_builder() {
        let config = RetryConfig::builder()
            .max_retries(5)
            .backoff(FixedBackoff::from_millis(200))
            .total_timeout(Duration::from_secs(60))
            .build();

        assert_eq!(config.max_retries, 5);
        assert_eq!(config.total_timeout, Some(Duration::from_secs(60)));
    }

    #[test]
    fn test_retry_config_disabled() {
        let config = RetryConfig::disabled();

        assert_eq!(config.max_retries, 0);
        assert_eq!(config.total_timeout, None);
    }

    #[tokio::test]
    async fn test_retry_execute_success() {
        let config = RetryConfig::default();

        let result: Result<i32, tonic::Status> = config.execute(|| async { Ok(42) }).await;

        assert_eq!(result.unwrap(), 42);
    }

    #[tokio::test]
    async fn test_retry_execute_transient_failure() {
        let config = RetryConfig::builder()
            .max_retries(3)
            .backoff(NoBackoff::new())
            .build();

        let call_count = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
        let call_count_clone = call_count.clone();

        let result: Result<i32, tonic::Status> = config
            .execute(|| {
                let count = call_count_clone.clone();
                async move {
                    let n = count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                    if n < 2 {
                        Err(tonic::Status::unavailable("transient"))
                    } else {
                        Ok(42)
                    }
                }
            })
            .await;

        assert_eq!(result.unwrap(), 42);
        assert_eq!(call_count.load(std::sync::atomic::Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn test_retry_execute_permanent_failure() {
        let config = RetryConfig::builder()
            .max_retries(3)
            .backoff(NoBackoff::new())
            .build();

        let result: Result<i32, tonic::Status> = config
            .execute(|| async { Err(tonic::Status::invalid_argument("bad input")) })
            .await;

        assert!(result.is_err());
        assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
    }
}