Conditions | 5 |
Paths | 3 |
Total Lines | 23 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
22 | protected function retry(callable $callback) |
||
23 | { |
||
24 | $attempt = 0; |
||
25 | $outcome = new Outcome(); |
||
26 | $remainingAttempts = $this->config->attempts; |
||
27 | $backoff = Closure::fromCallable($this->config->backoff ?: function (int $attempt) { |
||
28 | return ($attempt - 1) ** 2 * 1000; |
||
29 | }); |
||
30 | |||
31 | do { |
||
32 | $attempt++; |
||
33 | $remainingAttempts--; |
||
34 | |||
35 | try { |
||
36 | return $callback($outcome); |
||
37 | } catch (Throwable $e) { |
||
38 | if ($remainingAttempts > 0) { |
||
39 | usleep($backoff($attempt) * 1000); |
||
40 | } else { |
||
41 | throw new OutOfAttemptsException($e, $outcome); |
||
42 | } |
||
43 | } |
||
44 | } while ($remainingAttempts > 0); |
||
45 | } |
||
47 |