1 | <?php |
||
8 | class ExponentialBackoff |
||
9 | { |
||
10 | const MAX_DELAY_MICROSECONDS = 120000000; |
||
11 | |||
12 | /** |
||
13 | * @var int |
||
14 | */ |
||
15 | private $retries; |
||
16 | |||
17 | /** |
||
18 | * @var callable |
||
19 | */ |
||
20 | private $retryFunction; |
||
21 | |||
22 | /** |
||
23 | * @var callable |
||
24 | */ |
||
25 | private $delayFunction; |
||
26 | |||
27 | /** |
||
28 | * @param int $retries [optional] Number of retries for a failed request. |
||
29 | * @param callable $retryFunction [optional] returns bool for whether or not to retry |
||
30 | */ |
||
31 | public function __construct($retries = null, callable $retryFunction = null) |
||
39 | |||
40 | /** |
||
41 | * Executes the retry process. |
||
42 | * |
||
43 | * @param callable $function |
||
44 | * @param array $arguments [optional] |
||
45 | * @return mixed |
||
46 | * @throws \Exception The last exception caught while retrying. |
||
47 | */ |
||
48 | public function execute(callable $function, array $arguments = []) |
||
49 | { |
||
50 | $delayFunction = $this->delayFunction; |
||
51 | $retryAttempt = 0; |
||
52 | $exception = null; |
||
53 | |||
54 | while (true) { |
||
55 | try { |
||
56 | return call_user_func_array($function, $arguments); |
||
57 | } catch (\Exception $exception) { |
||
58 | if ($this->retryFunction) { |
||
59 | if (! call_user_func($this->retryFunction, $exception)) { |
||
60 | throw $exception; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | if ($exception->getCode() == 401 || $exception->getCode() == 403) { |
||
65 | break; |
||
66 | } |
||
67 | |||
68 | if ($retryAttempt >= $this->retries) { |
||
69 | break; |
||
70 | } |
||
71 | |||
72 | $delayFunction($this->calculateDelay($retryAttempt)); |
||
73 | $retryAttempt++; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | throw $exception; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param callable $delayFunction |
||
82 | * @return void |
||
83 | */ |
||
84 | public function setDelayFunction(callable $delayFunction) |
||
88 | |||
89 | /** |
||
90 | * Calculates exponential delay. |
||
91 | * |
||
92 | * @param int $attempt |
||
93 | * @return int |
||
94 | */ |
||
95 | private function calculateDelay($attempt) |
||
102 | } |
||
103 |