caseyamcl / guzzle_retry_middleware

Middleware for Guzzle v6/7+ that automatically retries HTTP requests on 429, 503 responses.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Enhancement] Option to stop retrying after x seconds

SpartakusMd opened this issue · comments

Is there a way to specify a parameter of a max execution time for request?

Detailed description

Example I want to allow at most 30 seconds per request. This means that if we are in the middle of a retry it should stop. The problem is that each request/retry can take an indefinite amount of time, so we cannot hardcode a specific number of retries with a specific interval.

Possible implementation

Track start time of the request, and calculate a timeout per retry 🤔

@SpartakusMd apologies, for the delay (pun intended). yadda yadda work, life, etc. you know the drill...

Anyhow, if I understand your inquiry correctly, I think this is out of the scope of this library.

Setting the timeout per request is actually built into Guzzle via the timeout parameter.

If I have misunderstood your request, please provide more details.

Here's the idea. We allow for 5 retries with retry_on_timeout enabled. We can receive timeout response up to 2 minutes. What I would like is a way to allow no more than 5 minutes to get the response with all the retries. Example:

  • Fetching /products endpoint with 5 retries allowed
  • Received timeout after 2m
  • Received timeout after 30s
  • Received timeout after 2m
  • Here I would like to have the max allowed time to get the response calculated to 30 seconds (5m - 2m - 30s - 2m = 30s)

Currently I didn't see an option / a way to achieve this logic.

I just added a new option that I think covers this use-case: give_up_after_secs:

# This will fail when either the number of seconds is reached, or the number of retry attempts is reached, whichever
# comes first 
$response = $client->get('/some-path', [
   'max_retry_attempts' => 10 
   'give_up_after_secs' => 10
]);

See more details in the README for version 2.7