|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\EntityManager\RetryConnection; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
8
|
|
|
*/ |
|
9
|
|
|
class ShouldConnectionByRetried |
|
10
|
|
|
{ |
|
11
|
|
|
public const KEY_USE_RECONNECT = 'should-reconnect'; |
|
12
|
|
|
public const KEY_RECONNECT_ATTEMPTS = 'reconnect-attempts'; |
|
13
|
|
|
public const KEY_RECONNECT_TIMEOUT = 'reconnect-timeout'; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
private $allowedAttempts; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
private $timeout; |
|
22
|
|
|
|
|
23
|
|
|
private function __construct(int $allowedAttempts, int $timeout) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->allowedAttempts = $allowedAttempts; |
|
26
|
|
|
$this->timeout = $timeout; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function createWithConfigParams(array $config): ShouldConnectionByRetried |
|
30
|
|
|
{ |
|
31
|
|
|
$allowedAttempts = $config['driverOptions'][self::KEY_RECONNECT_ATTEMPTS] ?? 3; |
|
32
|
|
|
$timeout = $config['driverOptions'][self::KEY_RECONNECT_TIMEOUT] ?? 5; |
|
33
|
|
|
|
|
34
|
|
|
return new self($allowedAttempts, $timeout); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function checkAndSleep( |
|
38
|
|
|
\Exception $exception, |
|
39
|
|
|
int $transactionNestingLevel, |
|
40
|
|
|
int $numberOfAttempts, |
|
41
|
|
|
bool $ignoreTransactionLevel = false |
|
42
|
|
|
): bool { |
|
43
|
|
|
switch (true) { |
|
44
|
|
|
case $this->attemptsHaveExceededLimit($numberOfAttempts): |
|
45
|
|
|
case $this->transactionLevelWillCauseProblems($ignoreTransactionLevel, $transactionNestingLevel): |
|
46
|
|
|
case $this->exceptionIsNotRelatedToLostConnection($exception): |
|
47
|
|
|
$retry = false; |
|
48
|
|
|
break; |
|
49
|
|
|
default: |
|
50
|
|
|
sleep($this->timeout); |
|
51
|
|
|
$retry = true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $retry; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
private function attemptsHaveExceededLimit(int $numberOfAttempts): bool |
|
59
|
|
|
{ |
|
60
|
|
|
return $numberOfAttempts > $this->allowedAttempts; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function exceptionIsNotRelatedToLostConnection(\Exception $exception): bool |
|
64
|
|
|
{ |
|
65
|
|
|
$message = $exception->getMessage(); |
|
66
|
|
|
switch (true) { |
|
67
|
|
|
case \ts\stringContains($message, 'MySQL server has gone away'): |
|
68
|
|
|
case \ts\stringContains($message, 'Lost connection to MySQL server during query'): |
|
69
|
|
|
case \ts\stringContains($message, 'Connection timed out'): |
|
70
|
|
|
return false; |
|
71
|
|
|
default: |
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
private function transactionLevelWillCauseProblems(bool $ignoreTransactionLevel, int $transactionNestingLevel): bool |
|
78
|
|
|
{ |
|
79
|
|
|
if ($ignoreTransactionLevel === true) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $transactionNestingLevel === 0; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|