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
|
|
|
private $reconnect = true; |
29
|
|
|
|
30
|
12 |
|
private function __construct(\Closure $getDelayMs) |
31
|
|
|
{ |
32
|
12 |
|
$this->getDelayMs = $getDelayMs; |
33
|
12 |
|
} |
34
|
|
|
|
35
|
2 |
|
public function withoutReconnect() : self |
36
|
|
|
{ |
37
|
2 |
|
$new = clone $this; |
38
|
2 |
|
$new->reconnect = false; |
39
|
|
|
|
40
|
2 |
|
return $new; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
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
|
2 |
|
return $retries > $maxRetries ? null : $intervalMs; |
47
|
2 |
|
}); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
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
|
|
|
return $retries > $maxRetries ? null : $baseMs ** $retries; |
54
|
|
|
}); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
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
|
4 |
|
return $retries > $maxRetries ? null : $differenceMs * $retries; |
61
|
6 |
|
}); |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
public static function custom(\Closure $getDelayMs) : self |
65
|
|
|
{ |
66
|
|
|
return new self(static function (int $retries, \Throwable $e) use ($getDelayMs) : ?int { |
67
|
4 |
|
return $getDelayMs($retries, $e); |
68
|
4 |
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
12 |
|
public function process(Request $request, Handler $handler) : Response |
72
|
|
|
{ |
73
|
12 |
|
$retries = 0; |
74
|
|
|
|
75
|
|
|
do { |
76
|
|
|
try { |
77
|
12 |
|
return $handler->handle($request); |
78
|
12 |
|
} catch (UnexpectedResponse $e) { |
79
|
2 |
|
break; |
80
|
10 |
|
} catch (\Throwable $e) { |
81
|
10 |
|
if (null === $delayMs = ($this->getDelayMs)(++$retries, $e)) { |
82
|
6 |
|
break; |
83
|
|
|
} |
84
|
10 |
|
if ($this->reconnect && ($e instanceof ConnectionFailed || $e instanceof CommunicationFailed)) { |
85
|
4 |
|
$handler->getConnection()->close(); |
86
|
|
|
} |
87
|
10 |
|
\usleep($delayMs * 1000); |
88
|
|
|
} |
89
|
10 |
|
} while (true); |
90
|
|
|
|
91
|
8 |
|
throw $e; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|