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
PollingTrait.php
98 lines
| 1 | <?php |
| 2 | /* |
| 3 | * Copyright 2018 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 | |
| 33 | namespace Google\ApiCore; |
| 34 | |
| 35 | /** |
| 36 | * @internal |
| 37 | */ |
| 38 | trait PollingTrait |
| 39 | { |
| 40 | /** |
| 41 | * @param callable $pollCallable The call to poll. Must return a boolean indicating whether the |
| 42 | * operation has completed, and the polling loop can be terminated. |
| 43 | * @param array $options { |
| 44 | * Options for configuring the polling behaviour. |
| 45 | * |
| 46 | * @type int $initialPollDelayMillis The initial polling interval to use, in milliseconds. |
| 47 | * @type int $pollDelayMultiplier Multiplier applied to the polling interval on each retry. |
| 48 | * @type int $maxPollDelayMillis The maximum polling interval to use, in milliseconds. |
| 49 | * @type int $totalPollTimeoutMillis The maximum amount of time to continue polling, in milliseconds. |
| 50 | * } |
| 51 | * @return bool |
| 52 | */ |
| 53 | private function poll(callable $pollCallable, array $options) |
| 54 | { |
| 55 | $currentPollDelayMillis = $options['initialPollDelayMillis']; |
| 56 | $pollDelayMultiplier = $options['pollDelayMultiplier']; |
| 57 | $maxPollDelayMillis = $options['maxPollDelayMillis']; |
| 58 | $totalPollTimeoutMillis = $options['totalPollTimeoutMillis']; |
| 59 | |
| 60 | $hasTotalPollTimeout = $totalPollTimeoutMillis > 0.0; |
| 61 | $endTime = $this->getCurrentTimeMillis() + $totalPollTimeoutMillis; |
| 62 | |
| 63 | while (true) { |
| 64 | if ($hasTotalPollTimeout && $this->getCurrentTimeMillis() > $endTime) { |
| 65 | return false; |
| 66 | } |
| 67 | $this->sleepMillis($currentPollDelayMillis); |
| 68 | if ($pollCallable()) { |
| 69 | return true; |
| 70 | } |
| 71 | $currentPollDelayMillis = min([ |
| 72 | $currentPollDelayMillis * $pollDelayMultiplier, |
| 73 | $maxPollDelayMillis |
| 74 | ]); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Protected to allow overriding for tests |
| 80 | * |
| 81 | * @return float Current time in milliseconds |
| 82 | */ |
| 83 | protected function getCurrentTimeMillis() |
| 84 | { |
| 85 | return microtime(true) * 1000.0; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Protected to allow overriding for tests |
| 90 | * |
| 91 | * @param int $millis |
| 92 | */ |
| 93 | protected function sleepMillis(int $millis) |
| 94 | { |
| 95 | usleep($millis * 1000); |
| 96 | } |
| 97 | } |
| 98 |