Skip to content

Commit 3bbec32

Browse files
authored
refactor: refine RetryLayer type signature (#5905)
* refactor!: refine RetryLayer type signature Signed-off-by: tison <wander4096@gmail.com> * fixup rustc cannot cast noncapture closure to Fn with static lifetime Signed-off-by: tison <wander4096@gmail.com> --------- Signed-off-by: tison <wander4096@gmail.com>
1 parent a0dd080 commit 3bbec32

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

core/src/layers/retry.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ use crate::*;
116116
/// Ok(())
117117
/// # }
118118
/// ```
119-
pub struct RetryLayer<I = DefaultRetryInterceptor> {
119+
pub struct RetryLayer<I: RetryInterceptor = DefaultRetryInterceptor> {
120120
builder: ExponentialBuilder,
121121
notify: Arc<I>,
122122
}
123123

124-
impl<I> Clone for RetryLayer<I> {
124+
impl<I: RetryInterceptor> Clone for RetryLayer<I> {
125125
fn clone(&self) -> Self {
126126
Self {
127127
builder: self.builder,
@@ -154,37 +154,27 @@ impl RetryLayer {
154154
/// .expect("must init")
155155
/// .layer(RetryLayer::new());
156156
/// ```
157-
pub fn new() -> Self {
157+
pub fn new() -> RetryLayer {
158158
Self::default()
159159
}
160+
}
160161

162+
impl<I: RetryInterceptor> RetryLayer<I> {
161163
/// Set the retry interceptor as new notify.
162164
///
163165
/// ```no_run
164-
/// use std::time::Duration;
165-
///
166-
/// use anyhow::Result;
167-
/// use opendal::layers::RetryInterceptor;
168166
/// use opendal::layers::RetryLayer;
169167
/// use opendal::services;
170-
/// use opendal::Error;
171168
/// use opendal::Operator;
172-
/// use opendal::Scheme;
173-
///
174-
/// struct MyRetryInterceptor;
175169
///
176-
/// impl RetryInterceptor for MyRetryInterceptor {
177-
/// fn intercept(&self, err: &Error, dur: Duration) {
178-
/// // do something
179-
/// }
180-
/// }
170+
/// fn notify(_err: &opendal::Error, _dur: std::time::Duration) {}
181171
///
182172
/// let _ = Operator::new(services::Memory::default())
183173
/// .expect("must init")
184-
/// .layer(RetryLayer::new().with_notify(MyRetryInterceptor))
174+
/// .layer(RetryLayer::new().with_notify(notify))
185175
/// .finish();
186176
/// ```
187-
pub fn with_notify<I: RetryInterceptor>(self, notify: I) -> RetryLayer<I> {
177+
pub fn with_notify<NI: RetryInterceptor>(self, notify: NI) -> RetryLayer<NI> {
188178
RetryLayer {
189179
builder: self.builder,
190180
notify: Arc::new(notify),
@@ -218,7 +208,7 @@ impl RetryLayer {
218208

219209
/// Set max_delay of current backoff.
220210
///
221-
/// Delay will not increasing if current delay is larger than max_delay.
211+
/// Delay will not increase if current delay is larger than max_delay.
222212
pub fn with_max_delay(mut self, max_delay: Duration) -> Self {
223213
self.builder = self.builder.with_max_delay(max_delay);
224214
self
@@ -261,10 +251,19 @@ pub trait RetryInterceptor: Send + Sync + 'static {
261251
/// # Notes
262252
///
263253
/// The intercept must be quick and non-blocking. No heavy IO is
264-
/// allowed. Otherwise the retry will be blocked.
254+
/// allowed. Otherwise, the retry will be blocked.
265255
fn intercept(&self, err: &Error, dur: Duration);
266256
}
267257

258+
impl<F> RetryInterceptor for F
259+
where
260+
F: Fn(&Error, Duration) + Send + Sync + 'static,
261+
{
262+
fn intercept(&self, err: &Error, dur: Duration) {
263+
self(err, dur);
264+
}
265+
}
266+
268267
/// The DefaultRetryInterceptor will log the retry error in warning level.
269268
pub struct DefaultRetryInterceptor;
270269

0 commit comments

Comments
 (0)