redisctl_config/
resilience.rs

1//! Resilience configuration for API clients
2//!
3//! This module defines configuration structures for resilience patterns
4//! (circuit breaker, retry, rate limiting) that can be stored in profiles.
5
6use serde::{Deserialize, Serialize};
7
8/// Configuration for resilience patterns
9#[derive(Debug, Clone, Default, Serialize, Deserialize)]
10pub struct ResilienceConfig {
11    /// Circuit breaker configuration
12    #[serde(default)]
13    pub circuit_breaker: CircuitBreakerConfig,
14
15    /// Retry configuration
16    #[serde(default)]
17    pub retry: RetryConfig,
18
19    /// Rate limiting configuration
20    #[serde(default)]
21    pub rate_limit: RateLimitConfig,
22}
23
24/// Circuit breaker configuration
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct CircuitBreakerConfig {
27    /// Whether circuit breaker is enabled
28    #[serde(default = "default_true")]
29    pub enabled: bool,
30
31    /// Failure rate threshold (0.0 to 1.0) to open the circuit
32    #[serde(default = "default_failure_threshold")]
33    pub failure_threshold: f32,
34
35    /// Number of calls to track in the sliding window
36    #[serde(default = "default_window_size")]
37    pub window_size: u32,
38
39    /// Duration in seconds to wait before attempting to close the circuit
40    #[serde(default = "default_reset_timeout")]
41    pub reset_timeout_secs: u64,
42}
43
44impl Default for CircuitBreakerConfig {
45    fn default() -> Self {
46        Self {
47            enabled: true,
48            failure_threshold: 0.5,
49            window_size: 20,
50            reset_timeout_secs: 60,
51        }
52    }
53}
54
55/// Retry configuration
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct RetryConfig {
58    /// Whether retry is enabled
59    #[serde(default = "default_true")]
60    pub enabled: bool,
61
62    /// Maximum number of retry attempts
63    #[serde(default = "default_max_attempts")]
64    pub max_attempts: u32,
65
66    /// Initial backoff in milliseconds
67    #[serde(default = "default_backoff_ms")]
68    pub backoff_ms: u64,
69
70    /// Maximum backoff in milliseconds
71    #[serde(default = "default_max_backoff_ms")]
72    pub max_backoff_ms: u64,
73}
74
75impl Default for RetryConfig {
76    fn default() -> Self {
77        Self {
78            enabled: true,
79            max_attempts: 3,
80            backoff_ms: 100,
81            max_backoff_ms: 5000,
82        }
83    }
84}
85
86/// Rate limiting configuration
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct RateLimitConfig {
89    /// Whether rate limiting is enabled
90    #[serde(default)]
91    pub enabled: bool,
92
93    /// Maximum requests per minute
94    #[serde(default = "default_requests_per_minute")]
95    pub requests_per_minute: u32,
96}
97
98impl Default for RateLimitConfig {
99    fn default() -> Self {
100        Self {
101            enabled: false,
102            requests_per_minute: 100,
103        }
104    }
105}
106
107// Default value functions for serde
108fn default_true() -> bool {
109    true
110}
111
112fn default_failure_threshold() -> f32 {
113    0.5
114}
115
116fn default_window_size() -> u32 {
117    20
118}
119
120fn default_reset_timeout() -> u64 {
121    60
122}
123
124fn default_max_attempts() -> u32 {
125    3
126}
127
128fn default_backoff_ms() -> u64 {
129    100
130}
131
132fn default_max_backoff_ms() -> u64 {
133    5000
134}
135
136fn default_requests_per_minute() -> u32 {
137    100
138}