Skip to content

Commit

Permalink
added custom delay function for the retry middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
bcismariu committed Sep 28, 2020
1 parent 8c305da commit 9e26d55
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ public function dontQueue()
return $this;
}

public function retries(int $allowed)
public function retries(int $allowed, callable $delay = null)
{
$this->middleware['retries'] = Middleware::retries($allowed);
$this->middleware['retries'] = Middleware::retries($allowed, $delay);
return $this;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Middleware/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ function (ResponseInterface $response) use ($mode) {
);
}

public static function retries(int $allowed)
public static function retries(int $allowed, callable $delay = null)
{
return GuzzleMiddleware::retry(
function ($retries, $request, ResponseInterface $response) use ($allowed) {
return (($response->getStatusCode() >= 400)
&& ($retries < $allowed));
}
},
$delay
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testItRetries()
new Response(200, [], '{}'),
]);

$client->retries(7)->call();
$client->retries(7, function () { return 100; })->call();
}

public function testItFailsAfterRetries()
Expand All @@ -149,7 +149,7 @@ public function testItFailsAfterRetries()
new Response(200, [], '{}'),
]);

$client->retries(2)->call();
$client->retries(2, function () { return 100; })->call();
}

/**
Expand Down

0 comments on commit 9e26d55

Please sign in to comment.