rustyschwab 0.2.0

Unofficial Rust SDK for the Charles Schwab Trader and Market Data APIs: OAuth, REST, streaming, journaled orders, risk limits and a paper venue
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
//! Circuit breaker pattern for cascading failure prevention.
//!
//! This module implements a three-state circuit breaker following the standard pattern:
//!
//! ```text
//!                    ┌───────────┐
//!            success │           │ failure threshold
//!       ┌────────────┤  Closed   ├─────────────┐
//!       │            │           │             │
//!       │            └───────────┘             │
//!       │                                      ▼
//!  ┌────┴────┐                           ┌─────────┐
//!  │         │                           │         │
//!  │ Closed  │◄──success threshold───────┤  Open   │
//!  │         │                           │         │
//!  └────┬────┘                           └────┬────┘
//!       │                                     │
//!       │ failure       ┌───────────┐         │ timeout
//!       └──────────────►│ Half-Open │◄────────┘
//!                       └───────────┘
//! ```
//!
//! # States
//!
//! - **Closed**: Normal operation. Requests pass through. Failures are counted.
//! - **Open**: Circuit is tripped. Requests fail immediately without execution.
//! - **Half-Open**: Testing state. Limited requests allowed to probe recovery.
//!
//! # Example
//!
//! ```no_run
//! use rustyschwab::Error;
//! use rustyschwab::circuit_breaker::CircuitBreaker;
//! use rustyschwab::config::CircuitBreakerConfig;
//!
//! # async fn run() {
//! let config = CircuitBreakerConfig::default();
//! let breaker = CircuitBreaker::new(config);
//!
//! // Execute with circuit breaker protection
//! let result = breaker
//!     .call(|| async {
//!         // Your fallible operation here
//!         Ok::<_, Error>(42)
//!     })
//!     .await;
//! # let _ = result;
//! # }
//! ```

use crate::config::CircuitBreakerConfig;
use crate::error::{Error, Result};
use parking_lot::Mutex;
use std::future::Future;
#[cfg(test)]
use std::time::Duration;
use std::time::Instant;

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CircuitState {
    Closed,
    Open,
    HalfOpen,
}

struct State {
    kind: CircuitState,
    generation: u64,
    failures: u32,
    successes: u32,
    probes: u32,
    opened: Option<Instant>,
}

pub struct CircuitBreaker {
    config: CircuitBreakerConfig,
    state: Mutex<State>,
}

// Returning or cancelling the future releases its probe. Old completions
// cannot modify a newer recovery generation.
struct Permit<'a> {
    breaker: &'a CircuitBreaker,
    generation: u64,
    probe: bool,
}
impl Drop for Permit<'_> {
    fn drop(&mut self) {
        let mut s = self.breaker.state.lock();
        if self.probe && s.generation == self.generation {
            s.probes = s.probes.saturating_sub(1);
        }
    }
}

impl std::fmt::Debug for CircuitBreaker {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CircuitBreaker")
            .field("state", &self.state())
            .field("config", &self.config)
            .finish()
    }
}

impl CircuitBreaker {
    pub fn new(config: CircuitBreakerConfig) -> Self {
        Self {
            config,
            state: Mutex::new(State {
                kind: CircuitState::Closed,
                generation: 0,
                failures: 0,
                successes: 0,
                probes: 0,
                opened: None,
            }),
        }
    }
    pub fn state(&self) -> CircuitState {
        self.state.lock().kind
    }
    pub fn failure_count(&self) -> u32 {
        self.state.lock().failures
    }
    fn transition(s: &mut State, next: CircuitState) {
        let from = s.kind;
        s.kind = next;
        s.generation = s.generation.wrapping_add(1);
        s.probes = 0;
        s.successes = 0;
        s.opened = if next == CircuitState::Open {
            Some(Instant::now())
        } else {
            None
        };
        if next == CircuitState::Closed {
            s.failures = 0;
        }
        if from != next {
            record_transition(from, next);
        }
    }
    fn ready(&self, s: &mut State) -> bool {
        if !self.config.enabled {
            return true;
        }
        if s.kind == CircuitState::Open
            && s.opened
                .is_some_and(|t| t.elapsed() >= self.config.open_duration)
        {
            Self::transition(s, CircuitState::HalfOpen);
        }
        s.kind == CircuitState::Closed
            || (s.kind == CircuitState::HalfOpen
                && s.probes < self.config.half_open_max_requests.max(1))
    }
    /// Advisory check; does not consume a probe. `call` atomically reserves it.
    pub fn is_allowed(&self) -> bool {
        self.ready(&mut self.state.lock())
    }
    pub async fn call<F, Fut, T>(&self, f: F) -> Result<T>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<T>>,
    {
        let permit = {
            let mut s = self.state.lock();
            if !self.ready(&mut s) {
                return Err(Error::CircuitOpen);
            }
            let probe = self.config.enabled && s.kind == CircuitState::HalfOpen;
            if probe {
                s.probes += 1;
            }
            Permit {
                breaker: self,
                generation: s.generation,
                probe,
            }
        };
        let result = f().await;
        self.record(permit.generation, responded(&result), unhealthy(&result));
        result
    }
    /// Count a request that was sent and never answered before the caller's
    /// deadline (the deadline drops the call before the breaker sees it).
    pub(crate) fn record_unanswered(&self) {
        let generation = self.state.lock().generation;
        self.record(generation, false, true);
    }
    fn record(&self, generation: u64, success: bool, failure: bool) {
        if !self.config.enabled {
            return;
        }
        let mut s = self.state.lock();
        if s.generation != generation {
            return;
        }
        match s.kind {
            CircuitState::Closed if failure => {
                s.failures += 1;
                if s.failures >= self.config.failure_threshold.max(1) {
                    Self::transition(&mut s, CircuitState::Open);
                }
            }
            CircuitState::Closed if success => s.failures = 0,
            CircuitState::HalfOpen if failure => Self::transition(&mut s, CircuitState::Open),
            CircuitState::HalfOpen if success => {
                s.successes += 1;
                if s.successes >= self.config.success_threshold.max(1) {
                    Self::transition(&mut s, CircuitState::Closed);
                }
            }
            _ => {}
        }
    }
    #[cfg(test)]
    fn record_failure(&self) {
        let g = self.state.lock().generation;
        self.record(g, false, true);
    }
    #[cfg(test)]
    fn record_success(&self) {
        let g = self.state.lock().generation;
        self.record(g, true, false);
    }
    pub fn reset(&self) {
        Self::transition(&mut self.state.lock(), CircuitState::Closed);
    }
}

/// The service answered: a success, or a permanent client error. A 429 is
/// an answer too: the broker is up and pacing this credential, which the
/// request budget and `Retry-After` handle, and tripping the one shared
/// breaker on it would refuse orders because of read traffic.
fn responded<T>(result: &Result<T>) -> bool {
    match result {
        Ok(_) => true,
        Err(Error::RateLimit { .. }) => true,
        Err(e) => e.http_status().is_some_and(|s| s.is_client_error()) && !e.is_retryable(),
    }
}

/// The service may be unhealthy: a transport failure, timeout or 5xx.
/// Local validation and auth failures are neither; the permit still releases.
fn unhealthy<T>(result: &Result<T>) -> bool {
    result
        .as_ref()
        .err()
        .is_some_and(|e| e.is_retryable() && !matches!(e, Error::RateLimit { .. }))
}

/// Emit the transition counter + state gauge. Split out so the
/// transition_to_* methods don't each duplicate the metric glue.
fn record_transition(from: CircuitState, to: CircuitState) {
    metrics::counter!(
        crate::metrics::CIRCUIT_TRANSITIONS_TOTAL,
        "from" => crate::metrics::circuit_state_name(from),
        "to" => crate::metrics::circuit_state_name(to),
    )
    .increment(1);
    metrics::gauge!(crate::metrics::CIRCUIT_STATE).set(crate::metrics::circuit_state_value(to));
}

impl Default for CircuitBreaker {
    fn default() -> Self {
        Self::new(CircuitBreakerConfig::default())
    }
}

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

    #[test]
    fn test_circuit_breaker_starts_closed() {
        let breaker = CircuitBreaker::default();
        assert_eq!(breaker.state(), CircuitState::Closed);
        assert!(breaker.is_allowed());
    }

    #[test]
    fn test_circuit_opens_after_threshold() {
        let config = CircuitBreakerConfig {
            failure_threshold: 3,
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        // Record failures up to threshold
        for _ in 0..3 {
            breaker.record_failure();
        }

        assert_eq!(breaker.state(), CircuitState::Open);
        assert!(!breaker.is_allowed());
    }

    #[test]
    fn test_success_resets_failure_count() {
        let config = CircuitBreakerConfig {
            failure_threshold: 3,
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        // Record some failures
        breaker.record_failure();
        breaker.record_failure();
        assert_eq!(breaker.failure_count(), 2);

        // Success should reset
        breaker.record_success();
        assert_eq!(breaker.failure_count(), 0);
        assert_eq!(breaker.state(), CircuitState::Closed);
    }

    /// Rate limiting proves the broker is answering: however many 429s,
    /// the breaker stays closed, so reads cannot lock out order submission.
    #[tokio::test]
    async fn rate_limits_never_open_the_breaker() {
        let breaker = CircuitBreaker::new(CircuitBreakerConfig {
            failure_threshold: 2,
            ..Default::default()
        });
        for _ in 0..10 {
            let _ = breaker
                .call(|| async { Err::<(), _>(Error::RateLimit { retry_after: 1 }) })
                .await;
        }
        assert_eq!(breaker.state(), CircuitState::Closed);
        for _ in 0..2 {
            let _ = breaker
                .call(|| async { Err::<(), _>(Error::ConnectionClosed) })
                .await;
        }
        assert_eq!(breaker.state(), CircuitState::Open);
    }

    #[test]
    fn test_disabled_breaker_always_allows() {
        let config = CircuitBreakerConfig {
            enabled: false,
            failure_threshold: 1,
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        // Even with failures, should allow
        breaker.record_failure();
        breaker.record_failure();
        assert!(breaker.is_allowed());
    }

    #[test]
    fn test_manual_reset() {
        let config = CircuitBreakerConfig {
            failure_threshold: 2,
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        // Open the circuit
        breaker.record_failure();
        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);

        // Manual reset
        breaker.reset();
        assert_eq!(breaker.state(), CircuitState::Closed);
        assert_eq!(breaker.failure_count(), 0);
        assert!(breaker.is_allowed());
    }

    #[tokio::test]
    async fn test_call_records_success() {
        let breaker = CircuitBreaker::default();

        let result: Result<i32> = breaker.call(|| async { Ok(42) }).await;

        assert!(result.is_ok());
        assert_eq!(result.unwrap(), 42);
        assert_eq!(breaker.failure_count(), 0);
    }

    #[tokio::test]
    async fn test_call_records_failure() {
        let config = CircuitBreakerConfig {
            failure_threshold: 5,
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        let result: Result<i32> = breaker
            .call(|| async { Err(Error::ConnectionClosed) })
            .await;

        assert!(result.is_err());
        assert_eq!(breaker.failure_count(), 1);
    }

    #[tokio::test]
    async fn test_open_circuit_fails_fast() {
        let config = CircuitBreakerConfig {
            failure_threshold: 2,
            open_duration: Duration::from_secs(60), // Long timeout for test
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        // Open the circuit
        breaker.record_failure();
        breaker.record_failure();

        // Should fail immediately without executing
        let result: Result<i32> = breaker.call(|| async { Ok(42) }).await;

        assert!(result.is_err());
        assert!(matches!(result, Err(Error::CircuitOpen)));
    }

    /// Regression: the previous `update_failure_time` implementation
    /// stored `Instant::now().elapsed()` (always ~0), which left the
    /// breaker stuck Open forever. Verify a short `open_duration`
    /// elapses into HalfOpen and allows a probe request through.
    #[tokio::test]
    async fn test_open_transitions_to_half_open_after_timeout() {
        let config = CircuitBreakerConfig {
            failure_threshold: 1,
            open_duration: Duration::from_millis(50),
            ..Default::default()
        };
        let breaker = CircuitBreaker::new(config);

        breaker.record_failure();
        assert_eq!(breaker.state(), CircuitState::Open);
        assert!(!breaker.is_allowed());

        tokio::time::sleep(Duration::from_millis(80)).await;

        // First is_allowed() call after timeout should transition to
        // HalfOpen and allow exactly `half_open_max_requests` probes.
        assert!(
            breaker.is_allowed(),
            "breaker should flip to HalfOpen once open_duration elapses"
        );
        assert_eq!(breaker.state(), CircuitState::HalfOpen);
    }
}