You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by ti...@apache.org on 2025/03/28 12:29:52 UTC

(opendal) 01/01: refactor!: refine RetryLayer type signature

This is an automated email from the ASF dual-hosted git repository.

tison pushed a commit to branch refine-RetryLayer-type
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit f04ea6182166a87d98519efa1a4f5eed2943e1c5
Author: tison <wa...@gmail.com>
AuthorDate: Fri Mar 28 20:29:35 2025 +0800

    refactor!: refine RetryLayer type signature
    
    Signed-off-by: tison <wa...@gmail.com>
---
 core/src/layers/retry.rs | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/core/src/layers/retry.rs b/core/src/layers/retry.rs
index b4d33c59b..f118fb126 100644
--- a/core/src/layers/retry.rs
+++ b/core/src/layers/retry.rs
@@ -116,12 +116,12 @@ use crate::*;
 /// Ok(())
 /// # }
 /// ```
-pub struct RetryLayer<I = DefaultRetryInterceptor> {
+pub struct RetryLayer<I: RetryInterceptor = DefaultRetryInterceptor> {
     builder: ExponentialBuilder,
     notify: Arc<I>,
 }
 
-impl<I> Clone for RetryLayer<I> {
+impl<I: RetryInterceptor> Clone for RetryLayer<I> {
     fn clone(&self) -> Self {
         Self {
             builder: self.builder,
@@ -130,7 +130,7 @@ impl<I> Clone for RetryLayer<I> {
     }
 }
 
-impl Default for RetryLayer {
+impl<I: RetryInterceptor> Default for RetryLayer<I> {
     fn default() -> Self {
         Self {
             builder: ExponentialBuilder::default(),
@@ -139,7 +139,7 @@ impl Default for RetryLayer {
     }
 }
 
-impl RetryLayer {
+impl<I: RetryInterceptor> RetryLayer<I> {
     /// Create a new retry layer.
     /// # Examples
     ///
@@ -164,24 +164,15 @@ impl RetryLayer {
     /// use std::time::Duration;
     ///
     /// use anyhow::Result;
-    /// use opendal::layers::RetryInterceptor;
     /// use opendal::layers::RetryLayer;
     /// use opendal::services;
     /// use opendal::Error;
     /// use opendal::Operator;
     /// use opendal::Scheme;
     ///
-    /// struct MyRetryInterceptor;
-    ///
-    /// impl RetryInterceptor for MyRetryInterceptor {
-    ///     fn intercept(&self, err: &Error, dur: Duration) {
-    ///         // do something
-    ///     }
-    /// }
-    ///
     /// let _ = Operator::new(services::Memory::default())
     ///     .expect("must init")
-    ///     .layer(RetryLayer::new().with_notify(MyRetryInterceptor))
+    ///     .layer(RetryLayer::new().with_notify(|_err, _dur| {}))
     ///     .finish();
     /// ```
     pub fn with_notify<I: RetryInterceptor>(self, notify: I) -> RetryLayer<I> {
@@ -218,7 +209,7 @@ impl RetryLayer {
 
     /// Set max_delay of current backoff.
     ///
-    /// Delay will not increasing if current delay is larger than max_delay.
+    /// Delay will not increase if current delay is larger than max_delay.
     pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
         self.builder = self.builder.with_max_delay(max_delay);
         self
@@ -261,10 +252,19 @@ pub trait RetryInterceptor: Send + Sync + 'static {
     /// # Notes
     ///
     /// The intercept must be quick and non-blocking. No heavy IO is
-    /// allowed. Otherwise the retry will be blocked.
+    /// allowed. Otherwise, the retry will be blocked.
     fn intercept(&self, err: &Error, dur: Duration);
 }
 
+impl<F> RetryInterceptor for F
+where
+    F: FnMut(&Error, Duration) + Send + Sync + 'static,
+{
+    fn intercept(&self, err: &Error, dur: Duration) {
+        self(err, dur);
+    }
+}
+
 /// The DefaultRetryInterceptor will log the retry error in warning level.
 pub struct DefaultRetryInterceptor;