pub async fn retry<Fut, FutureFactoryFn, R, E>(
address_list: &AddressList,
settings: RequestSettings,
future_factory_fn: FutureFactoryFn,
) -> ExecutionResult<R, E>where
Fut: Future<Output = ExecutionResult<R, E>>,
FutureFactoryFn: FnMut(RequestSettings) -> Fut,
E: CanRetry + Display + Debug,
Expand description
Retry the provided closure.
This function is used to retry async code. It takes into account number of retries already executed by lower layers and stops retrying once the maximum number of retries is reached.
The settings
should contain maximum number of retries that should be executed. In case of failure, total number of
requests sent is expected to be at least settings.retries + 1
(initial request + retries
configured in settings).
The actual number of requests sent can be higher, as the lower layers can retry the request multiple times.
future_factory_fn
should be a FnMut()
closure that returns a future that should be retried.
It takes RequestSettings
as an argument and returns [ExecutionResult
].
Retry mechanism can change RequestSettings
between invocations of the future_factory_fn
closure
to limit the number of retries for lower layers.
§Parameters
address_list
- list of addresses to be used for the requests.settings
- global settings with any request-specific settings overrides applied.future_factory_fn
- closure that returns a future that should be retried. It should takeRequestSettings
as an argument and return [ExecutionResult
].
§Returns
Returns future that resolves to [ExecutionResult
].
§Example
async fn retry_test_function(settings: RequestSettings) -> ExecutionResult<(), dash_sdk::Error> {
// do something
Err(ExecutionError {
inner: Error::StaleNode(StaleNodeError::Height{
expected_height: 10,
received_height: 3,
tolerance_blocks: 1,
}),
retries: 0,
address: None,
})
}
#[tokio::main]
async fn main() {
let address_list = rs_dapi_client::AddressList::default();
let global_settings = RequestSettings::default();
dash_sdk::sync::retry(&address_list, global_settings, retry_test_function).await.expect_err("should fail");
}
§Troubleshooting
Compiler error: no method named retry found for closure
:
- ensure returned value is [
ExecutionResult
]., - consider adding
.await
at the end of the closure.
§See also
- [
::backon
] crate that is used by this function.