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
|
|
|
private const MAX_RETRIES_LIMIT = 100; |
27
|
|
|
|
28
|
|
|
/** @var \Closure */ |
29
|
|
|
private $getDelayMs; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \Closure $getDelayMs |
33
|
|
|
*/ |
34
|
18 |
|
private function __construct($getDelayMs) |
35
|
|
|
{ |
36
|
18 |
|
$this->getDelayMs = $getDelayMs; |
37
|
18 |
|
} |
38
|
|
|
|
39
|
|
|
public static function constant(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $intervalMs = 100) : self |
40
|
|
|
{ |
41
|
|
|
return new self(static function (int $retries) use ($maxRetries, $intervalMs) { |
42
|
|
|
return $retries > $maxRetries ? null : $intervalMs; |
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function exponential(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $baseMs = 100) : self |
47
|
|
|
{ |
48
|
|
|
return new self(static function (int $retries) use ($maxRetries, $baseMs) { |
49
|
|
|
return $retries > $maxRetries ? null : $baseMs ** $retries; |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
public static function linear(int $maxRetries = self::DEFAULT_MAX_RETRIES, int $differenceMs = 100) : self |
54
|
|
|
{ |
55
|
9 |
|
return new self(static function (int $retries) use ($maxRetries, $differenceMs) { |
56
|
6 |
|
return $retries > $maxRetries ? null : $differenceMs * $retries; |
57
|
9 |
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
9 |
|
public static function custom(\Closure $getDelayMs) : self |
61
|
|
|
{ |
62
|
9 |
|
return new self(static function (int $retries, \Throwable $e) use ($getDelayMs) : ?int { |
63
|
9 |
|
return $getDelayMs($retries, $e); |
64
|
9 |
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
18 |
|
public function process(Request $request, Handler $handler) : Response |
68
|
|
|
{ |
69
|
18 |
|
$retries = 0; |
70
|
|
|
|
71
|
|
|
do { |
72
|
|
|
try { |
73
|
18 |
|
return $handler->handle($request); |
74
|
18 |
|
} catch (UnexpectedResponse $e) { |
75
|
3 |
|
$handler->getConnection()->close(); |
76
|
3 |
|
break; |
77
|
15 |
|
} catch (ConnectionFailed | CommunicationFailed $e) { |
78
|
9 |
|
$handler->getConnection()->close(); |
79
|
9 |
|
goto retry; |
80
|
9 |
|
} catch (\Throwable $e) { |
81
|
|
|
retry: |
82
|
15 |
|
if (self::MAX_RETRIES_LIMIT === $retries) { |
83
|
3 |
|
break; |
84
|
|
|
} |
85
|
15 |
|
if (null === $delayMs = ($this->getDelayMs)(++$retries, $e)) { |
86
|
6 |
|
break; |
87
|
|
|
} |
88
|
15 |
|
\usleep($delayMs * 1000); |
89
|
|
|
} |
90
|
15 |
|
} while (true); |
91
|
|
|
|
92
|
12 |
|
throw $e; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|