retries = $retries !== null ? (int) $retries : 3; $this->retryFunction = $retryFunction; $this->retryListener = $retryListener; // @todo revisit this approach // @codeCoverageIgnoreStart $this->delayFunction = static function ($delay) { usleep($delay); }; // @codeCoverageIgnoreEnd } /** * Executes the retry process. * * @param callable $function * @param array $arguments [optional] * @return mixed * @throws \Exception The last exception caught while retrying. */ public function execute(callable $function, array $arguments = []) { $delayFunction = $this->delayFunction; $calcDelayFunction = $this->calcDelayFunction ?: [$this, 'calculateDelay']; $retryAttempt = 0; $exception = null; while (true) { try { return call_user_func_array($function, $arguments); } catch (\Exception $exception) { if ($this->retryFunction) { if (!call_user_func($this->retryFunction, $exception, $retryAttempt)) { throw $exception; } } if ($retryAttempt >= $this->retries) { break; } $delayFunction($calcDelayFunction($retryAttempt)); $retryAttempt++; if ($this->retryListener) { // Developer can modify the $arguments using the retryListener // callback. call_user_func_array( $this->retryListener, [$exception, $retryAttempt, &$arguments] ); } } } throw $exception; } /** * If not set, defaults to using `usleep`. * * @param callable $delayFunction * @return void */ public function setDelayFunction(callable $delayFunction) { $this->delayFunction = $delayFunction; } /** * If not set, defaults to using * {@see \Google\Cloud\Core\ExponentialBackoff::calculateDelay()}. * * @param callable $calcDelayFunction * @return void */ public function setCalcDelayFunction(callable $calcDelayFunction) { $this->calcDelayFunction = $calcDelayFunction; } /** * Calculates exponential delay. * * @param int $attempt The attempt number used to calculate the delay. * @return int */ public static function calculateDelay($attempt) { return min( mt_rand(0, 1000000) + (pow(2, $attempt) * 1000000), self::MAX_DELAY_MICROSECONDS ); } }