RetryAcquireTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 1
b 0
f 0
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A retryAcquire() 0 12 3
A withRetryDelay() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Mutex;
6
7
use InvalidArgumentException;
8
9
use function microtime;
10
use function usleep;
11
12
/**
13
 * Allows retrying acquire with a certain timeout.
14
 */
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
44
    {
45 13
        $start = microtime(true);
46
47
        do {
48 13
            if ($callback()) {
49 12
                return true;
50
            }
51 6
            usleep($this->retryDelay * 1000);
52 6
        } while (microtime(true) - $start < $timeout);
53
54 5
        return false;
55
    }
56
}
57