Passed
Pull Request — master (#61)
by Eugene
06:11
created

RetryMiddleware::process()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 8

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 21
ccs 4
cts 4
cp 1
rs 8.4444
cc 8
nc 5
nop 2
crap 8
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
    public const DEFAULT_MAX_RETRIES = 3;
26
27 10
    private $getDelayMs;
28
    private $reconnect = true;
29 10
30 10
    private function __construct(\Closure $getDelayMs)
31
    {
32
        $this->getDelayMs = $getDelayMs;
33
    }
34
35
    public function withoutReconnect() : self
36
    {
37
        $new = clone $this;
38
        $new->reconnect = false;
39
40
        return $new;
41
    }
42
43
    public static function constant(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $intervalMs = 100) : self
44
    {
45
        return new self(static function (int $retries) use ($maxRetries, $intervalMs) {
46 6
            return $retries > $maxRetries ? null : $intervalMs;
47
        });
48
    }
49 4
50 6
    public static function exponential(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $baseMs = 100) : self
51
    {
52
        return new self(static function (int $retries) use ($maxRetries, $baseMs) {
53 4
            return $retries > $maxRetries ? null : $baseMs ** $retries;
54
        });
55
    }
56 4
57 4
    public static function linear(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $differenceMs = 100) : self
58
    {
59
        return new self(static function (int $retries) use ($maxRetries, $differenceMs) {
60 10
            return $retries > $maxRetries ? null : $differenceMs * $retries;
61
        });
62 10
    }
63
64
    public static function custom(\Closure $getDelayMs) : self
65
    {
66 10
        return new self(static function (int $retries, \Throwable $e) use ($getDelayMs) : ?int {
67 10
            return $getDelayMs($retries, $e);
68 2
        });
69 8
    }
70 8
71 4
    public function process(Request $request, Handler $handler) : Response
72
    {
73 8
        $retries = 0;
74
75 8
        do {
76
            try {
77 6
                return $handler->handle($request);
78
            } catch (UnexpectedResponse $e) {
79
                break;
80
            } catch (\Throwable $e) {
81
                if (null === $delayMs = ($this->getDelayMs)(++$retries, $e)) {
82
                    break;
83
                }
84
                if ($this->reconnect && ($e instanceof ConnectionFailed || $e instanceof CommunicationFailed)) {
85
                    $handler->getConnection()->close();
86
                }
87
                \usleep($delayMs * 1000);
88
            }
89
        } while (true);
90
91
        throw $e;
92
    }
93
}
94