Open w41ter opened 7 months ago
Here is a simple solution:
pub trait Retryable<T> {
async fn retry(&self, retry_state: RetryState) -> Result<T>;
}
impl<F, Future, T> Retryable<T> for F
where
F: Fn() -> Future,
Future: std::future::Future<Output = Result<T>>,
{
async fn retry(&self, retry_state: RetryState) -> Result<T> {
let mut retry_state = retry_state;
loop {
match self().await {
Ok(result) => return Ok(result),
Err(err) => retry_state.retry(err).await?,
}
}
}
}
// usage
pub async fn acquire_shard(&mut self, desc: &MoveShardDesc) -> Result<()> {
{ || async { self.group_client().acquire_shard(desc).await } }
.retry(RetryState::default())
.await
}
Here is the structure to use
RetryState
, maybe it can be optimized here: