nizaam-core 1.0.1

Domain-agnostic Rust infrastructure core for building secure, observable, reliable, and extensible Nizaam engines.
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
//! Bounded retry backoff and randomized jitter calculation.
//!
//! This module computes how long execution should wait before a subsequent
//! retry. It does not decide whether a retry is permitted, create attempts,
//! perform asynchronous sleeping, or own cancellation, deadlines, resource
//! admission, or idempotency semantics.

use std::time::Duration;

/// Jitter strategy applied after the exponential backoff has been bounded by
/// the configured maximum delay.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum JitterPolicy {
    /// Do not randomize the calculated delay.
    None,
    /// Choose a delay uniformly across the range from zero to the bounded
    /// delay, inclusive at the conceptual boundary.
    Full,
    /// Keep at least half of the bounded delay and randomize the remaining
    /// half.
    Equal,
}

/// Supplies randomness to a [`BackoffPolicy`] when jitter is enabled.
///
/// The retry subsystem does not own a global random-number generator. Runtime
/// or test code supplies a source explicitly, allowing production randomness
/// and deterministic tests to use the same backoff implementation.
pub trait JitterSource {
    /// Returns the next uniformly distributed 64-bit sample from the source.
    fn next_u64(&mut self) -> u64;
}

/// Error returned when a backoff policy cannot be constructed safely.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BackoffPolicyError {
    /// The configured base delay cannot exceed the configured maximum delay.
    BaseDelayExceedsMaximum,
}

impl std::fmt::Display for BackoffPolicyError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BaseDelayExceedsMaximum => {
                formatter.write_str("base delay must not exceed maximum delay")
            }
        }
    }
}

impl std::error::Error for BackoffPolicyError {}

/// Error returned while calculating a retry delay.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BackoffError {
    /// Retry numbering starts at one because the first retry follows the
    /// original attempt.
    InvalidRetryNumber,
    /// A jitter strategy requiring randomness was selected without supplying
    /// a randomness source.
    JitterSourceRequired,
}

impl std::fmt::Display for BackoffError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidRetryNumber => {
                formatter.write_str("retry number must be greater than zero")
            }
            Self::JitterSourceRequired => {
                formatter.write_str("a jitter source is required when jitter is enabled")
            }
        }
    }
}

impl std::error::Error for BackoffError {}

/// Immutable retry timing configuration.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BackoffPolicy {
    base_delay: Duration,
    max_delay: Duration,
    jitter: JitterPolicy,
}

impl BackoffPolicy {
    /// Creates a bounded exponential backoff policy.
    ///
    /// The first retry uses the configured base delay. Each subsequent retry
    /// doubles the delay until `max_delay` is reached. Jitter is applied only
    /// after that maximum-delay bound has been enforced.
    pub fn new(
        base_delay: Duration,
        max_delay: Duration,
        jitter: JitterPolicy,
    ) -> Result<Self, BackoffPolicyError> {
        if base_delay > max_delay {
            return Err(BackoffPolicyError::BaseDelayExceedsMaximum);
        }

        Ok(Self {
            base_delay,
            max_delay,
            jitter,
        })
    }

    /// Creates an explicit no-backoff, no-jitter policy.
    pub const fn no_backoff() -> Self {
        Self {
            base_delay: Duration::ZERO,
            max_delay: Duration::ZERO,
            jitter: JitterPolicy::None,
        }
    }

    /// Returns the configured base delay.
    pub const fn base_delay(&self) -> Duration {
        self.base_delay
    }

    /// Returns the configured maximum delay.
    pub const fn max_delay(&self) -> Duration {
        self.max_delay
    }

    /// Returns the configured jitter strategy.
    pub const fn jitter(&self) -> JitterPolicy {
        self.jitter
    }

    /// Calculates the delay before the specified one-based retry.
    ///
    /// `retry_number = 1` refers to the delay before creating the second
    /// attempt, `retry_number = 2` to the delay before the third attempt, and
    /// so on.
    pub fn delay(
        &self,
        retry_number: u32,
        mut jitter_source: Option<&mut dyn JitterSource>,
    ) -> Result<Duration, BackoffError> {
        if retry_number == 0 {
            return Err(BackoffError::InvalidRetryNumber);
        }

        let bounded_delay = self.exponential_delay(retry_number);

        match self.jitter {
            JitterPolicy::None => Ok(bounded_delay),
            JitterPolicy::Full => {
                let source = jitter_source
                    .as_deref_mut()
                    .ok_or(BackoffError::JitterSourceRequired)?;
                Ok(jitter_full(bounded_delay, source.next_u64()))
            }
            JitterPolicy::Equal => {
                let source = jitter_source.ok_or(BackoffError::JitterSourceRequired)?;
                Ok(jitter_equal(bounded_delay, source.next_u64()))
            }
        }
    }

    /// Calculates the exponentially increasing delay and caps it at the
    /// configured maximum without overflowing or iterating through an
    /// unbounded retry count.
    fn exponential_delay(&self, retry_number: u32) -> Duration {
        if self.base_delay.is_zero() || self.base_delay >= self.max_delay {
            return self.base_delay;
        }

        let mut delay = self.base_delay;
        let mut remaining_doublings = retry_number.saturating_sub(1);

        while remaining_doublings > 0 {
            if delay >= self.max_delay {
                break;
            }

            let doubled = delay.saturating_add(delay);
            delay = doubled.min(self.max_delay);
            remaining_doublings -= 1;
        }

        delay.min(self.max_delay)
    }
}

fn jitter_full(delay: Duration, sample: u64) -> Duration {
    duration_from_nanos(random_inclusive(delay.as_nanos(), sample))
}

fn jitter_equal(delay: Duration, sample: u64) -> Duration {
    let delay_nanos = delay.as_nanos();
    let lower_bound = delay_nanos / 2;
    let span = delay_nanos.saturating_sub(lower_bound);
    let offset = random_inclusive(span, sample);

    duration_from_nanos(lower_bound.saturating_add(offset))
}

/// Maps a 64-bit random sample into the inclusive range `0..=upper_bound`.
///
/// The calculation avoids multiplying the full `u128` upper bound by the
/// `u64` sample directly, because a maximum [`Duration`] can require more
/// than 128 bits for that intermediate product.
fn random_inclusive(upper_bound: u128, sample: u64) -> u128 {
    if upper_bound == 0 || sample == 0 {
        return 0;
    }

    let denominator = u64::MAX as u128;
    let sample = sample as u128;
    let quotient = upper_bound / denominator;
    let remainder = upper_bound % denominator;

    quotient * sample + (remainder * sample) / denominator
}

fn duration_from_nanos(nanos: u128) -> Duration {
    let seconds = nanos / 1_000_000_000;
    let subsec_nanos = (nanos % 1_000_000_000) as u32;

    if seconds > u64::MAX as u128 {
        Duration::MAX
    } else {
        Duration::new(seconds as u64, subsec_nanos)
    }
}

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

    #[derive(Clone, Copy, Debug)]
    struct FixedJitterSource {
        sample: u64,
    }

    impl JitterSource for FixedJitterSource {
        fn next_u64(&mut self) -> u64 {
            self.sample
        }
    }

    fn milliseconds(value: u64) -> Duration {
        Duration::from_millis(value)
    }

    fn policy() -> BackoffPolicy {
        BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::None).unwrap()
    }

    #[test]
    fn creates_valid_policy() {
        let backoff = policy();

        assert_eq!(backoff.base_delay(), milliseconds(100));
        assert_eq!(backoff.max_delay(), milliseconds(1_000));
        assert_eq!(backoff.jitter(), JitterPolicy::None);
    }

    #[test]
    fn rejects_base_delay_above_maximum() {
        let result =
            BackoffPolicy::new(milliseconds(1_001), milliseconds(1_000), JitterPolicy::None);

        assert_eq!(result, Err(BackoffPolicyError::BaseDelayExceedsMaximum));
    }

    #[test]
    fn allows_zero_delays() {
        let result = BackoffPolicy::new(Duration::ZERO, Duration::ZERO, JitterPolicy::None);

        assert!(result.is_ok());
        assert_eq!(result.unwrap().delay(1, None).unwrap(), Duration::ZERO);
    }

    #[test]
    fn no_backoff_is_explicit_and_zero() {
        let backoff = BackoffPolicy::no_backoff();

        assert_eq!(backoff.base_delay(), Duration::ZERO);
        assert_eq!(backoff.max_delay(), Duration::ZERO);
        assert_eq!(backoff.jitter(), JitterPolicy::None);
        assert_eq!(backoff.delay(1, None).unwrap(), Duration::ZERO);
    }

    #[test]
    fn rejects_zero_retry_number() {
        assert_eq!(
            policy().delay(0, None),
            Err(BackoffError::InvalidRetryNumber)
        );
    }

    #[test]
    fn first_retry_uses_base_delay() {
        assert_eq!(policy().delay(1, None).unwrap(), milliseconds(100));
    }

    #[test]
    fn subsequent_retries_double_exponentially() {
        let backoff = policy();

        assert_eq!(backoff.delay(1, None).unwrap(), milliseconds(100));
        assert_eq!(backoff.delay(2, None).unwrap(), milliseconds(200));
        assert_eq!(backoff.delay(3, None).unwrap(), milliseconds(400));
        assert_eq!(backoff.delay(4, None).unwrap(), milliseconds(800));
    }

    #[test]
    fn delay_is_capped_at_maximum() {
        let backoff = policy();

        assert_eq!(backoff.delay(5, None).unwrap(), milliseconds(1_000));
        assert_eq!(backoff.delay(6, None).unwrap(), milliseconds(1_000));
    }

    #[test]
    fn very_large_retry_number_remains_bounded() {
        let backoff = policy();

        assert_eq!(backoff.delay(u32::MAX, None).unwrap(), milliseconds(1_000));
    }

    #[test]
    fn none_jitter_does_not_require_source() {
        assert_eq!(policy().delay(3, None).unwrap(), milliseconds(400));
    }

    #[test]
    fn enabled_jitter_requires_source() {
        let full =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Full).unwrap();
        let equal = BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Equal)
            .unwrap();

        assert_eq!(full.delay(1, None), Err(BackoffError::JitterSourceRequired));
        assert_eq!(
            equal.delay(1, None),
            Err(BackoffError::JitterSourceRequired)
        );
    }

    #[test]
    fn full_jitter_zero_sample_reaches_lower_bound() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Full).unwrap();
        let mut source = FixedJitterSource { sample: 0 };

        assert_eq!(backoff.delay(3, Some(&mut source)).unwrap(), Duration::ZERO);
    }

    #[test]
    fn full_jitter_max_sample_reaches_upper_bound() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Full).unwrap();
        let mut source = FixedJitterSource { sample: u64::MAX };

        assert_eq!(
            backoff.delay(3, Some(&mut source)).unwrap(),
            milliseconds(400)
        );
    }

    #[test]
    fn full_jitter_stays_within_bounded_delay() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Full).unwrap();
        let samples = [0, 1, u64::MAX / 2, u64::MAX - 1, u64::MAX];

        for sample in samples {
            let mut source = FixedJitterSource { sample };
            let delay = backoff.delay(5, Some(&mut source)).unwrap();
            assert!(delay <= milliseconds(1_000));
        }
    }

    #[test]
    fn equal_jitter_preserves_half_of_delay() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Equal)
                .unwrap();

        let mut lower_source = FixedJitterSource { sample: 0 };
        let mut upper_source = FixedJitterSource { sample: u64::MAX };

        assert_eq!(
            backoff.delay(3, Some(&mut lower_source)).unwrap(),
            milliseconds(200)
        );
        assert_eq!(
            backoff.delay(3, Some(&mut upper_source)).unwrap(),
            milliseconds(400)
        );
    }

    #[test]
    fn equal_jitter_stays_between_half_and_full_delay() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Equal)
                .unwrap();
        let samples = [0, 1, u64::MAX / 2, u64::MAX - 1, u64::MAX];

        for sample in samples {
            let mut source = FixedJitterSource { sample };
            let delay = backoff.delay(4, Some(&mut source)).unwrap();
            assert!(delay >= milliseconds(400));
            assert!(delay <= milliseconds(800));
        }
    }

    #[test]
    fn jitter_is_applied_after_maximum_delay_is_enforced() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(500), JitterPolicy::Equal).unwrap();
        let mut source = FixedJitterSource { sample: u64::MAX };

        assert_eq!(
            backoff.delay(10, Some(&mut source)).unwrap(),
            milliseconds(500)
        );
    }

    #[test]
    fn zero_base_delay_remains_zero() {
        let backoff =
            BackoffPolicy::new(Duration::ZERO, milliseconds(1_000), JitterPolicy::None).unwrap();

        assert_eq!(backoff.delay(1, None).unwrap(), Duration::ZERO);
        assert_eq!(backoff.delay(100, None).unwrap(), Duration::ZERO);
    }

    #[test]
    fn deterministic_source_produces_repeatable_jitter() {
        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Full).unwrap();
        let mut first_source = FixedJitterSource { sample: 42 };
        let mut second_source = FixedJitterSource { sample: 42 };

        let first = backoff.delay(4, Some(&mut first_source)).unwrap();
        let second = backoff.delay(4, Some(&mut second_source)).unwrap();

        assert_eq!(first, second);
    }

    #[test]
    fn source_is_consumed_once_per_calculation() {
        #[derive(Debug)]
        struct CountingSource {
            calls: u32,
        }

        impl JitterSource for CountingSource {
            fn next_u64(&mut self) -> u64 {
                self.calls += 1;
                0
            }
        }

        let backoff =
            BackoffPolicy::new(milliseconds(100), milliseconds(1_000), JitterPolicy::Full).unwrap();
        let mut source = CountingSource { calls: 0 };

        let _ = backoff.delay(1, Some(&mut source)).unwrap();
        assert_eq!(source.calls, 1);
    }

    #[test]
    fn duration_conversion_preserves_subsecond_precision() {
        let delay = Duration::new(2, 345_678_901);

        assert_eq!(duration_from_nanos(delay.as_nanos()), delay);
    }
}