| 1 | <?php |
||
| 2 | |||
| 3 | namespace AlibabaCloud\Client\Request\Traits; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use AlibabaCloud\Client\Support\Stringy; |
||
| 7 | use AlibabaCloud\Client\Result\Result; |
||
| 8 | use AlibabaCloud\Client\Filter\ClientFilter; |
||
| 9 | use AlibabaCloud\Client\Exception\ClientException; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Trait RetryTrait |
||
| 13 | * |
||
| 14 | * @package AlibabaCloud\Client\Request\Traits |
||
| 15 | */ |
||
| 16 | trait RetryTrait |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * Server Retry Times |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $serverRetry = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Server Retry Strings |
||
| 27 | * |
||
| 28 | * @var string[] |
||
| 29 | */ |
||
| 30 | private $serverRetryStrings = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Server Retry Codes |
||
| 34 | * |
||
| 35 | * @var int[] |
||
| 36 | */ |
||
| 37 | private $serverRetryStatusCodes = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Client Retry Times |
||
| 41 | * |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | private $clientRetry = 0; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Client Retry Strings |
||
| 48 | * |
||
| 49 | * @var string[] |
||
| 50 | */ |
||
| 51 | private $clientRetryStrings = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Client Retry Codes |
||
| 55 | * |
||
| 56 | * @var int[] |
||
| 57 | */ |
||
| 58 | private $clientRetryStatusCodes = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param int $times |
||
| 62 | * @param array $strings |
||
| 63 | * @param array $statusCodes |
||
| 64 | * |
||
| 65 | * @return $this |
||
| 66 | * @throws ClientException |
||
| 67 | */ |
||
| 68 | 3 | public function retryByServer($times, array $strings, array $statusCodes = []) |
|
| 69 | { |
||
| 70 | 3 | $this->serverRetry = ClientFilter::retry($times); |
|
|
0 ignored issues
–
show
|
|||
| 71 | 3 | $this->serverRetryStrings = $strings; |
|
| 72 | 3 | $this->serverRetryStatusCodes = $statusCodes; |
|
| 73 | |||
| 74 | 3 | return $this; |
|
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param int $times |
||
| 79 | * @param array $strings |
||
| 80 | * @param array $codes |
||
| 81 | * |
||
| 82 | * @return $this |
||
| 83 | * @throws ClientException |
||
| 84 | */ |
||
| 85 | 3 | public function retryByClient($times, array $strings, array $codes = []) |
|
| 86 | { |
||
| 87 | 3 | $this->clientRetry = ClientFilter::retry($times); |
|
|
0 ignored issues
–
show
The property
$clientRetry was declared of type integer, but AlibabaCloud\Client\Filt...ntFilter::retry($times) is of type string. Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
| 88 | 3 | $this->clientRetryStrings = $strings; |
|
| 89 | 3 | $this->clientRetryStatusCodes = $codes; |
|
| 90 | |||
| 91 | 3 | return $this; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param Result $result |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | 56 | private function shouldServerRetry(Result $result) |
|
| 100 | { |
||
| 101 | 56 | if ($this->serverRetry <= 0) { |
|
| 102 | 55 | return false; |
|
| 103 | } |
||
| 104 | |||
| 105 | 3 | if (in_array($result->getStatusCode(), $this->serverRetryStatusCodes)) { |
|
| 106 | 1 | $this->serverRetry--; |
|
| 107 | |||
| 108 | 1 | return true; |
|
| 109 | } |
||
| 110 | |||
| 111 | 2 | foreach ($this->serverRetryStrings as $message) { |
|
| 112 | 1 | if (Stringy::contains($result->getBody(), $message)) { |
|
| 113 | 1 | $this->serverRetry--; |
|
| 114 | |||
| 115 | 1 | return true; |
|
| 116 | } |
||
| 117 | 1 | } |
|
| 118 | |||
| 119 | 1 | return false; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param Exception $exception |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | 7 | private function shouldClientRetry(Exception $exception) |
|
| 128 | { |
||
| 129 | 7 | if ($this->clientRetry <= 0) { |
|
| 130 | 6 | return false; |
|
| 131 | } |
||
| 132 | |||
| 133 | 3 | if (in_array($exception->getCode(), $this->clientRetryStatusCodes, true)) { |
|
| 134 | 1 | $this->clientRetry--; |
|
| 135 | |||
| 136 | 1 | return true; |
|
| 137 | } |
||
| 138 | |||
| 139 | 2 | foreach ($this->clientRetryStrings as $message) { |
|
| 140 | 1 | if (Stringy::contains($exception->getMessage(), $message)) { |
|
| 141 | 1 | $this->clientRetry--; |
|
| 142 | |||
| 143 | 1 | return true; |
|
| 144 | } |
||
| 145 | 1 | } |
|
| 146 | |||
| 147 | 1 | return false; |
|
| 148 | } |
||
| 149 | } |
||
| 150 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.