1 | <?php |
||||
2 | /* |
||||
3 | * Copyright 2014 Google Inc. |
||||
4 | * |
||||
5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||||
6 | * you may not use this file except in compliance with the License. |
||||
7 | * You may obtain a copy of the License at |
||||
8 | * |
||||
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
||||
10 | * |
||||
11 | * Unless required by applicable law or agreed to in writing, software |
||||
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
||||
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
14 | * See the License for the specific language governing permissions and |
||||
15 | * limitations under the License. |
||||
16 | */ |
||||
17 | |||||
18 | /** |
||||
19 | * A task runner with exponential backoff support. |
||||
20 | * |
||||
21 | * @see https://developers.google.com/drive/web/handle-errors#implementing_exponential_backoff |
||||
22 | */ |
||||
23 | class Google_Task_Runner |
||||
24 | { |
||||
25 | const TASK_RETRY_NEVER = 0; |
||||
26 | const TASK_RETRY_ONCE = 1; |
||||
27 | const TASK_RETRY_ALWAYS = -1; |
||||
28 | |||||
29 | /** |
||||
30 | * @var integer $maxDelay The max time (in seconds) to wait before a retry. |
||||
31 | */ |
||||
32 | private $maxDelay = 60; |
||||
33 | /** |
||||
34 | * @var integer $delay The previous delay from which the next is calculated. |
||||
35 | */ |
||||
36 | private $delay = 1; |
||||
37 | |||||
38 | /** |
||||
39 | * @var integer $factor The base number for the exponential back off. |
||||
40 | */ |
||||
41 | private $factor = 2; |
||||
42 | /** |
||||
43 | * @var float $jitter A random number between -$jitter and $jitter will be |
||||
44 | * added to $factor on each iteration to allow for a better distribution of |
||||
45 | * retries. |
||||
46 | */ |
||||
47 | private $jitter = 0.5; |
||||
48 | |||||
49 | /** |
||||
50 | * @var integer $attempts The number of attempts that have been tried so far. |
||||
51 | */ |
||||
52 | private $attempts = 0; |
||||
53 | /** |
||||
54 | * @var integer $maxAttempts The max number of attempts allowed. |
||||
55 | */ |
||||
56 | private $maxAttempts = 1; |
||||
57 | |||||
58 | /** |
||||
59 | * @var callable $action The task to run and possibly retry. |
||||
60 | */ |
||||
61 | private $action; |
||||
62 | /** |
||||
63 | * @var array $arguments The task arguments. |
||||
64 | */ |
||||
65 | private $arguments; |
||||
66 | |||||
67 | /** |
||||
68 | * @var array $retryMap Map of errors with retry counts. |
||||
69 | */ |
||||
70 | protected $retryMap = [ |
||||
71 | '500' => self::TASK_RETRY_ALWAYS, |
||||
72 | '503' => self::TASK_RETRY_ALWAYS, |
||||
73 | 'rateLimitExceeded' => self::TASK_RETRY_ALWAYS, |
||||
74 | 'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS, |
||||
75 | 6 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_RESOLVE_HOST |
||||
76 | 7 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_CONNECT |
||||
77 | 28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED |
||||
78 | 35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR |
||||
79 | 52 => self::TASK_RETRY_ALWAYS // CURLE_GOT_NOTHING |
||||
80 | ]; |
||||
81 | |||||
82 | /** |
||||
83 | * Creates a new task runner with exponential backoff support. |
||||
84 | * |
||||
85 | * @param array $config The task runner config |
||||
86 | * @param string $name The name of the current task (used for logging) |
||||
87 | * @param callable $action The task to run and possibly retry |
||||
88 | * @param array $arguments The task arguments |
||||
89 | * @throws Google_Task_Exception when misconfigured |
||||
90 | */ |
||||
91 | public function __construct( |
||||
92 | $config, |
||||
93 | $name, |
||||
0 ignored issues
–
show
|
|||||
94 | $action, |
||||
95 | array $arguments = array() |
||||
96 | ) { |
||||
97 | if (isset($config['initial_delay'])) { |
||||
98 | if ($config['initial_delay'] < 0) { |
||||
99 | throw new Google_Task_Exception( |
||||
100 | 'Task configuration `initial_delay` must not be negative.' |
||||
101 | ); |
||||
102 | } |
||||
103 | |||||
104 | $this->delay = $config['initial_delay']; |
||||
105 | } |
||||
106 | |||||
107 | if (isset($config['max_delay'])) { |
||||
108 | if ($config['max_delay'] <= 0) { |
||||
109 | throw new Google_Task_Exception( |
||||
110 | 'Task configuration `max_delay` must be greater than 0.' |
||||
111 | ); |
||||
112 | } |
||||
113 | |||||
114 | $this->maxDelay = $config['max_delay']; |
||||
115 | } |
||||
116 | |||||
117 | if (isset($config['factor'])) { |
||||
118 | if ($config['factor'] <= 0) { |
||||
119 | throw new Google_Task_Exception( |
||||
120 | 'Task configuration `factor` must be greater than 0.' |
||||
121 | ); |
||||
122 | } |
||||
123 | |||||
124 | $this->factor = $config['factor']; |
||||
125 | } |
||||
126 | |||||
127 | if (isset($config['jitter'])) { |
||||
128 | if ($config['jitter'] <= 0) { |
||||
129 | throw new Google_Task_Exception( |
||||
130 | 'Task configuration `jitter` must be greater than 0.' |
||||
131 | ); |
||||
132 | } |
||||
133 | |||||
134 | $this->jitter = $config['jitter']; |
||||
135 | } |
||||
136 | |||||
137 | if (isset($config['retries'])) { |
||||
138 | if ($config['retries'] < 0) { |
||||
139 | throw new Google_Task_Exception( |
||||
140 | 'Task configuration `retries` must not be negative.' |
||||
141 | ); |
||||
142 | } |
||||
143 | $this->maxAttempts += $config['retries']; |
||||
144 | } |
||||
145 | |||||
146 | if (!is_callable($action)) { |
||||
147 | throw new Google_Task_Exception( |
||||
148 | 'Task argument `$action` must be a valid callable.' |
||||
149 | ); |
||||
150 | } |
||||
151 | |||||
152 | $this->action = $action; |
||||
153 | $this->arguments = $arguments; |
||||
154 | } |
||||
155 | |||||
156 | /** |
||||
157 | * Checks if a retry can be attempted. |
||||
158 | * |
||||
159 | * @return boolean |
||||
160 | */ |
||||
161 | public function canAttempt() |
||||
162 | { |
||||
163 | return $this->attempts < $this->maxAttempts; |
||||
164 | } |
||||
165 | |||||
166 | /** |
||||
167 | * Runs the task and (if applicable) automatically retries when errors occur. |
||||
168 | * |
||||
169 | * @return mixed |
||||
170 | * @throws Google_Task_Retryable on failure when no retries are available. |
||||
171 | */ |
||||
172 | public function run() |
||||
173 | { |
||||
174 | while ($this->attempt()) { |
||||
175 | try { |
||||
176 | return call_user_func_array($this->action, $this->arguments); |
||||
177 | } catch (Google_Service_Exception $exception) { |
||||
178 | $allowedRetries = $this->allowedRetries( |
||||
179 | $exception->getCode(), |
||||
180 | $exception->getErrors() |
||||
181 | ); |
||||
182 | |||||
183 | if (!$this->canAttempt() || !$allowedRetries) { |
||||
184 | throw $exception; |
||||
185 | } |
||||
186 | |||||
187 | if ($allowedRetries > 0) { |
||||
188 | $this->maxAttempts = min( |
||||
189 | $this->maxAttempts, |
||||
190 | $this->attempts + $allowedRetries |
||||
191 | ); |
||||
192 | } |
||||
193 | } |
||||
194 | } |
||||
195 | } |
||||
196 | |||||
197 | /** |
||||
198 | * Runs a task once, if possible. This is useful for bypassing the `run()` |
||||
199 | * loop. |
||||
200 | * |
||||
201 | * NOTE: If this is not the first attempt, this function will sleep in |
||||
202 | * accordance to the backoff configurations before running the task. |
||||
203 | * |
||||
204 | * @return boolean |
||||
205 | */ |
||||
206 | public function attempt() |
||||
207 | { |
||||
208 | if (!$this->canAttempt()) { |
||||
209 | return false; |
||||
210 | } |
||||
211 | |||||
212 | if ($this->attempts > 0) { |
||||
213 | $this->backOff(); |
||||
214 | } |
||||
215 | |||||
216 | $this->attempts++; |
||||
217 | return true; |
||||
218 | } |
||||
219 | |||||
220 | /** |
||||
221 | * Sleeps in accordance to the backoff configurations. |
||||
222 | */ |
||||
223 | private function backOff() |
||||
224 | { |
||||
225 | $delay = $this->getDelay(); |
||||
226 | |||||
227 | usleep($delay * 1000000); |
||||
0 ignored issues
–
show
$delay * 1000000 of type double is incompatible with the type integer expected by parameter $micro_seconds of usleep() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
228 | } |
||||
229 | |||||
230 | /** |
||||
231 | * Gets the delay (in seconds) for the current backoff period. |
||||
232 | * |
||||
233 | * @return float |
||||
234 | */ |
||||
235 | private function getDelay() |
||||
236 | { |
||||
237 | $jitter = $this->getJitter(); |
||||
238 | $factor = $this->attempts > 1 ? $this->factor + $jitter : 1 + abs($jitter); |
||||
239 | |||||
240 | return $this->delay = min($this->maxDelay, $this->delay * $factor); |
||||
241 | } |
||||
242 | |||||
243 | /** |
||||
244 | * Gets the current jitter (random number between -$this->jitter and |
||||
245 | * $this->jitter). |
||||
246 | * |
||||
247 | * @return float |
||||
248 | */ |
||||
249 | private function getJitter() |
||||
250 | { |
||||
251 | return $this->jitter * 2 * mt_rand() / mt_getrandmax() - $this->jitter; |
||||
252 | } |
||||
253 | |||||
254 | /** |
||||
255 | * Gets the number of times the associated task can be retried. |
||||
256 | * |
||||
257 | * NOTE: -1 is returned if the task can be retried indefinitely |
||||
258 | * |
||||
259 | * @return integer |
||||
260 | */ |
||||
261 | public function allowedRetries($code, $errors = array()) |
||||
262 | { |
||||
263 | if (isset($this->retryMap[$code])) { |
||||
264 | return $this->retryMap[$code]; |
||||
265 | } |
||||
266 | |||||
267 | if ( |
||||
268 | !empty($errors) && |
||||
269 | isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']]) |
||||
270 | ) { |
||||
271 | return $this->retryMap[$errors[0]['reason']]; |
||||
272 | } |
||||
273 | |||||
274 | return 0; |
||||
275 | } |
||||
276 | |||||
277 | public function setRetryMap($retryMap) |
||||
278 | { |
||||
279 | $this->retryMap = $retryMap; |
||||
280 | } |
||||
281 | } |
||||
282 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.