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
|
|
|
|