Skip to content

Consider having current attempt number sent as a closure argument #25

@olekspickle

Description

@olekspickle

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<String, String> {
    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:?}");
}


Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions