Skip to content

Small opcode optimisation #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
20 changes: 11 additions & 9 deletions src/retry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

class FailingTooHardException extends \Exception {}

function retry($retries, callable $fn)
function retry($retries, callable $fn, callable $onError = null)
{
beginning:
try {
return $fn();
} catch (\Exception $e) {
if (!$retries) {
throw new FailingTooHardException('', 0, $e);
do
{
try {
return $fn();
} catch (\Exception $e) {}
if ($onError) {
$onError($e);
}
$retries--;
goto beginning;
}
while ($retries--);
throw new FailingTooHardException('', 0, $e);
}