PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / trunk
ShareThis Dashboard for Google Analytics vtrunk
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / google / gax / src / RetrySettings.php
googleanalytics / lib / analytics-admin / vendor / google / gax / src Last commit date
Middleware 3 years ago ResourceTemplate 3 years ago Testing 3 years ago Transport 3 years ago AgentHeader.php 3 years ago ApiException.php 3 years ago ApiStatus.php 3 years ago ArrayTrait.php 3 years ago BidiStream.php 3 years ago Call.php 3 years ago ClientStream.php 3 years ago CredentialsWrapper.php 3 years ago FixedSizeCollection.php 3 years ago GPBLabel.php 3 years ago GPBType.php 3 years ago GapicClientTrait.php 3 years ago GrpcSupportTrait.php 3 years ago OperationResponse.php 3 years ago Page.php 3 years ago PageStreamingDescriptor.php 3 years ago PagedListResponse.php 3 years ago PathTemplate.php 3 years ago PollingTrait.php 3 years ago RequestBuilder.php 3 years ago RequestParamsHeaderDescriptor.php 3 years ago RetrySettings.php 3 years ago Serializer.php 3 years ago ServerStream.php 3 years ago ServerStreamingCallInterface.php 3 years ago ServiceAddressTrait.php 3 years ago UriTrait.php 3 years ago ValidationException.php 3 years ago ValidationTrait.php 3 years ago Version.php 3 years ago
RetrySettings.php
502 lines
1 <?php
2 /*
3 * Copyright 2016 Google LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 namespace Google\ApiCore;
33
34 /**
35 * The RetrySettings class is used to configure retrying and timeouts for RPCs.
36 * This class can be passed as an optional parameter to RPC methods, or as part
37 * of an optional array in the constructor of a client object. In addition,
38 * many RPCs and API clients accept a PHP array in place of a RetrySettings
39 * object. This can be used to change particular retry parameters without
40 * needing to construct a complete RetrySettings object.
41 *
42 * Constructing a RetrySettings object
43 * -----------------------------------
44 *
45 * See the RetrySettings constructor for documentation about parameters that
46 * can be passed to RetrySettings.
47 *
48 * Example of creating a RetrySettings object using the constructor:
49 * ```
50 * $retrySettings = new RetrySettings([
51 * 'initialRetryDelayMillis' => 100,
52 * 'retryDelayMultiplier' => 1.3,
53 * 'maxRetryDelayMillis' => 60000,
54 * 'initialRpcTimeoutMillis' => 20000,
55 * 'rpcTimeoutMultiplier' => 1.0,
56 * 'maxRpcTimeoutMillis' => 20000,
57 * 'totalTimeoutMillis' => 600000,
58 * 'retryableCodes' => [ApiStatus::DEADLINE_EXCEEDED, ApiStatus::UNAVAILABLE],
59 * ]);
60 * ```
61 *
62 * It is also possible to create a new RetrySettings object from an existing
63 * object using the {@see \Google\ApiCore\RetrySettings::with()} method.
64 *
65 * Example modifying an existing RetrySettings object using `with()`:
66 * ```
67 * $newRetrySettings = $retrySettings->with([
68 * 'totalTimeoutMillis' => 700000,
69 * ]);
70 * ```
71 *
72 * Modifying the retry behavior of an RPC method
73 * ---------------------------------------------
74 *
75 * RetrySettings objects can be used to control retries for many RPC methods in
76 * [google-cloud-php](https://github.com/googleapis/google-cloud-php).
77 * The examples below make use of the
78 * [GroupServiceClient](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/monitoring/v3/groupserviceclient)
79 * from the [Monitoring V3 API](https://github.com/googleapis/google-cloud-php/tree/master/src/Monitoring/V3),
80 * but they can be applied to other APIs in the
81 * [google-cloud-php](https://github.com/googleapis/google-cloud-php) repository.
82 *
83 * It is possible to specify the retry behavior to be used by an RPC via the
84 * `retrySettings` field in the `optionalArgs` parameter. The `retrySettings`
85 * field can contain either a RetrySettings object, or a PHP array containing
86 * the particular retry parameters to be updated.
87 *
88 * Example of disabling retries for a single call to the
89 * [listGroups](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/monitoring/v3/groupserviceclient?method=listGroups)
90 * method, and setting a custom timeout:
91 * ```
92 * $result = $client->listGroups($name, [
93 * 'retrySettings' => [
94 * 'retriesEnabled' => false,
95 * 'noRetriesRpcTimeoutMillis' => 5000,
96 * ]
97 * ]);
98 * ```
99 *
100 * Example of creating a new RetrySettings object and using it to override
101 * the retry settings for a call to the
102 * [listGroups](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/monitoring/v3/groupserviceclient?method=listGroups)
103 * method:
104 * ```
105 * $customRetrySettings = new RetrySettings([
106 * 'initialRetryDelayMillis' => 100,
107 * 'retryDelayMultiplier' => 1.3,
108 * 'maxRetryDelayMillis' => 60000,
109 * 'initialRpcTimeoutMillis' => 20000,
110 * 'rpcTimeoutMultiplier' => 1.0,
111 * 'maxRpcTimeoutMillis' => 20000,
112 * 'totalTimeoutMillis' => 600000,
113 * 'retryableCodes' => [ApiStatus::DEADLINE_EXCEEDED, ApiStatus::UNAVAILABLE],
114 * ]);
115 *
116 * $result = $client->listGroups($name, [
117 * 'retrySettings' => $customRetrySettings
118 * ]);
119 * ```
120 *
121 * Modifying the default retry behavior for RPC methods on a Client object
122 * -----------------------------------------------------------------------
123 *
124 * It is also possible to specify the retry behavior for RPC methods when
125 * constructing a client object using the 'retrySettingsArray'. The examples
126 * below again make use of the
127 * [GroupServiceClient](https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/monitoring/v3/groupserviceclient)
128 * from the [Monitoring V3 API](https://github.com/googleapis/google-cloud-php/tree/master/src/Monitoring/V3),
129 * but they can be applied to other APIs in the
130 * [google-cloud-php](https://github.com/googleapis/google-cloud-php) repository.
131 *
132 * The GroupServiceClient object accepts an optional `retrySettingsArray`
133 * parameter, which can be used to specify retry behavior for RPC methods
134 * on the client. The `retrySettingsArray` accepts a PHP array in which keys
135 * are the names of RPC methods on the client, and values are either a
136 * RetrySettings object or a PHP array containing the particular retry
137 * parameters to be updated.
138 *
139 * Example updating the retry settings for four methods of GroupServiceClient:
140 * ```
141 * use Google\Cloud\Monitoring\V3\GroupServiceClient;
142 *
143 * $customRetrySettings = new RetrySettings([
144 * 'initialRetryDelayMillis' => 100,
145 * 'retryDelayMultiplier' => 1.3,
146 * 'maxRetryDelayMillis' => 60000,
147 * 'initialRpcTimeoutMillis' => 20000,
148 * 'rpcTimeoutMultiplier' => 1.0,
149 * 'maxRpcTimeoutMillis' => 20000,
150 * 'totalTimeoutMillis' => 600000,
151 * 'retryableCodes' => [ApiStatus::DEADLINE_EXCEEDED, ApiStatus::UNAVAILABLE],
152 * ]);
153 *
154 * $updatedCustomRetrySettings = $customRetrySettings->with([
155 * 'totalTimeoutMillis' => 700000
156 * ]);
157 *
158 * $client = new GroupServiceClient([
159 * 'retrySettingsArray' => [
160 * 'listGroups' => ['retriesEnabled' => false],
161 * 'getGroup' => [
162 * 'initialRpcTimeoutMillis' => 10000,
163 * 'maxRpcTimeoutMillis' => 30000,
164 * 'totalTimeoutMillis' => 60000,
165 * ],
166 * 'deleteGroup' => $customRetrySettings,
167 * 'updateGroup' => $updatedCustomRetrySettings
168 * ],
169 * ]);
170 * ```
171 *
172 * Configure the use of logical timeout
173 * ------------------------------------
174 *
175 * To configure the use of a logical timeout, where a logical timeout is the
176 * duration a method is given to complete one or more RPC attempts, with each
177 * attempt using only the time remaining in the logical timeout, use
178 * {@see \Google\ApiCore\RetrySettings::logicalTimeout()} combined with
179 * {@see \Google\ApiCore\RetrySettings::with()}.
180 *
181 * ```
182 * $timeoutSettings = RetrySettings::logicalTimeout(30000);
183 *
184 * $customRetrySettings = $customRetrySettings->with($timeoutSettings);
185 *
186 * $result = $client->listGroups($name, [
187 * 'retrySettings' => $customRetrySettings
188 * ]);
189 * ```
190 *
191 * {@see \Google\ApiCore\RetrySettings::logicalTimeout()} can also be used on a
192 * method call independent of a RetrySettings instance.
193 *
194 * ```
195 * $timeoutSettings = RetrySettings::logicalTimeout(30000);
196 *
197 * $result = $client->listGroups($name, [
198 * 'retrySettings' => $timeoutSettings
199 * ]);
200 * ```
201 */
202 class RetrySettings
203 {
204 use ValidationTrait;
205
206 private $retriesEnabled;
207
208 private $retryableCodes;
209
210 private $initialRetryDelayMillis;
211 private $retryDelayMultiplier;
212 private $maxRetryDelayMillis;
213 private $initialRpcTimeoutMillis;
214 private $rpcTimeoutMultiplier;
215 private $maxRpcTimeoutMillis;
216 private $totalTimeoutMillis;
217
218 private $noRetriesRpcTimeoutMillis;
219
220 /**
221 * Constructs an instance.
222 *
223 * @param array $settings {
224 * Required. Settings for configuring the retry behavior. All parameters are required except
225 * $retriesEnabled and $noRetriesRpcTimeoutMillis, which are optional and have defaults
226 * determined based on the other settings provided.
227 *
228 * @type bool $retriesEnabled Optional. Enables retries. If not specified, the value is
229 * determined using the $retryableCodes setting. If $retryableCodes is empty,
230 * then $retriesEnabled is set to false; otherwise, it is set to true.
231 * @type int $noRetriesRpcTimeoutMillis Optional. The timeout of the rpc call to be used
232 * if $retriesEnabled is false, in milliseconds. It not specified, the value
233 * of $initialRpcTimeoutMillis is used.
234 * @type array $retryableCodes The Status codes that are retryable. Each status should be
235 * either one of the string constants defined on {@see \Google\ApiCore\ApiStatus}
236 * or an integer constant defined on {@see \Google\Rpc\Code}.
237 * @type int $initialRetryDelayMillis The initial delay of retry in milliseconds.
238 * @type int $retryDelayMultiplier The exponential multiplier of retry delay.
239 * @type int $maxRetryDelayMillis The max delay of retry in milliseconds.
240 * @type int $initialRpcTimeoutMillis The initial timeout of rpc call in milliseconds.
241 * @type int $rpcTimeoutMultiplier The exponential multiplier of rpc timeout.
242 * @type int $maxRpcTimeoutMillis The max timeout of rpc call in milliseconds.
243 * @type int $totalTimeoutMillis The max accumulative timeout in total.
244 * }
245 */
246 public function __construct(array $settings)
247 {
248 $this->validateNotNull($settings, [
249 'initialRetryDelayMillis',
250 'retryDelayMultiplier',
251 'maxRetryDelayMillis',
252 'initialRpcTimeoutMillis',
253 'rpcTimeoutMultiplier',
254 'maxRpcTimeoutMillis',
255 'totalTimeoutMillis',
256 'retryableCodes'
257 ]);
258 $this->initialRetryDelayMillis = $settings['initialRetryDelayMillis'];
259 $this->retryDelayMultiplier = $settings['retryDelayMultiplier'];
260 $this->maxRetryDelayMillis = $settings['maxRetryDelayMillis'];
261 $this->initialRpcTimeoutMillis = $settings['initialRpcTimeoutMillis'];
262 $this->rpcTimeoutMultiplier = $settings['rpcTimeoutMultiplier'];
263 $this->maxRpcTimeoutMillis = $settings['maxRpcTimeoutMillis'];
264 $this->totalTimeoutMillis = $settings['totalTimeoutMillis'];
265 $this->retryableCodes = $settings['retryableCodes'];
266 $this->retriesEnabled = array_key_exists('retriesEnabled', $settings)
267 ? $settings['retriesEnabled']
268 : (count($this->retryableCodes) > 0);
269 $this->noRetriesRpcTimeoutMillis = array_key_exists('noRetriesRpcTimeoutMillis', $settings)
270 ? $settings['noRetriesRpcTimeoutMillis']
271 : $this->initialRpcTimeoutMillis;
272 }
273
274 /**
275 * Constructs an array mapping method names to CallSettings.
276 *
277 * @param string $serviceName
278 * The fully-qualified name of this service, used as a key into
279 * the client config file.
280 * @param array $clientConfig
281 * An array parsed from the standard API client config file.
282 * @param bool $disableRetries
283 * Disable retries in all loaded RetrySettings objects. Defaults to false.
284 * @throws ValidationException
285 * @return RetrySettings[] $retrySettings
286 */
287 public static function load(
288 string $serviceName,
289 array $clientConfig,
290 bool $disableRetries = false
291 ) {
292 $serviceRetrySettings = [];
293
294 $serviceConfig = $clientConfig['interfaces'][$serviceName];
295 $retryCodes = $serviceConfig['retry_codes'];
296 $retryParams = $serviceConfig['retry_params'];
297 foreach ($serviceConfig['methods'] as $methodName => $methodConfig) {
298 $timeoutMillis = $methodConfig['timeout_millis'];
299
300 if (empty($methodConfig['retry_codes_name']) || empty($methodConfig['retry_params_name'])) {
301 // Construct a RetrySettings object with retries disabled
302 $retrySettings = self::constructDefault()->with([
303 'noRetriesRpcTimeoutMillis' => $timeoutMillis,
304 ]);
305 } else {
306 $retryCodesName = $methodConfig['retry_codes_name'];
307 $retryParamsName = $methodConfig['retry_params_name'];
308
309 if (!array_key_exists($retryCodesName, $retryCodes)) {
310 throw new ValidationException("Invalid retry_codes_name setting: '$retryCodesName'");
311 }
312 if (!array_key_exists($retryParamsName, $retryParams)) {
313 throw new ValidationException("Invalid retry_params_name setting: '$retryParamsName'");
314 }
315
316 foreach ($retryCodes[$retryCodesName] as $status) {
317 if (!ApiStatus::isValidStatus($status)) {
318 throw new ValidationException("Invalid status code: '$status'");
319 }
320 }
321
322 $retryParameters = self::convertArrayFromSnakeCase($retryParams[$retryParamsName]) + [
323 'retryableCodes' => $retryCodes[$retryCodesName],
324 'noRetriesRpcTimeoutMillis' => $timeoutMillis,
325 ];
326 if ($disableRetries) {
327 $retryParameters['retriesEnabled'] = false;
328 }
329
330 $retrySettings = new RetrySettings($retryParameters);
331 }
332
333 $serviceRetrySettings[$methodName] = $retrySettings;
334 }
335
336 return $serviceRetrySettings;
337 }
338
339 public static function constructDefault()
340 {
341 return new RetrySettings([
342 'retriesEnabled' => false,
343 'noRetriesRpcTimeoutMillis' => 30000,
344 'initialRetryDelayMillis' => 100,
345 'retryDelayMultiplier' => 1.3,
346 'maxRetryDelayMillis' => 60000,
347 'initialRpcTimeoutMillis' => 20000,
348 'rpcTimeoutMultiplier' => 1,
349 'maxRpcTimeoutMillis' => 20000,
350 'totalTimeoutMillis' => 600000,
351 'retryableCodes' => []]);
352 }
353
354 /**
355 * Creates a new instance of RetrySettings that updates the settings in the existing instance
356 * with the settings specified in the $settings parameter.
357 *
358 * @param array $settings {
359 * Settings for configuring the retry behavior. Supports all of the options supported by
360 * the constructor; see {@see \Google\ApiCore\RetrySettings::__construct()}. All parameters
361 * are optional - all unset parameters will default to the value in the existing instance.
362 * }
363 * @return RetrySettings
364 */
365 public function with(array $settings)
366 {
367 $existingSettings = [
368 'initialRetryDelayMillis' => $this->getInitialRetryDelayMillis(),
369 'retryDelayMultiplier' => $this->getRetryDelayMultiplier(),
370 'maxRetryDelayMillis' => $this->getMaxRetryDelayMillis(),
371 'initialRpcTimeoutMillis' => $this->getInitialRpcTimeoutMillis(),
372 'rpcTimeoutMultiplier' => $this->getRpcTimeoutMultiplier(),
373 'maxRpcTimeoutMillis' => $this->getMaxRpcTimeoutMillis(),
374 'totalTimeoutMillis' => $this->getTotalTimeoutMillis(),
375 'retryableCodes' => $this->getRetryableCodes(),
376 'retriesEnabled' => $this->retriesEnabled(),
377 'noRetriesRpcTimeoutMillis' => $this->getNoRetriesRpcTimeoutMillis(),
378 ];
379 return new RetrySettings($settings + $existingSettings);
380 }
381
382 /**
383 * Creates an associative array of the {@see \Google\ApiCore\RetrySettings} timeout fields configured
384 * with the given timeout specified in the $timeout parameter interpreted as a logical timeout.
385 *
386 * @param int $timeout The timeout in milliseconds to be used as a logical call timeout.
387 * @return array
388 */
389 public static function logicalTimeout(int $timeout)
390 {
391 return [
392 'initialRpcTimeoutMillis' => $timeout,
393 'maxRpcTimeoutMillis' => $timeout,
394 'totalTimeoutMillis' => $timeout,
395 'noRetriesRpcTimeoutMillis' => $timeout,
396 'rpcTimeoutMultiplier' => 1.0
397 ];
398 }
399
400 /**
401 * @return bool Returns true if retries are enabled, otherwise returns false.
402 */
403 public function retriesEnabled()
404 {
405 return $this->retriesEnabled;
406 }
407
408 /**
409 * @return int The timeout of the rpc call to be used if $retriesEnabled is false,
410 * in milliseconds.
411 */
412 public function getNoRetriesRpcTimeoutMillis()
413 {
414 return $this->noRetriesRpcTimeoutMillis;
415 }
416
417 /**
418 * @return int[] Status codes to retry
419 */
420 public function getRetryableCodes()
421 {
422 return $this->retryableCodes;
423 }
424
425 /**
426 * @return int The initial retry delay in milliseconds. If $this->retriesEnabled()
427 * is false, this setting is unused.
428 */
429 public function getInitialRetryDelayMillis()
430 {
431 return $this->initialRetryDelayMillis;
432 }
433
434 /**
435 * @return float The retry delay multiplier. If $this->retriesEnabled()
436 * is false, this setting is unused.
437 */
438 public function getRetryDelayMultiplier()
439 {
440 return $this->retryDelayMultiplier;
441 }
442
443 /**
444 * @return int The maximum retry delay in milliseconds. If $this->retriesEnabled()
445 * is false, this setting is unused.
446 */
447 public function getMaxRetryDelayMillis()
448 {
449 return $this->maxRetryDelayMillis;
450 }
451
452 /**
453 * @return int The initial rpc timeout in milliseconds. If $this->retriesEnabled()
454 * is false, this setting is unused - use noRetriesRpcTimeoutMillis to
455 * set the timeout in that case.
456 */
457 public function getInitialRpcTimeoutMillis()
458 {
459 return $this->initialRpcTimeoutMillis;
460 }
461
462 /**
463 * @return float The rpc timeout multiplier. If $this->retriesEnabled()
464 * is false, this setting is unused.
465 */
466 public function getRpcTimeoutMultiplier()
467 {
468 return $this->rpcTimeoutMultiplier;
469 }
470
471 /**
472 * @return int The maximum rpc timeout in milliseconds. If $this->retriesEnabled()
473 * is false, this setting is unused - use noRetriesRpcTimeoutMillis to
474 * set the timeout in that case.
475 */
476 public function getMaxRpcTimeoutMillis()
477 {
478 return $this->maxRpcTimeoutMillis;
479 }
480
481 /**
482 * @return int The total time in milliseconds to spend on the call, including all
483 * retry attempts and delays between attempts. If $this->retriesEnabled()
484 * is false, this setting is unused - use noRetriesRpcTimeoutMillis to
485 * set the timeout in that case.
486 */
487 public function getTotalTimeoutMillis()
488 {
489 return $this->totalTimeoutMillis;
490 }
491
492 private static function convertArrayFromSnakeCase(array $settings)
493 {
494 $camelCaseSettings = [];
495 foreach ($settings as $key => $value) {
496 $camelCaseKey = str_replace(' ', '', ucwords(str_replace('_', ' ', $key)));
497 $camelCaseSettings[lcfirst($camelCaseKey)] = $value;
498 }
499 return $camelCaseSettings;
500 }
501 }
502