Passed
Pull Request — master (#61)
by Eugene
05:00
created

RetryMiddleware   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
eloc 26
c 2
b 0
f 0
dl 0
loc 60
ccs 22
cts 28
cp 0.7856
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A exponential() 0 4 2
A custom() 0 4 1
B process() 0 21 7
A linear() 0 4 2
A constant() 0 4 2
A __construct() 0 3 1
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
    private $getDelayMs;
28
29 10
    private function __construct(\Closure $getDelayMs)
30
    {
31 10
        $this->getDelayMs = $getDelayMs;
32 10
    }
33
34
    public static function constant(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $intervalMs = 100) : self
35
    {
36
        return new self(static function (int $retries) use ($maxRetries, $intervalMs) {
37
            return $retries > $maxRetries ? null : $intervalMs;
38
        });
39
    }
40
41
    public static function exponential(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $baseMs = 100) : self
42
    {
43
        return new self(static function (int $retries) use ($maxRetries, $baseMs) {
44
            return $retries > $maxRetries ? null : $baseMs ** $retries;
45
        });
46
    }
47
48 6
    public static function linear(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $differenceMs = 100) : self
49
    {
50
        return new self(static function (int $retries) use ($maxRetries, $differenceMs) {
51 4
            return $retries > $maxRetries ? null : $differenceMs * $retries;
52 6
        });
53
    }
54
55 4
    public static function custom(\Closure $getDelayMs) : self
56
    {
57
        return new self(static function (int $retries, \Throwable $e) use ($getDelayMs) : ?int {
58 4
            return $getDelayMs($retries, $e);
59 4
        });
60
    }
61
62 10
    public function process(Request $request, Handler $handler) : Response
63
    {
64 10
        $retries = 0;
65
66
        do {
67
            try {
68 10
                return $handler->handle($request);
69 10
            } catch (UnexpectedResponse $e) {
70 2
                break;
71 8
            } catch (\Throwable $e) {
72 8
                if (null === $delayMs = ($this->getDelayMs)(++$retries, $e)) {
73 4
                    break;
74
                }
75 8
                if ($e instanceof ConnectionFailed || $e instanceof CommunicationFailed) {
76 4
                    $handler->getConnection()->close();
77
                }
78 8
                \usleep($delayMs * 1000);
79
            }
80 8
        } while (true);
81
82 6
        throw $e;
83
    }
84
}
85