lazily 0.48.1

Lazy reactive signals with dependency tracking and cache invalidation
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
//! Phase 6 of the realtime + distributed primitives plan — `#lzresilience`
//! fault-tolerance primitives.
//!
//! See `lazily-spec/docs/resilience.md` and the formal model
//! `lazily-formal/LazilyFormal/Resilience.lean`. Circuit breaker / retry /
//! bulkhead / timeout, each a pure compute **core** (a state machine / counter
//! over the logical clock — `BytesPayload`) split from a reactive **cell**
//! projecting the salient reader. Composes with `CommandTransport` /
//! `CommandPolicy` so RPCs degrade gracefully.

use std::cell::RefCell;
use std::collections::VecDeque;

use crate::Context;
use crate::cell::Source;

// ===========================================================================
// Circuit breaker
// ===========================================================================

/// Circuit-breaker state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BreakerState {
    /// Calls pass; failures accumulate in the window.
    Closed,
    /// Fast-fail until the reset deadline.
    Open,
    /// Allow a single probe.
    HalfOpen,
}

/// Circuit-breaker compute core: a sliding window of outcomes trips
/// `Closed → Open` at `failure_threshold`; `Open → HalfOpen` at the deadline; a
/// HalfOpen success closes, a failure re-opens.
pub struct CircuitBreakerCore {
    window: usize,
    failure_threshold: usize,
    reset_timeout: u64,
    state: BreakerState,
    outcomes: VecDeque<bool>, // true = success
    open_until: u64,
}

impl CircuitBreakerCore {
    pub fn new(window: usize, failure_threshold: usize, reset_timeout: u64) -> Self {
        Self {
            window: window.max(1),
            failure_threshold: failure_threshold.max(1),
            reset_timeout,
            state: BreakerState::Closed,
            outcomes: VecDeque::new(),
            open_until: 0,
        }
    }

    pub fn state(&self) -> BreakerState {
        self.state
    }

    fn failures(&self) -> usize {
        self.outcomes.iter().filter(|s| !**s).count()
    }

    /// Whether a call is permitted; performs the `Open → HalfOpen` transition at
    /// the deadline.
    pub fn allow(&mut self, now: u64) -> bool {
        match self.state {
            BreakerState::Closed => true,
            BreakerState::Open => {
                if now >= self.open_until {
                    self.state = BreakerState::HalfOpen;
                    true
                } else {
                    false
                }
            }
            BreakerState::HalfOpen => true,
        }
    }

    /// Feed a call outcome and drive the state machine.
    pub fn record(&mut self, success: bool, now: u64) {
        match self.state {
            BreakerState::HalfOpen => {
                if success {
                    self.state = BreakerState::Closed;
                    self.outcomes.clear();
                } else {
                    self.state = BreakerState::Open;
                    self.open_until = now + self.reset_timeout;
                }
            }
            BreakerState::Closed => {
                self.outcomes.push_back(success);
                while self.outcomes.len() > self.window {
                    self.outcomes.pop_front();
                }
                if self.failures() >= self.failure_threshold {
                    self.state = BreakerState::Open;
                    self.open_until = now + self.reset_timeout;
                }
            }
            BreakerState::Open => {}
        }
    }
}

/// Reactive circuit breaker: projects the `state` onto a `Cell`.
pub struct CircuitBreakerCell {
    core: RefCell<CircuitBreakerCore>,
    state: Source<BreakerState>,
}

impl CircuitBreakerCell {
    pub fn new(ctx: &Context, window: usize, failure_threshold: usize, reset_timeout: u64) -> Self {
        Self {
            core: RefCell::new(CircuitBreakerCore::new(
                window,
                failure_threshold,
                reset_timeout,
            )),
            state: ctx.source(BreakerState::Closed),
        }
    }
    fn refresh(&self, ctx: &Context) {
        let s = self.core.borrow().state();
        self.state.set(ctx, s);
    }
    pub fn allow(&self, ctx: &Context, now: u64) -> bool {
        let r = self.core.borrow_mut().allow(now);
        self.refresh(ctx);
        r
    }
    pub fn record(&self, ctx: &Context, success: bool, now: u64) {
        self.core.borrow_mut().record(success, now);
        self.refresh(ctx);
    }
    pub fn state(&self) -> BreakerState {
        self.core.borrow().state()
    }
    pub fn state_cell(&self) -> Source<BreakerState> {
        self.state
    }
}

// ===========================================================================
// Retry backoff
// ===========================================================================

/// Exponential-backoff compute core: `delay(attempt) = min(cap, base·2^attempt)`,
/// saturating to `cap` on shift overflow.
#[derive(Debug, Clone, Copy)]
pub struct RetryPolicyCore {
    base: u64,
    cap: u64,
    attempt: u32,
}

impl RetryPolicyCore {
    pub fn new(base: u64, cap: u64) -> Self {
        Self {
            base,
            cap,
            attempt: 0,
        }
    }
    /// The delay for `attempt` (saturating at `cap`).
    pub fn delay(&self, attempt: u32) -> u64 {
        self.base
            .checked_shl(attempt)
            .map_or(self.cap, |d| d.min(self.cap))
    }
    /// The current attempt's delay, then advance.
    pub fn next_delay(&mut self) -> u64 {
        let d = self.delay(self.attempt);
        self.attempt = self.attempt.saturating_add(1);
        d
    }
    pub fn reset(&mut self) {
        self.attempt = 0;
    }
}

/// Reactive retry policy: projects the current delay onto a `Cell`.
pub struct RetryPolicyCell {
    core: RefCell<RetryPolicyCore>,
    delay: Source<u64>,
}

impl RetryPolicyCell {
    pub fn new(ctx: &Context, base: u64, cap: u64) -> Self {
        Self {
            core: RefCell::new(RetryPolicyCore::new(base, cap)),
            delay: ctx.source(0),
        }
    }
    pub fn next_delay(&self, ctx: &Context) -> u64 {
        let d = self.core.borrow_mut().next_delay();
        self.delay.set(ctx, d);
        d
    }
    pub fn reset(&self, ctx: &Context) {
        self.core.borrow_mut().reset();
        self.delay.set(ctx, 0);
    }
    pub fn delay(&self, ctx: &Context) -> u64 {
        self.delay.get(ctx)
    }
    pub fn delay_cell(&self) -> Source<u64> {
        self.delay
    }
}

// ===========================================================================
// Bulkhead
// ===========================================================================

/// Bounded isolation-pool compute core.
#[derive(Debug, Clone, Copy)]
pub struct BulkheadCore {
    capacity: u64,
    in_use: u64,
}

impl BulkheadCore {
    pub fn new(capacity: u64) -> Self {
        Self {
            capacity,
            in_use: 0,
        }
    }
    pub fn in_use(&self) -> u64 {
        self.in_use
    }
    pub fn acquire(&mut self) -> bool {
        if self.in_use < self.capacity {
            self.in_use += 1;
            true
        } else {
            false
        }
    }
    pub fn release(&mut self) {
        if self.in_use > 0 {
            self.in_use -= 1;
        }
    }
}

/// Reactive bulkhead: projects `permits_in_use` onto a `Cell`.
pub struct BulkheadCell {
    core: RefCell<BulkheadCore>,
    in_use: Source<u64>,
}

impl BulkheadCell {
    pub fn new(ctx: &Context, capacity: u64) -> Self {
        Self {
            core: RefCell::new(BulkheadCore::new(capacity)),
            in_use: ctx.source(0),
        }
    }
    fn refresh(&self, ctx: &Context) {
        let u = self.core.borrow().in_use();
        self.in_use.set(ctx, u);
    }
    pub fn acquire(&self, ctx: &Context) -> bool {
        let r = self.core.borrow_mut().acquire();
        self.refresh(ctx);
        r
    }
    pub fn release(&self, ctx: &Context) {
        self.core.borrow_mut().release();
        self.refresh(ctx);
    }
    pub fn permits_in_use(&self, ctx: &Context) -> u64 {
        self.in_use.get(ctx)
    }
    pub fn permits_in_use_cell(&self) -> Source<u64> {
        self.in_use
    }
}

// ===========================================================================
// Timeout
// ===========================================================================

/// Deadline-bounded call compute core.
#[derive(Debug, Clone, Copy)]
pub struct TimeoutCore {
    deadline: u64,
    armed: bool,
    timed_out: bool,
}

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

impl TimeoutCore {
    pub fn new() -> Self {
        Self {
            deadline: 0,
            armed: false,
            timed_out: false,
        }
    }
    /// Arm the timeout with `deadline = now + timeout`.
    pub fn arm(&mut self, now: u64, timeout: u64) {
        self.deadline = now + timeout;
        self.armed = true;
        self.timed_out = false;
    }
    /// Fast-fail when `now ≥ deadline`; returns the timeout edge (once).
    pub fn tick(&mut self, now: u64) -> bool {
        if self.armed && !self.timed_out && now >= self.deadline {
            self.timed_out = true;
            true
        } else {
            false
        }
    }
    pub fn is_timed_out(&self) -> bool {
        self.timed_out
    }
}

/// Reactive timeout: projects `is_timed_out` onto a `Cell`.
pub struct TimeoutCell {
    core: RefCell<TimeoutCore>,
    timed_out: Source<bool>,
}

impl TimeoutCell {
    pub fn new(ctx: &Context) -> Self {
        Self {
            core: RefCell::new(TimeoutCore::new()),
            timed_out: ctx.source(false),
        }
    }
    fn refresh(&self, ctx: &Context) {
        let t = self.core.borrow().is_timed_out();
        self.timed_out.set(ctx, t);
    }
    pub fn arm(&self, ctx: &Context, now: u64, timeout: u64) {
        self.core.borrow_mut().arm(now, timeout);
        self.refresh(ctx);
    }
    pub fn tick(&self, ctx: &Context, now: u64) -> bool {
        let r = self.core.borrow_mut().tick(now);
        self.refresh(ctx);
        r
    }
    pub fn is_timed_out(&self, ctx: &Context) -> bool {
        self.timed_out.get(ctx)
    }
    pub fn is_timed_out_cell(&self) -> Source<bool> {
        self.timed_out
    }
}

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

    #[test]
    fn breaker_trips_and_recovers() {
        let mut b = CircuitBreakerCore::new(3, 2, 5);
        b.record(false, 0);
        assert_eq!(b.state(), BreakerState::Closed);
        b.record(false, 1);
        assert_eq!(b.state(), BreakerState::Open);
        assert!(!b.allow(2)); // fast-fail
        assert!(b.allow(6)); // -> HalfOpen probe
        assert_eq!(b.state(), BreakerState::HalfOpen);
        b.record(true, 6); // close
        assert_eq!(b.state(), BreakerState::Closed);
    }

    #[test]
    fn retry_exponential_saturates() {
        let mut r = RetryPolicyCore::new(100, 2000);
        assert_eq!(r.next_delay(), 100);
        assert_eq!(r.next_delay(), 200);
        assert_eq!(r.next_delay(), 400);
        assert_eq!(r.next_delay(), 800);
        assert_eq!(r.next_delay(), 1600);
        assert_eq!(r.next_delay(), 2000);
        assert_eq!(r.next_delay(), 2000);
    }

    #[test]
    fn bulkhead_bounded() {
        let mut b = BulkheadCore::new(2);
        assert!(b.acquire());
        assert!(b.acquire());
        assert!(!b.acquire());
        b.release();
        assert_eq!(b.in_use(), 1);
    }

    #[test]
    fn timeout_fires_once() {
        let mut t = TimeoutCore::new();
        t.arm(0, 5);
        assert!(!t.tick(3));
        assert!(t.tick(5));
        assert!(!t.tick(9)); // idempotent
        assert!(t.is_timed_out());
    }
}