pub struct RetryLayer<I: RetryInterceptor = DefaultRetryInterceptor> { /* private fields */ }
Expand description
Add retry for temporary failed operations.
§Notes
This layer will retry failed operations when Error::is_temporary
returns true. If operation still failed, this layer will set error to
Persistent
which means error has been retried.
§Panics
While retrying Reader
or Writer
operations, please make sure either:
- All futures generated by
Reader::read
orWriter::close
are resolved toReady
. - Or, won’t call any
Reader
orWriter
methods after retry returns final error.
Otherwise, RetryLayer
could panic while hitting in bad states.
For example, while composing RetryLayer
with TimeoutLayer
. The order of layer is sensitive.
let op = Operator::new(services::Memory::default())?
// This is fine, since timeout happen during retry.
.layer(TimeoutLayer::new().with_io_timeout(Duration::from_nanos(1)))
.layer(RetryLayer::new())
// This is wrong. Since timeout layer will drop future, leaving retry layer in a bad state.
.layer(TimeoutLayer::new().with_io_timeout(Duration::from_nanos(1)))
.finish();
Ok(())
§Examples
let _ = Operator::new(services::Memory::default())?
.layer(RetryLayer::new())
.finish();
Ok(())
§Customize retry interceptor
RetryLayer accepts RetryInterceptor
to allow users to customize
their own retry interceptor logic.
struct MyRetryInterceptor;
impl RetryInterceptor for MyRetryInterceptor {
fn intercept(&self, err: &Error, dur: Duration) {
// do something
}
}
let _ = Operator::new(services::Memory::default())?
.layer(RetryLayer::new().with_notify(MyRetryInterceptor))
.finish();
Ok(())
Implementations§
Source§impl RetryLayer
impl RetryLayer
Sourcepub fn new() -> RetryLayer
pub fn new() -> RetryLayer
Create a new retry layer.
§Examples
use anyhow::Result;
use opendal::layers::RetryLayer;
use opendal::services;
use opendal::Operator;
use opendal::Scheme;
let _ = Operator::new(services::Memory::default())
.expect("must init")
.layer(RetryLayer::new());
Source§impl<I: RetryInterceptor> RetryLayer<I>
impl<I: RetryInterceptor> RetryLayer<I>
Sourcepub fn with_notify<NI: RetryInterceptor>(self, notify: NI) -> RetryLayer<NI>
pub fn with_notify<NI: RetryInterceptor>(self, notify: NI) -> RetryLayer<NI>
Set the retry interceptor as new notify.
use opendal::layers::RetryLayer;
use opendal::services;
use opendal::Operator;
fn notify(_err: &opendal::Error, _dur: std::time::Duration) {}
let _ = Operator::new(services::Memory::default())
.expect("must init")
.layer(RetryLayer::new().with_notify(notify))
.finish();
Sourcepub fn with_jitter(self) -> Self
pub fn with_jitter(self) -> Self
Set jitter of current backoff.
If jitter is enabled, ExponentialBackoff will add a random jitter in `[0, min_delay) to current delay.
Sourcepub fn with_factor(self, factor: f32) -> Self
pub fn with_factor(self, factor: f32) -> Self
Sourcepub fn with_min_delay(self, min_delay: Duration) -> Self
pub fn with_min_delay(self, min_delay: Duration) -> Self
Set min_delay of current backoff.
Sourcepub fn with_max_delay(self, max_delay: Duration) -> Self
pub fn with_max_delay(self, max_delay: Duration) -> Self
Set max_delay of current backoff.
Delay will not increase if current delay is larger than max_delay.
Sourcepub fn with_max_times(self, max_times: usize) -> Self
pub fn with_max_times(self, max_times: usize) -> Self
Set max_times of current backoff.
Backoff will return None
if max times is reaching.