marque-utils 0.1.0

Common utilities for Marque, the marking compiler.
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
// Adapted from code originally in [CocoIndex](https://CocoIndex)
// Original code from CocoIndex is copyrighted by CocoIndex
// and licensed under the Apache-2.0 License.
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2026 CocoIndex
//
// All modifications from the upstream for Marque are copyrighted by Knitli Inc.
// SPDX-FileCopyrightText: 2026 Knitli Inc. (Marque)
// SPDX-License-Identifier: LicenseRef-MarqueLicense-1.0

//! Retries a fallible async operation with exponential backoff.
//!
//! [`run`] calls a closure that returns a future, and on a retryable error
//! sleeps and tries again — backing off from [`RetryOptions::initial_backoff`]
//! toward [`max_backoff`](RetryOptions::max_backoff) with jitter, and giving up
//! at [`retry_timeout`](RetryOptions::retry_timeout). An error decides its own
//! fate through [`IsRetryable`]; a non-retryable error returns immediately. The
//! crate [`Error`] type pairs the underlying error with that flag.

use std::{
    future::Future,
    time::{Duration, Instant},
};
use tracing::trace;

/// Lets an error declare whether retrying it could succeed.
pub trait IsRetryable {
    /// Returns `true` if the operation is worth retrying after this error.
    fn is_retryable(&self) -> bool;
}

/// A crate [`Error`](crate::error::Error) paired with its retryable flag, so
/// [`run`] knows whether to back off and try again or give up.
pub struct Error {
    pub error: crate::error::Error,
    pub is_retryable: bool,
}

/// Default ceiling on total retry time: ten minutes.
pub const DEFAULT_RETRY_TIMEOUT: Duration = Duration::from_secs(10 * 60);

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.error, f)
    }
}

impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(&self.error, f)
    }
}

impl IsRetryable for Error {
    fn is_retryable(&self) -> bool {
        self.is_retryable
    }
}

impl Error {
    /// Wraps an error and marks it retryable.
    pub fn retryable<E: Into<crate::error::Error>>(error: E) -> Self {
        Self {
            error: error.into(),
            is_retryable: true,
        }
    }

    /// Wraps an error and marks it non-retryable.
    pub fn not_retryable<E: Into<crate::error::Error>>(error: E) -> Self {
        Self {
            error: error.into(),
            is_retryable: false,
        }
    }
}

// A bare crate error carries no retryable signal, so it defaults to
// non-retryable; reach for `Error::retryable` to opt in.
impl From<crate::error::Error> for Error {
    fn from(error: crate::error::Error) -> Self {
        Self {
            error,
            is_retryable: false,
        }
    }
}

impl From<Error> for crate::error::Error {
    fn from(val: Error) -> Self {
        val.error
    }
}

impl<E: IsRetryable + std::error::Error + Send + Sync + 'static> From<E> for Error {
    fn from(error: E) -> Self {
        Self {
            is_retryable: error.is_retryable(),
            error: anyhow::Error::from(error).into(),
        }
    }
}

/// A [`Result`](std::result::Result) whose error defaults to this module's
/// [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Constructs the `Ok` variant of this module's [`Result`], shadowing the
/// prelude `Ok` so a `run` closure body can write `Ok(value)` without naming the
/// error type.
#[allow(non_snake_case)]
pub fn Ok<T>(value: T) -> Result<T> {
    Result::Ok(value)
}

/// Backoff and timeout knobs for [`run`].
pub struct RetryOptions {
    /// Total budget for all attempts. `None` retries until success.
    pub retry_timeout: Option<Duration>,
    /// Delay before the first retry.
    pub initial_backoff: Duration,
    /// Cap each backoff grows toward.
    pub max_backoff: Duration,
}

impl Default for RetryOptions {
    fn default() -> Self {
        Self {
            retry_timeout: Some(DEFAULT_RETRY_TIMEOUT),
            initial_backoff: Duration::from_millis(100),
            max_backoff: Duration::from_secs(10),
        }
    }
}

/// Slower-backoff preset (1s initial, 60s cap) for operations against a
/// resource already under load, where eager retries would only add pressure.
pub static HEAVY_LOADED_OPTIONS: RetryOptions = RetryOptions {
    retry_timeout: Some(DEFAULT_RETRY_TIMEOUT),
    initial_backoff: Duration::from_secs(1),
    max_backoff: Duration::from_secs(60),
};

/// Runs `f` until it succeeds, hits a non-retryable error, or the retry budget
/// runs out.
///
/// On a retryable error, sleeps for the current backoff and tries again,
/// growing the backoff by a jittered factor toward
/// [`max_backoff`](RetryOptions::max_backoff). A non-retryable error returns at
/// once. The final sleep is clamped so it never overshoots
/// [`retry_timeout`](RetryOptions::retry_timeout); once the deadline passes, the
/// last error is returned.
pub async fn run<
    Ok,
    Err: std::fmt::Display + IsRetryable,
    Fut: Future<Output = Result<Ok, Err>>,
    F: Fn() -> Fut,
>(
    f: F,
    options: &RetryOptions,
) -> Result<Ok, Err> {
    let deadline = options
        .retry_timeout
        .map(|timeout| Instant::now() + timeout);
    let mut backoff = options.initial_backoff;

    loop {
        match f().await {
            Result::Ok(result) => return Result::Ok(result),
            Result::Err(err) => {
                if !err.is_retryable() {
                    return Result::Err(err);
                }
                let mut sleep_duration = backoff;
                if let Some(deadline) = deadline {
                    let now = Instant::now();
                    if now >= deadline {
                        return Result::Err(err);
                    }
                    let remaining_time = deadline.saturating_duration_since(now);
                    sleep_duration = std::cmp::min(sleep_duration, remaining_time);
                }
                trace!(
                    "Will retry in {}ms for error: {}",
                    sleep_duration.as_millis(),
                    err
                );
                tokio::time::sleep(sleep_duration).await;
                if backoff < options.max_backoff {
                    backoff = std::cmp::min(
                        Duration::from_micros(
                            (backoff.as_micros() * u128::from(fastrand::u32(1618..=2000)) / 1000)
                                as u64,
                        ),
                        options.max_backoff,
                    );
                }
            }
        }
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::sync::atomic::{AtomicUsize, Ordering};

    /// Bare retryable error used to drive `run` without going through the
    /// crate error type's `std::error::Error` conversion path.
    struct TestErr {
        retryable: bool,
    }

    impl std::fmt::Display for TestErr {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "test error (retryable={})", self.retryable)
        }
    }

    impl IsRetryable for TestErr {
        fn is_retryable(&self) -> bool {
            self.retryable
        }
    }

    /// Implements `std::error::Error` + `IsRetryable` to exercise the blanket
    /// `From<E>` conversion that reads the source error's own retryable flag.
    #[derive(Debug)]
    struct StdRetryableErr;

    impl std::fmt::Display for StdRetryableErr {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "std retryable error")
        }
    }

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

    impl IsRetryable for StdRetryableErr {
        fn is_retryable(&self) -> bool {
            true
        }
    }

    fn fast_options() -> RetryOptions {
        RetryOptions {
            retry_timeout: Some(Duration::from_secs(5)),
            initial_backoff: Duration::from_millis(1),
            max_backoff: Duration::from_millis(2),
        }
    }

    #[test]
    fn retryable_and_not_retryable_set_flag() {
        let e = Error::retryable(crate::error::Error::internal_msg("boom"));
        assert!(e.is_retryable());

        let e = Error::not_retryable(crate::error::Error::internal_msg("boom"));
        assert!(!e.is_retryable());
    }

    #[test]
    fn from_core_error_defaults_to_not_retryable() {
        let core = crate::error::Error::internal_msg("boom");
        let e: Error = core.into();
        assert!(!e.is_retryable());
    }

    #[test]
    fn into_core_error_unwraps_inner() {
        let e = Error::retryable(crate::error::Error::internal_msg("inner cause"));
        let core: crate::error::Error = e.into();
        assert_eq!(core.to_string(), "inner cause");
    }

    #[test]
    fn from_std_error_reads_its_retryable_flag() {
        let e: Error = StdRetryableErr.into();
        assert!(e.is_retryable());
    }

    #[test]
    fn display_and_debug_delegate_to_inner_error() {
        let e = Error::not_retryable(crate::error::Error::internal_msg("inner msg"));
        assert_eq!(format!("{e}"), "inner msg");
        assert!(format!("{e:?}").contains("inner msg"));
    }

    #[test]
    fn ok_helper_constructs_ok_variant() {
        let r: Result<i32> = Ok(42);
        assert!(matches!(r, Result::Ok(42)));
    }

    #[test]
    fn retry_options_default_matches_documented_values() {
        let o = RetryOptions::default();
        assert_eq!(o.retry_timeout, Some(DEFAULT_RETRY_TIMEOUT));
        assert_eq!(o.initial_backoff, Duration::from_millis(100));
        assert_eq!(o.max_backoff, Duration::from_secs(10));
    }

    #[test]
    fn heavy_loaded_options_use_longer_backoff() {
        assert_eq!(HEAVY_LOADED_OPTIONS.initial_backoff, Duration::from_secs(1));
        assert_eq!(HEAVY_LOADED_OPTIONS.max_backoff, Duration::from_secs(60));
    }

    #[tokio::test]
    async fn run_returns_immediately_on_success() {
        let calls = Arc::new(AtomicUsize::new(0));
        let c = calls.clone();
        let opts = fast_options();

        let res: Result<i32, TestErr> = run(
            || {
                let c = c.clone();
                async move {
                    c.fetch_add(1, Ordering::SeqCst);
                    Result::Ok(7)
                }
            },
            &opts,
        )
        .await;

        assert!(matches!(res, Result::Ok(7)));
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn run_retries_until_success() {
        let calls = Arc::new(AtomicUsize::new(0));
        let c = calls.clone();
        let opts = fast_options();

        let res: Result<i32, TestErr> = run(
            || {
                let c = c.clone();
                async move {
                    let n = c.fetch_add(1, Ordering::SeqCst);
                    if n < 2 {
                        Result::Err(TestErr { retryable: true })
                    } else {
                        Result::Ok(99)
                    }
                }
            },
            &opts,
        )
        .await;

        assert!(matches!(res, Result::Ok(99)));
        assert_eq!(calls.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn run_stops_on_non_retryable_error() {
        let calls = Arc::new(AtomicUsize::new(0));
        let c = calls.clone();
        let opts = fast_options();

        let res: Result<i32, TestErr> = run(
            || {
                let c = c.clone();
                async move {
                    c.fetch_add(1, Ordering::SeqCst);
                    Result::Err(TestErr { retryable: false })
                }
            },
            &opts,
        )
        .await;

        assert!(res.is_err());
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn run_gives_up_when_deadline_elapsed() {
        let calls = Arc::new(AtomicUsize::new(0));
        let c = calls.clone();
        // A zero timeout means the deadline is already in the past after the
        // first attempt, so a retryable error still terminates the loop.
        let opts = RetryOptions {
            retry_timeout: Some(Duration::ZERO),
            initial_backoff: Duration::from_millis(1),
            max_backoff: Duration::from_millis(1),
        };

        let res: Result<i32, TestErr> = run(
            || {
                let c = c.clone();
                async move {
                    c.fetch_add(1, Ordering::SeqCst);
                    Result::Err(TestErr { retryable: true })
                }
            },
            &opts,
        )
        .await;

        assert!(res.is_err());
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn run_without_timeout_retries_then_succeeds() {
        let calls = Arc::new(AtomicUsize::new(0));
        let c = calls.clone();
        let opts = RetryOptions {
            retry_timeout: None,
            initial_backoff: Duration::from_millis(1),
            max_backoff: Duration::from_millis(1),
        };

        let res: Result<i32, TestErr> = run(
            || {
                let c = c.clone();
                async move {
                    let n = c.fetch_add(1, Ordering::SeqCst);
                    if n < 1 {
                        Result::Err(TestErr { retryable: true })
                    } else {
                        Result::Ok(5)
                    }
                }
            },
            &opts,
        )
        .await;

        assert!(matches!(res, Result::Ok(5)));
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }
}