dactor 0.3.3

An abstract framework for distributed actors in Rust
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
//! Circuit breaker interceptor for per-actor fault isolation.
//!
//! Tracks error rates per actor and trips the circuit when the error count
//! within a sliding time window exceeds a configurable threshold.
//!
//! # States
//!
//! - **Closed** — normal operation, messages flow through.
//! - **Open** — too many errors; all messages are rejected immediately.
//! - **HalfOpen** — cooldown elapsed; one probe message is allowed through
//!   to test whether the actor has recovered.

use std::any::Any;
use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;
use std::time::{Duration, Instant};

use crate::interceptor::*;
use crate::message::{Headers, RuntimeHeaders};
use crate::node::ActorId;

/// The three states of a circuit breaker.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CircuitState {
    /// Normal — messages flow through.
    Closed,
    /// Tripped — messages are rejected immediately.
    Open,
    /// Testing — one probe message is allowed through.
    HalfOpen,
}

/// Per-actor bookkeeping for the circuit breaker.
struct CircuitBreakerState {
    state: CircuitState,
    /// Timestamps of recent errors (within the error window).
    error_timestamps: VecDeque<Instant>,
    /// When the circuit was last opened. Used to determine cooldown expiry.
    opened_at: Option<Instant>,
    /// Whether a probe message is currently in flight (HalfOpen).
    probe_in_flight: bool,
}

impl CircuitBreakerState {
    fn new() -> Self {
        Self {
            state: CircuitState::Closed,
            error_timestamps: VecDeque::new(),
            opened_at: None,
            probe_in_flight: false,
        }
    }

    /// Remove error timestamps older than the window.
    fn prune_errors(&mut self, window: Duration, now: Instant) {
        while let Some(&front) = self.error_timestamps.front() {
            if now.duration_since(front) > window {
                self.error_timestamps.pop_front();
            } else {
                break;
            }
        }
    }
}

/// Inbound interceptor that implements the circuit breaker pattern.
///
/// When an actor's error count within a sliding time window exceeds
/// `error_threshold`, the circuit opens and all subsequent messages are
/// rejected. After `cooldown` elapses the circuit enters half-open state,
/// allowing a single probe message through. If the probe succeeds the
/// circuit closes; if it fails the circuit re-opens.
///
/// # Example
///
/// ```rust
/// use std::time::Duration;
/// use dactor::circuit_breaker::CircuitBreakerInterceptor;
///
/// let cb = CircuitBreakerInterceptor::new(
///     5,                          // trip after 5 errors
///     Duration::from_secs(60),    // within a 60-second window
///     Duration::from_secs(30),    // stay open for 30 seconds
/// );
/// ```
pub struct CircuitBreakerInterceptor {
    /// Number of errors within the window required to trip the circuit.
    error_threshold: u32,
    /// Sliding window for counting errors.
    error_window: Duration,
    /// How long the circuit stays open before transitioning to half-open.
    cooldown: Duration,
    /// Per-actor circuit state.
    states: Mutex<HashMap<ActorId, CircuitBreakerState>>,
}

impl CircuitBreakerInterceptor {
    /// Create a new circuit breaker interceptor.
    ///
    /// * `error_threshold` — number of errors within `error_window` to trip.
    /// * `error_window`    — sliding window for error counting.
    /// * `cooldown`        — duration the circuit stays open before half-open.
    pub fn new(error_threshold: u32, error_window: Duration, cooldown: Duration) -> Self {
        Self {
            error_threshold,
            error_window,
            cooldown,
            states: Mutex::new(HashMap::new()),
        }
    }

    /// Query the current circuit state for an actor.
    /// Returns `Closed` if the actor has no recorded state.
    pub fn state_for(&self, actor_id: &ActorId) -> CircuitState {
        let states = self.states.lock().unwrap();
        states
            .get(actor_id)
            .map_or(CircuitState::Closed, |s| s.state)
    }
}

impl InboundInterceptor for CircuitBreakerInterceptor {
    fn name(&self) -> &'static str {
        "circuit-breaker"
    }

    fn on_receive(
        &self,
        ctx: &InboundContext<'_>,
        _runtime_headers: &RuntimeHeaders,
        _headers: &mut Headers,
        _message: &dyn Any,
    ) -> Disposition {
        let mut states = self.states.lock().unwrap();
        let now = Instant::now();
        let entry = states
            .entry(ctx.actor_id.clone())
            .or_insert_with(CircuitBreakerState::new);

        match entry.state {
            CircuitState::Closed => Disposition::Continue,
            CircuitState::Open => {
                // Check whether cooldown has elapsed → transition to HalfOpen.
                if let Some(opened_at) = entry.opened_at {
                    if now.duration_since(opened_at) >= self.cooldown {
                        entry.state = CircuitState::HalfOpen;
                        entry.probe_in_flight = true;
                        return Disposition::Continue;
                    }
                }
                Disposition::Reject(format!("circuit breaker open: {}", ctx.actor_name))
            }
            CircuitState::HalfOpen => {
                if entry.probe_in_flight {
                    // A probe is already in flight; reject additional messages.
                    Disposition::Reject(format!("circuit breaker open: {}", ctx.actor_name))
                } else {
                    // Allow the probe message.
                    entry.probe_in_flight = true;
                    Disposition::Continue
                }
            }
        }
    }

    fn on_complete(
        &self,
        ctx: &InboundContext<'_>,
        _runtime_headers: &RuntimeHeaders,
        _headers: &Headers,
        outcome: &Outcome<'_>,
    ) {
        let is_error = matches!(outcome, Outcome::HandlerError { .. });

        let mut states = self.states.lock().unwrap();
        let now = Instant::now();
        let entry = states
            .entry(ctx.actor_id.clone())
            .or_insert_with(CircuitBreakerState::new);

        match entry.state {
            CircuitState::Closed => {
                if is_error {
                    entry.error_timestamps.push_back(now);
                    entry.prune_errors(self.error_window, now);

                    if entry.error_timestamps.len() as u32 >= self.error_threshold {
                        entry.state = CircuitState::Open;
                        entry.opened_at = Some(now);
                    }
                }
            }
            CircuitState::HalfOpen => {
                entry.probe_in_flight = false;
                if is_error {
                    // Probe failed — re-open.
                    entry.state = CircuitState::Open;
                    entry.opened_at = Some(now);
                    entry.error_timestamps.push_back(now);
                    entry.prune_errors(self.error_window, now);
                } else {
                    // Probe succeeded — close and reset errors.
                    entry.state = CircuitState::Closed;
                    entry.error_timestamps.clear();
                    entry.opened_at = None;
                }
            }
            CircuitState::Open => {
                // Shouldn't normally receive on_complete while open,
                // but handle gracefully by recording errors if any.
                if is_error {
                    entry.error_timestamps.push_back(now);
                    entry.prune_errors(self.error_window, now);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::actor::ActorError;
    use crate::node::NodeId;

    fn make_actor_id(local: u64) -> ActorId {
        ActorId {
            node: NodeId("test-node".into()),
            local,
        }
    }

    fn make_ctx<'a>(actor_id: &'a ActorId, name: &'a str) -> InboundContext<'a> {
        InboundContext {
            actor_id: actor_id.clone(),
            actor_name: name,
            message_type: "TestMsg",
            send_mode: SendMode::Tell,
            remote: false,
            origin_node: None,
        }
    }

    /// Helper: send on_receive + on_complete(error) for the given interceptor.
    fn send_error(cb: &CircuitBreakerInterceptor, actor_id: &ActorId, name: &str) -> Disposition {
        let ctx = make_ctx(actor_id, name);
        let rh = RuntimeHeaders::new();
        let mut headers = Headers::new();
        let msg = 0u64;

        let d = cb.on_receive(&ctx, &rh, &mut headers, &msg);
        if matches!(d, Disposition::Continue) {
            cb.on_complete(
                &ctx,
                &rh,
                &headers,
                &Outcome::HandlerError {
                    error: ActorError::internal("boom"),
                },
            );
        }
        d
    }

    /// Helper: send on_receive + on_complete(success).
    fn send_success(cb: &CircuitBreakerInterceptor, actor_id: &ActorId, name: &str) -> Disposition {
        let ctx = make_ctx(actor_id, name);
        let rh = RuntimeHeaders::new();
        let mut headers = Headers::new();
        let msg = 0u64;

        let d = cb.on_receive(&ctx, &rh, &mut headers, &msg);
        if matches!(d, Disposition::Continue) {
            cb.on_complete(&ctx, &rh, &headers, &Outcome::TellSuccess);
        }
        d
    }

    #[test]
    fn messages_flow_normally_under_threshold() {
        let cb =
            CircuitBreakerInterceptor::new(5, Duration::from_secs(60), Duration::from_secs(30));
        let id = make_actor_id(1);

        // 4 errors (threshold = 5) — circuit stays closed
        for _ in 0..4 {
            let d = send_error(&cb, &id, "actor-a");
            assert!(matches!(d, Disposition::Continue));
        }
        assert_eq!(cb.state_for(&id), CircuitState::Closed);

        // Successes don't count toward the threshold
        let d = send_success(&cb, &id, "actor-a");
        assert!(matches!(d, Disposition::Continue));
        assert_eq!(cb.state_for(&id), CircuitState::Closed);
    }

    #[test]
    fn circuit_opens_after_n_errors() {
        let cb =
            CircuitBreakerInterceptor::new(3, Duration::from_secs(60), Duration::from_secs(30));
        let id = make_actor_id(1);

        for _ in 0..3 {
            send_error(&cb, &id, "actor-a");
        }
        assert_eq!(cb.state_for(&id), CircuitState::Open);
    }

    #[test]
    fn messages_rejected_when_circuit_is_open() {
        let cb =
            CircuitBreakerInterceptor::new(2, Duration::from_secs(60), Duration::from_secs(30));
        let id = make_actor_id(1);

        // Trip the circuit
        for _ in 0..2 {
            send_error(&cb, &id, "actor-a");
        }
        assert_eq!(cb.state_for(&id), CircuitState::Open);

        // Subsequent messages should be rejected
        let ctx = make_ctx(&id, "actor-a");
        let rh = RuntimeHeaders::new();
        let mut headers = Headers::new();
        let msg = 0u64;
        let d = cb.on_receive(&ctx, &rh, &mut headers, &msg);
        match d {
            Disposition::Reject(reason) => {
                assert!(reason.contains("circuit breaker open"));
                assert!(reason.contains("actor-a"));
            }
            other => panic!("expected Reject, got {:?}", other),
        }
    }

    #[test]
    fn circuit_transitions_to_half_open_after_cooldown() {
        // Use a tiny cooldown so the test is fast.
        let cb =
            CircuitBreakerInterceptor::new(2, Duration::from_secs(60), Duration::from_millis(1));
        let id = make_actor_id(1);

        // Trip the circuit
        for _ in 0..2 {
            send_error(&cb, &id, "actor-a");
        }
        assert_eq!(cb.state_for(&id), CircuitState::Open);

        // Wait for cooldown
        std::thread::sleep(Duration::from_millis(5));

        // Next on_receive should transition to HalfOpen and allow the probe
        let ctx = make_ctx(&id, "actor-a");
        let rh = RuntimeHeaders::new();
        let mut headers = Headers::new();
        let msg = 0u64;
        let d = cb.on_receive(&ctx, &rh, &mut headers, &msg);
        assert!(matches!(d, Disposition::Continue));
        assert_eq!(cb.state_for(&id), CircuitState::HalfOpen);
    }

    #[test]
    fn successful_probe_closes_circuit() {
        let cb =
            CircuitBreakerInterceptor::new(2, Duration::from_secs(60), Duration::from_millis(1));
        let id = make_actor_id(1);

        // Trip the circuit
        for _ in 0..2 {
            send_error(&cb, &id, "actor-a");
        }
        assert_eq!(cb.state_for(&id), CircuitState::Open);

        // Wait for cooldown
        std::thread::sleep(Duration::from_millis(5));

        // Probe succeeds
        let d = send_success(&cb, &id, "actor-a");
        assert!(matches!(d, Disposition::Continue));
        assert_eq!(cb.state_for(&id), CircuitState::Closed);
    }

    #[test]
    fn failed_probe_reopens_circuit() {
        let cb =
            CircuitBreakerInterceptor::new(2, Duration::from_secs(60), Duration::from_millis(1));
        let id = make_actor_id(1);

        // Trip the circuit
        for _ in 0..2 {
            send_error(&cb, &id, "actor-a");
        }
        assert_eq!(cb.state_for(&id), CircuitState::Open);

        // Wait for cooldown
        std::thread::sleep(Duration::from_millis(5));

        // Probe fails
        let d = send_error(&cb, &id, "actor-a");
        assert!(matches!(d, Disposition::Continue)); // probe was allowed through
        assert_eq!(cb.state_for(&id), CircuitState::Open); // re-opened
    }

    #[test]
    fn errors_outside_window_are_pruned() {
        // Window of 1 ms, threshold of 3, cooldown irrelevant
        let cb =
            CircuitBreakerInterceptor::new(3, Duration::from_millis(1), Duration::from_secs(30));
        let id = make_actor_id(1);

        // Record 2 errors
        send_error(&cb, &id, "actor-a");
        send_error(&cb, &id, "actor-a");

        // Wait for the window to expire
        std::thread::sleep(Duration::from_millis(5));

        // One more error shouldn't trip the circuit (old errors pruned)
        send_error(&cb, &id, "actor-a");
        assert_eq!(cb.state_for(&id), CircuitState::Closed);
    }

    #[test]
    fn independent_per_actor_state() {
        let cb =
            CircuitBreakerInterceptor::new(2, Duration::from_secs(60), Duration::from_secs(30));
        let id1 = make_actor_id(1);
        let id2 = make_actor_id(2);

        // Trip actor 1
        for _ in 0..2 {
            send_error(&cb, &id1, "actor-1");
        }
        assert_eq!(cb.state_for(&id1), CircuitState::Open);

        // Actor 2 should still be closed
        assert_eq!(cb.state_for(&id2), CircuitState::Closed);
        let d = send_success(&cb, &id2, "actor-2");
        assert!(matches!(d, Disposition::Continue));
    }

    #[test]
    fn half_open_rejects_extra_messages_while_probe_in_flight() {
        let cb =
            CircuitBreakerInterceptor::new(2, Duration::from_secs(60), Duration::from_millis(1));
        let id = make_actor_id(1);

        // Trip the circuit
        for _ in 0..2 {
            send_error(&cb, &id, "actor-a");
        }

        // Wait for cooldown
        std::thread::sleep(Duration::from_millis(5));

        // First message → probe (allowed)
        let ctx = make_ctx(&id, "actor-a");
        let rh = RuntimeHeaders::new();
        let mut headers = Headers::new();
        let msg = 0u64;
        let d = cb.on_receive(&ctx, &rh, &mut headers, &msg);
        assert!(matches!(d, Disposition::Continue));

        // Second message while probe is in flight → rejected
        let d2 = cb.on_receive(&ctx, &rh, &mut headers, &msg);
        assert!(matches!(d2, Disposition::Reject(_)));
    }
}