1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ez\DbLinker\RetryStrategy; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use stdClass; |
7
|
|
|
use Doctrine\DBAL\Driver\Connection; |
8
|
|
|
use Ez\DbLinker\Driver\Connection\MasterSlavesConnection; |
9
|
|
|
use Ez\DbLinker\Driver\Connection\RetryConnection; |
10
|
|
|
|
11
|
|
|
trait RetryStrategy |
12
|
|
|
{ |
13
|
|
|
private $retryLimit; |
14
|
|
|
private $nestingLimit; |
15
|
|
|
|
16
|
|
|
public function __construct($retryLimit = INF, $nestingLimit = 1) |
17
|
|
|
{ |
18
|
|
|
$this->retryLimit = $retryLimit; |
19
|
|
|
$this->nestingLimit = $nestingLimit; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function shouldRetry(Exception $exception, Callable $callable, $context) { |
|
|
|
|
23
|
|
|
if (!$this->canRetry($context)) { |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
$this->nestingLimit--; |
27
|
|
|
$strategy = $this->errorCodeStrategy($this->errorCode($exception)); |
28
|
|
|
$res = $this->applyStrategy($strategy, $context); |
29
|
|
|
$this->nestingLimit++; |
30
|
|
|
return $res; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function retryLimit() |
34
|
|
|
{ |
35
|
|
|
return $this->retryLimit > 0 ? (int) $this->retryLimit : 0; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function canRetry($context) |
39
|
|
|
{ |
40
|
|
|
return $this->retryLimit > 0 && |
41
|
|
|
$this->nestingLimit > 0 && |
42
|
|
|
(!$context instanceof RetryConnection || $context->transactionLevel() === 0); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function errorCodeStrategy($errorCode) |
46
|
|
|
{ |
47
|
|
|
$strategy = (object) [ |
48
|
|
|
'retry' => true, |
49
|
|
|
'wait' => 0, |
50
|
|
|
'changeServer' => false, |
51
|
|
|
'reconnect' => false, |
52
|
|
|
]; |
53
|
|
|
$errorCodeStrategies = $this->errorCodeStrategies(); |
54
|
|
|
if (array_key_exists($errorCode, $errorCodeStrategies)) { |
55
|
|
|
foreach ($errorCodeStrategies[$errorCode] as $behavior => $value) { |
56
|
|
|
$strategy->$behavior = $value; |
57
|
|
|
} |
58
|
|
|
return $strategy; |
59
|
|
|
} |
60
|
|
|
return (object) ['retry' => false]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function applyStrategy(stdClass $strategy, $context) { |
64
|
|
|
if ($strategy->retry === false || !$this->changeServer($strategy, $context)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
sleep($strategy->wait); |
68
|
|
|
$this->reconnect($strategy, $context); |
69
|
|
|
$this->retryLimit--; |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function changeServer(stdClass $strategy, $context) |
74
|
|
|
{ |
75
|
|
|
if (!$strategy->changeServer) { |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
if (!$context instanceof RetryConnection) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
$wrappedConnection = $context->wrappedConnection(); |
82
|
|
|
if ($wrappedConnection instanceof MasterSlavesConnection && !$wrappedConnection->isConnectedToMaster()) { |
83
|
|
|
$wrappedConnection->disableCurrentSlave(); |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function reconnect(stdClass $strategy, $context) |
90
|
|
|
{ |
91
|
|
|
if ($strategy->reconnect && $context instanceof Connection) { |
92
|
|
|
$context->close(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected abstract function errorCodeStrategies(); |
97
|
|
|
protected abstract function errorCode(Exception $exception); |
98
|
|
|
} |
99
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.