Skip to content

Instantly share code, notes, and snippets.

@leedavis81
Last active August 29, 2015 14:06
Show Gist options
  • Save leedavis81/e09e49740e4746049ebc to your computer and use it in GitHub Desktop.
Save leedavis81/e09e49740e4746049ebc to your computer and use it in GitHub Desktop.
<?php
function retry($retries, callable $fn)
{
while (true)
{
try {
return $fn();
} catch (\Exception $e) {
if (!$retries) {
throw new \Exception('', 0, $e);
}
$retries--;
}
}
}
$fn = function() {
if ($GLOBALS['counter'] > 0)
{
$GLOBALS['counter']--;
throw new \Exception('Try again');
}
return true;
};
$start = microtime(true);
try {
$GLOBALS['counter'] = 999999;
retry(1000000, $fn);
} catch (\Exception $e) {}
echo 'Retry finished in: ' . (microtime(true) -$start) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment