Created
October 19, 2016 23:31
-
-
Save ryun/59f899aa81277ce51642dfc748293512 to your computer and use it in GitHub Desktop.
Retry without using goto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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