Skip to content

Instantly share code, notes, and snippets.

@ryun
Created October 19, 2016 23:31
Show Gist options
  • Save ryun/59f899aa81277ce51642dfc748293512 to your computer and use it in GitHub Desktop.
Save ryun/59f899aa81277ce51642dfc748293512 to your computer and use it in GitHub Desktop.
Retry without using goto
<?php
if (! function_exists('retry')) {
function retry(callable $callback, $times = 3, $sleep = 3)
{
$attempts = 0;
do {
try
{
return $callback();
} catch (\Exception $e) {
$attempts++;
if ($sleep) {
usleep($sleep * 1000);
}
continue;
}
break;
} while($attempts < $times);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment