TrueLayer / reqwest-middleware

Wrapper around reqwest to allow for client middleware chains.
Apache License 2.0
257 stars 78 forks source link

Regex whitelist feature #151

Closed yourlogarithm closed 4 months ago

yourlogarithm commented 4 months ago

Adds a regex set matching feature so that if the request fails and the url matches against the provided whitelist pattern - it will be retried, otherwise not.

Example:

let whitelist = regex::RegexSet::new(vec!["foo", "bar"]).unwrap();
let reqwest_client = Client::builder().build().unwrap();
let client = ClientBuilder::new(reqwest_client)
    .with(RetryTransientMiddleware::new_with_policy(...).with_whitelist(whitelist))
    .build();

let resp = client
        .get("127.0.0.1/foo")
        .send()
        .await
        .expect("call failed"); // Will be retried if failed according to policy

let resp = client
        .get("127.0.0.1/bar")
        .send()
        .await
        .expect("call failed"); // Will be retried if failed according to policy

 let resp = client
        .get("127.0.0.1/something")
        .send()
        .await
        .expect("call failed");  // Will NOT be retried because the url does not match regex
LukeMathWalker commented 4 months ago

This feels way too specific to be a first-class feature in reqwest-retry.
But it points at an underlying issue: you can't implement this on your own because RetryableStrategy doesn't take into account the request. That's what we should be reasoning about imo.