Completed
Branch master (00332a)
by Eugene
05:11
created

RetryMiddleware::constant()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
crap 6
1
<?php
2
3
/**
4
 * This file is part of the Tarantool Client package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Tarantool\Client\Middleware;
15
16
use Tarantool\Client\Exception\CommunicationFailed;
17
use Tarantool\Client\Exception\ConnectionFailed;
18
use Tarantool\Client\Exception\UnexpectedResponse;
19
use Tarantool\Client\Handler\Handler;
20
use Tarantool\Client\Request\Request;
21
use Tarantool\Client\Response;
22
23
final class RetryMiddleware implements Middleware
24
{
25
    private const DEFAULT_MAX_RETRIES = 2;
26
27
    /** @var \Closure */
28
    private $getDelayMs;
29
30
    /**
31
     * @param \Closure $getDelayMs
32
     */
33 15
    private function __construct($getDelayMs)
34
    {
35 15
        $this->getDelayMs = $getDelayMs;
36 15
    }
37
38
    public static function constant(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $intervalMs = 100) : self
39
    {
40
        return new self(static function (int $retries) use ($maxRetries, $intervalMs) {
41
            return $retries > $maxRetries ? null : $intervalMs;
42
        });
43
    }
44
45
    public static function exponential(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $baseMs = 100) : self
46
    {
47
        return new self(static function (int $retries) use ($maxRetries, $baseMs) {
48
            return $retries > $maxRetries ? null : $baseMs ** $retries;
49
        });
50
    }
51
52 9
    public static function linear(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $differenceMs = 100) : self
53
    {
54
        return new self(static function (int $retries) use ($maxRetries, $differenceMs) {
55 6
            return $retries > $maxRetries ? null : $differenceMs * $retries;
56 9
        });
57
    }
58
59 6
    public static function custom(\Closure $getDelayMs) : self
60
    {
61
        return new self(static function (int $retries, \Throwable $e) use ($getDelayMs) : ?int {
62 6
            return $getDelayMs($retries, $e);
63 6
        });
64
    }
65
66 15
    public function process(Request $request, Handler $handler) : Response
67
    {
68 15
        $retries = 0;
69
70
        do {
71
            try {
72 15
                return $handler->handle($request);
73 15
            } catch (UnexpectedResponse $e) {
74 3
                $handler->getConnection()->close();
75 3
                break;
76 12
            } catch (ConnectionFailed | CommunicationFailed $e) {
77 6
                $handler->getConnection()->close();
78 6
                goto retry;
79 9
            } catch (\Throwable $e) {
80
                retry:
81 12
                if (null === $delayMs = ($this->getDelayMs)(++$retries, $e)) {
82 6
                    break;
83
                }
84 12
                \usleep($delayMs * 1000);
85
            }
86 12
        } while (true);
87
88 9
        throw $e;
89
    }
90
}
91