EmbarkStudios / EmbarkStudios/tryhard

Consider having current attempt number sent as a closure argument

Open
#25 0 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
248
Forks
11
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
Sometimes you want to know what is going on and more detailed logs can be useful

**Describe the solution you'd like**
A current attempt passed as a closure argument:
```
tryhard::retry_fn(|n| ...)

```

**Describe alternatives you've considered**
Of course you can always have `let mut attempt = 0`, but it is not really idiomatic, is it? :)

**Additional context**
Illustrative example:
```

use tryhard; // 0.5.0
use std::time::Duration;

async fn call(n: u8) -> Result {
if n > 5 {
Ok("Ok, move along".to_string())
}else {
Err("You shall not pass".to_string())
}
}

#[tokio::main]
async fn main() {
let mut attempt = 0;
let s = tryhard::retry_fn(|| async {
attempt += 1;
println!("Once more into the fray, attempt: {attempt}");
call(attempt).await
})
.retries(6)
.exponential_backoff(Duration::from_secs(1))
.max_delay(Duration::from_secs(10))
.await;

println!("{s:?}");
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.