progenitor-extras 0.2.0

Extra functionality for the Progenitor OpenAPI client generator
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
//! Retry logic for operations against Progenitor-generated API clients.
//!
//! The primary entry points are:
//!
//! * [`retry_operation`] and [`retry_operation_indefinitely`], for retries; and
//! * [`retry_operation_while`] and [`retry_operation_while_indefinitely`], for
//!   retries with an additional "gone check" that can abort the loop when the
//!   target is permanently unavailable.
//!
//! The `_indefinitely` variants never produce a "retries exhausted" error and
//! will retry transient failures forever (or until the gone check aborts).
//!
//! Retry uses a backoff policy via the [`backon`] crate. Call
//! [`default_retry_policy`] or [`default_indefinite_retry_policy`] for
//! reasonable defaults, or construct your own [`backon::BackoffBuilder`] for
//! custom behavior.
//!
//! Note that the retry operations currently assume a Tokio backend, matching
//! Progenitor.

use backon::{BackoffBuilder, ExponentialBuilder};
use std::{
    convert::Infallible, error::Error, fmt, future::Future, time::Duration,
};

/// Result of a gone check passed to [`retry_operation_while`] and
/// [`retry_operation_while_indefinitely`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GoneCheckResult {
    /// The target is still available; continue retrying.
    StillAvailable,

    /// The target is permanently gone; abort the retry loop.
    Gone,
}

/// Error produced by [`retry_operation`].
#[derive(Debug)]
pub struct RetryOperationError<E> {
    /// One-indexed attempt number at which the error occurred.
    pub attempt: usize,
    /// The kind of error.
    pub kind: RetryOperationErrorKind<E>,
}

/// The kind of error in a [`RetryOperationError`].
#[derive(Debug)]
pub enum RetryOperationErrorKind<E> {
    /// The operation failed with a non-retryable error.
    OperationError(progenitor_client::Error<E>),

    /// All retry attempts were exhausted without success.
    ///
    /// The contained error is the last retryable error encountered.
    RetriesExhausted(progenitor_client::Error<E>),
}

impl<E> fmt::Display for RetryOperationErrorKind<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::OperationError(_) => {
                f.write_str("progenitor API operation failed")
            }
            Self::RetriesExhausted(_) => f.write_str("retries exhausted"),
        }
    }
}

impl<E> fmt::Display for RetryOperationError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "failed at attempt {}: ", self.attempt)?;
        self.kind.fmt(f)
    }
}

impl<E> Error for RetryOperationError<E>
where
    E: fmt::Debug + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.kind {
            RetryOperationErrorKind::OperationError(e)
            | RetryOperationErrorKind::RetriesExhausted(e) => Some(e),
        }
    }
}

impl<E> RetryOperationError<E> {
    /// Returns true if the underlying operation error is a 404 Not Found.
    pub fn is_not_found(&self) -> bool {
        match &self.kind {
            // In practice, 404 is not retryable, so this will only match
            // OperationError. But something outside of this crate can
            // artificially construct a RetriesExhausted with a 404, so match
            // against that as well.
            RetryOperationErrorKind::OperationError(e)
            | RetryOperationErrorKind::RetriesExhausted(e) => {
                e.status() == Some(http::StatusCode::NOT_FOUND)
            }
        }
    }
}

/// Error produced by [`retry_operation_while`].
#[derive(Debug)]
pub struct RetryOperationWhileError<E, GoneErr = Infallible> {
    /// One-indexed attempt number at which the error occurred.
    pub attempt: usize,
    /// The kind of error.
    pub kind: RetryOperationWhileErrorKind<E, GoneErr>,
}

/// The kind of error in a [`RetryOperationWhileError`].
#[derive(Debug)]
pub enum RetryOperationWhileErrorKind<E, GoneErr = Infallible> {
    /// The gone check indicated that the remote server is permanently
    /// unavailable.
    Gone,

    /// The gone check itself failed.
    GoneCheckError(GoneErr),

    /// The operation failed with a non-retryable error.
    OperationError(progenitor_client::Error<E>),

    /// All retry attempts were exhausted without success.
    ///
    /// The contained error is the last retryable error encountered.
    RetriesExhausted(progenitor_client::Error<E>),
}

impl<E, GoneErr> fmt::Display for RetryOperationWhileErrorKind<E, GoneErr> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Gone => f.write_str("remote server is gone"),
            Self::GoneCheckError(_) => {
                f.write_str("failed to determine whether remote server is gone")
            }
            Self::OperationError(_) => {
                f.write_str("progenitor API operation failed")
            }
            Self::RetriesExhausted(_) => f.write_str("retries exhausted"),
        }
    }
}

impl<E, GoneErr> fmt::Display for RetryOperationWhileError<E, GoneErr> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "failed at attempt {}: ", self.attempt)?;
        self.kind.fmt(f)
    }
}

impl<E, GoneErr> Error for RetryOperationWhileError<E, GoneErr>
where
    E: fmt::Debug + 'static,
    GoneErr: Error + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.kind {
            RetryOperationWhileErrorKind::Gone => None,
            RetryOperationWhileErrorKind::GoneCheckError(e) => Some(e),
            RetryOperationWhileErrorKind::OperationError(e)
            | RetryOperationWhileErrorKind::RetriesExhausted(e) => Some(e),
        }
    }
}

impl<E, GoneErr> RetryOperationWhileError<E, GoneErr> {
    /// Returns true if the underlying operation error is a 404 Not Found.
    pub fn is_not_found(&self) -> bool {
        match &self.kind {
            // In practice, 404 is not retryable, so this will only match
            // OperationError. But something outside of this crate can
            // artificially construct a RetriesExhausted with a 404, so match
            // against that as well.
            RetryOperationWhileErrorKind::OperationError(e)
            | RetryOperationWhileErrorKind::RetriesExhausted(e) => {
                e.status() == Some(http::StatusCode::NOT_FOUND)
            }
            RetryOperationWhileErrorKind::Gone
            | RetryOperationWhileErrorKind::GoneCheckError(_) => false,
        }
    }

    /// Returns `true` if the remote server is gone.
    pub fn is_gone(&self) -> bool {
        match &self.kind {
            RetryOperationWhileErrorKind::Gone => true,
            RetryOperationWhileErrorKind::GoneCheckError(_)
            | RetryOperationWhileErrorKind::OperationError(_)
            | RetryOperationWhileErrorKind::RetriesExhausted(_) => false,
        }
    }
}

/// Error produced by [`retry_operation_indefinitely`].
///
/// Unlike [`RetryOperationError`], this error has no `RetriesExhausted`
/// variant: the indefinite retry function retries transient failures forever,
/// so the only way it can fail is with a non-retryable operation error.
#[derive(Debug)]
pub struct IndefiniteRetryOperationError<E> {
    /// One-indexed attempt number at which the error occurred.
    pub attempt: usize,
    /// The non-retryable operation error.
    pub error: progenitor_client::Error<E>,
}

impl<E> fmt::Display for IndefiniteRetryOperationError<E> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "failed at attempt {}: progenitor API operation failed",
            self.attempt,
        )
    }
}

impl<E> Error for IndefiniteRetryOperationError<E>
where
    E: fmt::Debug + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.error)
    }
}

impl<E> IndefiniteRetryOperationError<E> {
    /// Returns true if the underlying operation error is a 404 Not Found.
    pub fn is_not_found(&self) -> bool {
        self.error.status() == Some(http::StatusCode::NOT_FOUND)
    }
}

/// Error produced by [`retry_operation_while_indefinitely`].
#[derive(Debug)]
pub struct IndefiniteRetryOperationWhileError<E, GoneErr = Infallible> {
    /// One-indexed attempt number at which the error occurred.
    pub attempt: usize,
    /// The kind of error.
    pub kind: IndefiniteRetryOperationWhileErrorKind<E, GoneErr>,
}

/// The kind of error in an [`IndefiniteRetryOperationWhileError`].
#[derive(Debug)]
pub enum IndefiniteRetryOperationWhileErrorKind<E, GoneErr = Infallible> {
    /// The gone check indicated that the remote server is permanently
    /// unavailable.
    Gone,

    /// The gone check itself failed.
    GoneCheckError(GoneErr),

    /// The operation failed with a non-retryable error.
    OperationError(progenitor_client::Error<E>),
}

impl<E, GoneErr> fmt::Display
    for IndefiniteRetryOperationWhileErrorKind<E, GoneErr>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Gone => f.write_str("remote server is gone"),
            Self::GoneCheckError(_) => {
                f.write_str("failed to determine whether remote server is gone")
            }
            Self::OperationError(_) => {
                f.write_str("progenitor API operation failed")
            }
        }
    }
}

impl<E, GoneErr> fmt::Display
    for IndefiniteRetryOperationWhileError<E, GoneErr>
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "failed at attempt {}: ", self.attempt)?;
        self.kind.fmt(f)
    }
}

impl<E, GoneErr> Error for IndefiniteRetryOperationWhileError<E, GoneErr>
where
    E: fmt::Debug + 'static,
    GoneErr: Error + 'static,
{
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.kind {
            IndefiniteRetryOperationWhileErrorKind::Gone => None,
            IndefiniteRetryOperationWhileErrorKind::GoneCheckError(e) => {
                Some(e)
            }
            IndefiniteRetryOperationWhileErrorKind::OperationError(e) => {
                Some(e)
            }
        }
    }
}

impl<E, GoneErr> IndefiniteRetryOperationWhileError<E, GoneErr> {
    /// Returns true if the underlying operation error is a 404 Not Found.
    pub fn is_not_found(&self) -> bool {
        match &self.kind {
            IndefiniteRetryOperationWhileErrorKind::OperationError(e) => {
                e.status() == Some(http::StatusCode::NOT_FOUND)
            }
            IndefiniteRetryOperationWhileErrorKind::Gone
            | IndefiniteRetryOperationWhileErrorKind::GoneCheckError(_) => {
                false
            }
        }
    }

    /// Returns `true` if the remote server is gone.
    pub fn is_gone(&self) -> bool {
        match &self.kind {
            IndefiniteRetryOperationWhileErrorKind::Gone => true,
            IndefiniteRetryOperationWhileErrorKind::GoneCheckError(_)
            | IndefiniteRetryOperationWhileErrorKind::OperationError(_) => {
                false
            }
        }
    }
}

/// Returns a reasonable default retry policy.
///
/// This policy is an exponential backoff that sets:
///
/// * the initial retry interval to ~250ms (mean, with jitter)
/// * the maximum interval to 3 minutes
/// * a backoff multiplier of 2.0
/// * up to 13 retries
/// * with jitter enabled
///
/// The base delay is set to 167ms rather than 250ms to compensate for
/// `backon`'s additive jitter, which distributes each delay `d` over
/// `[d, 2d)` (mean = 1.5d). With a 167ms base, the mean first retry
/// delay is ~250ms.
pub fn default_retry_policy() -> ExponentialBuilder {
    ExponentialBuilder::default()
        .with_factor(2.0)
        .with_min_delay(Duration::from_millis(167))
        .with_max_delay(Duration::from_secs(60 * 3))
        .with_max_times(13)
        .with_jitter()
}

/// Exponential backoff parameters for the indefinite retry functions.
///
/// All fields must be specified explicitly. Use
/// [`default_indefinite_retry_policy`] for reasonable defaults.
#[derive(Clone, Copy, Debug)]
pub struct IndefiniteBackoffParams {
    /// Multiplier applied to the delay after each retry.
    pub factor: f32,
    /// The minimum (initial) delay between retries.
    pub min_delay: Duration,
    /// The maximum delay between retries.
    pub max_delay: Duration,
    /// Whether to add random jitter to each delay.
    ///
    /// When enabled, `backon` distributes each computed delay `d` uniformly
    /// over `[d, 2d)`. Account for this when choosing `min_delay` if you
    /// need a specific mean initial interval.
    pub jitter: bool,
}

impl IndefiniteBackoffParams {
    /// Builds an infinite delay iterator from these parameters.
    fn build(self) -> impl Iterator<Item = Duration> {
        let mut builder = ExponentialBuilder::default()
            .with_factor(self.factor)
            .with_min_delay(self.min_delay)
            .with_max_delay(self.max_delay)
            // backon 1.6.0 sets a max times of 3.
            .without_max_times()
            // backon 1.6.0 does not set a max total delay, but set one
            // explicitly anyway.
            .with_total_delay(None);

        if self.jitter {
            builder = builder.with_jitter();
        }
        builder.build()
    }
}

/// Returns a reasonable default indefinite retry policy.
///
/// This policy is an exponential backoff that sets:
///
/// * the initial retry interval to ~250ms (mean, with jitter)
/// * the maximum interval to 3 minutes
/// * a backoff multiplier of 2.0
/// * with jitter enabled
///
/// The base delay is set to 167ms rather than 250ms to compensate for
/// `backon`'s additive jitter, which distributes each delay `d` over
/// `[d, 2d)` (mean = 1.5d). With a 167ms base, the mean first retry
/// delay is ~250ms.
pub fn default_indefinite_retry_policy() -> IndefiniteBackoffParams {
    IndefiniteBackoffParams {
        factor: 2.0,
        min_delay: Duration::from_millis(167),
        max_delay: Duration::from_secs(60 * 3),
        jitter: true,
    }
}

/// Data passed into notify functions.
#[derive(Debug)]
#[non_exhaustive]
pub struct RetryNotification<E> {
    /// One-indexed attempt number. The first transient failure produces
    /// `attempt = 1`, the second produces `attempt = 2`, and so on.
    ///
    /// For finite retry functions ([`retry_operation`],
    /// [`retry_operation_while`]), the notify function is not called when
    /// retries are exhausted. Instead, an error is returned.
    pub attempt: usize,
    /// The retryable error that caused this retry. This error is always
    /// retryable (i.e., `error.is_retryable()` returns `true`).
    pub error: progenitor_client::Error<E>,
    /// The delay before the next attempt.
    pub delay: Duration,
}

/// Retries a progenitor client operation until it succeeds or fails
/// permanently.
///
/// Transient (retryable) errors are retried according to the supplied backoff
/// policy. All other errors are returned immediately as
/// [`RetryOperationErrorKind::OperationError`].
///
/// If all retries are exhausted, the last transient error is returned as
/// [`RetryOperationErrorKind::RetriesExhausted`].
///
/// `notify` is called on each transient failure with the error and the delay
/// before the next attempt. It is not called when retries are exhausted;
/// the terminal failure is communicated through the
/// [`RetriesExhausted`](RetryOperationErrorKind::RetriesExhausted) return variant.
///
/// The `operation` must be idempotent.
///
/// # Examples
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// use progenitor_extras::retry::{default_retry_policy, retry_operation};
///
/// // In practice, replace the closure body with a progenitor client
/// // call, e.g. `|| async { client.some_endpoint().send().await }`.
/// let result = retry_operation(
///     default_retry_policy(),
///     || async { Ok::<_, progenitor_client::Error<()>>(42u32) },
///     |notification| {
///         eprintln!(
///             "transient error ({:?}), retrying in {:?}",
///             notification.error, notification.delay,
///         );
///     },
/// )
/// .await;
///
/// assert_eq!(result.unwrap(), 42);
/// # }
/// ```
pub async fn retry_operation<T, E, B, N, F, Fut>(
    backoff: B,
    mut operation: F,
    mut notify: N,
) -> Result<T, RetryOperationError<E>>
where
    B: BackoffBuilder,
    N: FnMut(RetryNotification<E>),
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, progenitor_client::Error<E>>>,
{
    // This function implements its own retry loop rather than delegating to
    // backon's `Retryable` trait so that the notify function can be called with
    // an owned error.
    let mut delays = backoff.build();
    let mut attempt = 1;

    loop {
        match (operation)().await {
            Ok(v) => return Ok(v),
            Err(error) => {
                if !error.is_retryable() {
                    return Err(RetryOperationError {
                        attempt,
                        kind: RetryOperationErrorKind::OperationError(error),
                    });
                }
                match delays.next() {
                    Some(delay) => {
                        notify(RetryNotification { attempt, error, delay });
                        tokio::time::sleep(delay).await;
                        attempt += 1;
                    }
                    None => {
                        return Err(RetryOperationError {
                            attempt,
                            kind: RetryOperationErrorKind::RetriesExhausted(
                                error,
                            ),
                        });
                    }
                }
            }
        }
    }
}

/// Retries a progenitor client operation with an additional "gone check."
///
/// This function is intended for service mesh-type scenarios, where a
/// service being gone is determined independently of the operation itself.
///
/// Before each attempt, `gone_check` is called. If it returns
/// `Ok(GoneCheckResult::Gone)`, the loop is aborted with
/// [`RetryOperationWhileErrorKind::Gone`]. If the gone check itself fails, the
/// loop is aborted with [`RetryOperationWhileErrorKind::GoneCheckError`].
///
/// Transient errors (as classified by
/// [`progenitor_client::Error::is_retryable`]) are retried according to the
/// supplied `backoff` policy. Non-retryable errors are returned as
/// [`RetryOperationWhileErrorKind::OperationError`]. If all retries are
/// exhausted, the last transient error is returned as
/// [`RetryOperationWhileErrorKind::RetriesExhausted`].
///
/// Gone-check errors ([`RetryOperationWhileErrorKind::GoneCheckError`]) are
/// treated as permanent and abort the loop immediately. If the gone check
/// itself can fail transiently, handle retries within the `gone_check`
/// closure.
///
/// `notify` is called on each transient failure with the error and the
/// delay before the next attempt. It is not called when retries are
/// exhausted; the terminal failure is communicated through the
/// [`RetryOperationWhileErrorKind::RetriesExhausted`] return variant.
///
/// The `operation` must be idempotent.
///
/// # Examples
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// use progenitor_extras::retry::{
///     GoneCheckResult, default_retry_policy, retry_operation_while,
/// };
///
/// // In practice, replace these closure bodies with real client calls
/// // and a real gone check (e.g. querying whether a sled is in service).
/// let result = retry_operation_while(
///     default_retry_policy(),
///     || async { Ok::<_, progenitor_client::Error<()>>(42u32) },
///     || async {
///         Ok::<_, std::convert::Infallible>(GoneCheckResult::StillAvailable)
///     },
///     |notification| {
///         eprintln!(
///             "transient error ({:?}), retrying in {:?}",
///             notification.error, notification.delay,
///         );
///     },
/// )
/// .await;
///
/// assert_eq!(result.unwrap(), 42);
/// # }
/// ```
///
/// The gone check can abort the loop early:
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// use progenitor_extras::retry::{
///     GoneCheckResult, RetryOperationWhileError, default_retry_policy,
///     retry_operation_while,
/// };
///
/// let result: Result<(), RetryOperationWhileError<()>> =
///     retry_operation_while(
///         default_retry_policy(),
///         || async { Ok::<_, progenitor_client::Error<()>>(()) },
///         // Target is gone; abort immediately.
///         || async {
///             Ok::<_, std::convert::Infallible>(GoneCheckResult::Gone)
///         },
///         |_notification| {},
///     )
///     .await;
///
/// assert!(result.unwrap_err().is_gone());
/// # }
/// ```
pub async fn retry_operation_while<T, E, GoneErr, B, N, F, Fut, GF, GFut>(
    backoff: B,
    mut operation: F,
    mut gone_check: GF,
    mut notify: N,
) -> Result<T, RetryOperationWhileError<E, GoneErr>>
where
    B: BackoffBuilder,
    N: FnMut(RetryNotification<E>),
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, progenitor_client::Error<E>>>,
    GF: FnMut() -> GFut,
    GFut: Future<Output = Result<GoneCheckResult, GoneErr>>,
{
    // This function implements its own retry loop rather than delegating
    // to backon's `Retryable` trait so that:
    //
    // * the gone check can be interleaved before each operation attempt.
    // * the notify function can be called with an owned error.
    let mut delays = backoff.build();

    let mut attempt = 1;
    loop {
        // Check if the target is still available before attempting
        // the operation.
        //
        // An interesting question is: in this loop, should `gone_check` be
        // called before or after `operation`? There is an inherent TOCTTOU race
        // between `gone_check` and `operation`, and both before and after are
        // defensible. We call `gone_check` before `operation` to maintain
        // parity with Omicron from which this code was adapted, but we may want
        // to change this in the future.
        match (gone_check)().await {
            Ok(GoneCheckResult::Gone) => {
                return Err(RetryOperationWhileError {
                    attempt,
                    kind: RetryOperationWhileErrorKind::Gone,
                });
            }
            Ok(GoneCheckResult::StillAvailable) => {}
            Err(e) => {
                return Err(RetryOperationWhileError {
                    attempt,
                    kind: RetryOperationWhileErrorKind::GoneCheckError(e),
                });
            }
        }

        match (operation)().await {
            Ok(v) => return Ok(v),
            Err(error) => {
                if !error.is_retryable() {
                    return Err(RetryOperationWhileError {
                        attempt,
                        kind: RetryOperationWhileErrorKind::OperationError(
                            error,
                        ),
                    });
                }
                match delays.next() {
                    Some(delay) => {
                        notify(RetryNotification { attempt, error, delay });
                        tokio::time::sleep(delay).await;
                        attempt += 1;
                    }
                    None => {
                        return Err(RetryOperationWhileError {
                            attempt,
                            kind:
                                RetryOperationWhileErrorKind::RetriesExhausted(
                                    error,
                                ),
                        });
                    }
                }
            }
        }
    }
}

/// Retries a progenitor client operation indefinitely until it succeeds or
/// fails permanently.
///
/// Unlike [`retry_operation`], this function never returns a "retries
/// exhausted" error. Transient (retryable) errors are retried according to
/// the supplied [`IndefiniteBackoffParams`], indefinitely.
///
/// All non-retryable errors are returned immediately as
/// [`IndefiniteRetryOperationError`].
///
/// `notify` is called on each transient failure with the error and the delay
/// before the next attempt.
///
/// The `operation` must be idempotent.
///
/// # Examples
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// use progenitor_extras::retry::{
///     default_indefinite_retry_policy, retry_operation_indefinitely,
/// };
///
/// // In practice, replace the closure body with a progenitor client
/// // call, e.g. `|| async { client.some_endpoint().send().await }`.
/// let result = retry_operation_indefinitely(
///     default_indefinite_retry_policy(),
///     || async { Ok::<_, progenitor_client::Error<()>>(42u32) },
///     |notification| {
///         eprintln!(
///             "transient error ({:?}), retrying in {:?}",
///             notification.error, notification.delay,
///         );
///     },
/// )
/// .await;
///
/// assert_eq!(result.unwrap(), 42);
/// # }
/// ```
pub async fn retry_operation_indefinitely<T, E, N, F, Fut>(
    backoff: IndefiniteBackoffParams,
    mut operation: F,
    mut notify: N,
) -> Result<T, IndefiniteRetryOperationError<E>>
where
    N: FnMut(RetryNotification<E>),
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, progenitor_client::Error<E>>>,
{
    // The iterator is infinite by construction.
    let mut delays = backoff.build();
    let mut attempt = 1;

    loop {
        match (operation)().await {
            Ok(v) => return Ok(v),
            Err(error) => {
                if !error.is_retryable() {
                    return Err(IndefiniteRetryOperationError {
                        attempt,
                        error,
                    });
                }
                let delay = delays.next().unwrap_or_else(|| {
                    panic!(
                        "infinite backoff iterator produced a delay \
                         at attempt {attempt} (was usize::MAX exceeded?)"
                    )
                });
                notify(RetryNotification { attempt, error, delay });
                tokio::time::sleep(delay).await;
                attempt += 1;
            }
        }
    }
}

/// Retries a progenitor client operation indefinitely with an additional
/// "gone check."
///
/// Unlike [`retry_operation_while`], this function never returns a "retries
/// exhausted" error. Transient errors are retried according to the supplied
/// [`IndefiniteBackoffParams`], indefinitely.
///
/// Before each attempt, `gone_check` is called. If it returns
/// `Ok(GoneCheckResult::Gone)`, the loop is aborted with
/// [`IndefiniteRetryOperationWhileErrorKind::Gone`]. If the gone check
/// itself fails, the loop is aborted with
/// [`IndefiniteRetryOperationWhileErrorKind::GoneCheckError`].
///
/// Non-retryable errors are returned as
/// [`IndefiniteRetryOperationWhileErrorKind::OperationError`].
///
/// Gone-check errors
/// ([`IndefiniteRetryOperationWhileErrorKind::GoneCheckError`]) are treated
/// as permanent and abort the loop immediately. If the gone check itself can
/// fail transiently, handle retries within the `gone_check` closure.
///
/// `notify` is called on each transient failure with the error and the delay
/// before the next attempt.
///
/// The `operation` must be idempotent.
///
/// # Examples
///
/// ```
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// use progenitor_extras::retry::{
///     GoneCheckResult, default_indefinite_retry_policy,
///     retry_operation_while_indefinitely,
/// };
///
/// // In practice, replace these closure bodies with real client calls
/// // and a real gone check (e.g. querying whether a sled is in service).
/// let result = retry_operation_while_indefinitely(
///     default_indefinite_retry_policy(),
///     || async { Ok::<_, progenitor_client::Error<()>>(42u32) },
///     || async {
///         Ok::<_, std::convert::Infallible>(GoneCheckResult::StillAvailable)
///     },
///     |notification| {
///         eprintln!(
///             "transient error ({:?}), retrying in {:?}",
///             notification.error, notification.delay,
///         );
///     },
/// )
/// .await;
///
/// assert_eq!(result.unwrap(), 42);
/// # }
/// ```
pub async fn retry_operation_while_indefinitely<
    T,
    E,
    GoneErr,
    N,
    F,
    Fut,
    GF,
    GFut,
>(
    backoff: IndefiniteBackoffParams,
    mut operation: F,
    mut gone_check: GF,
    mut notify: N,
) -> Result<T, IndefiniteRetryOperationWhileError<E, GoneErr>>
where
    N: FnMut(RetryNotification<E>),
    F: FnMut() -> Fut,
    Fut: Future<Output = Result<T, progenitor_client::Error<E>>>,
    GF: FnMut() -> GFut,
    GFut: Future<Output = Result<GoneCheckResult, GoneErr>>,
{
    // The iterator is infinite by construction.
    let mut delays = backoff.build();
    let mut attempt = 1;

    loop {
        // Check if the target is still available before attempting
        // the operation. See the comment in `retry_operation_while` for
        // discussion of the before-vs-after ordering.
        match (gone_check)().await {
            Ok(GoneCheckResult::Gone) => {
                return Err(IndefiniteRetryOperationWhileError {
                    attempt,
                    kind: IndefiniteRetryOperationWhileErrorKind::Gone,
                });
            }
            Ok(GoneCheckResult::StillAvailable) => {}
            Err(e) => {
                return Err(IndefiniteRetryOperationWhileError {
                    attempt,
                    kind:
                        IndefiniteRetryOperationWhileErrorKind::GoneCheckError(
                            e,
                        ),
                });
            }
        }

        match (operation)().await {
            Ok(v) => return Ok(v),
            Err(error) => {
                if !error.is_retryable() {
                    return Err(IndefiniteRetryOperationWhileError {
                        attempt,
                        kind:
                            IndefiniteRetryOperationWhileErrorKind::OperationError(
                                error,
                            ),
                    });
                }
                let delay = delays.next().unwrap_or_else(|| {
                    panic!(
                        "infinite backoff iterator produced a delay \
                         at attempt {attempt} (was usize::MAX exceeded?)"
                    )
                });
                notify(RetryNotification { attempt, error, delay });
                tokio::time::sleep(delay).await;
                attempt += 1;
            }
        }
    }
}