Total Complexity | 5 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
15 | trait RetryAcquireTrait |
||
16 | { |
||
17 | /** |
||
18 | * @psalm-var positive-int |
||
19 | */ |
||
20 | private int $retryDelay = 50; |
||
21 | |||
22 | /** |
||
23 | * Returns a new instance with the specified retry delay. |
||
24 | * |
||
25 | * @param int $retryDelay Number of milliseconds between each try until specified timeout times out. |
||
26 | * By default, it is 50 milliseconds - it means that we may try to acquire lock up to 20 times per second. |
||
27 | * |
||
28 | * @return self |
||
29 | */ |
||
30 | 4 | public function withRetryDelay(int $retryDelay): self |
|
31 | { |
||
32 | 4 | if ($retryDelay < 1) { |
|
33 | 1 | throw new InvalidArgumentException( |
|
34 | 1 | "Retry delay value must be a positive number greater than zero, \"$retryDelay\" is received.", |
|
35 | 1 | ); |
|
36 | } |
||
37 | |||
38 | 3 | $new = clone $this; |
|
39 | 3 | $new->retryDelay = $retryDelay; |
|
40 | 3 | return $new; |
|
41 | } |
||
42 | |||
43 | 13 | private function retryAcquire(int $timeout, callable $callback): bool |
|
55 | } |
||
56 | } |
||
57 |