PageRenderTime 58ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/backwpup/sdk/WindowsAzure/Common/Internal/Filters/ExponentialRetryPolicy.php

https://bitbucket.org/cesarmedrano/cesarmedrano
PHP | 134 lines | 47 code | 13 blank | 74 comment | 4 complexity | 79c11a284766386c557e1b7f67d52f10 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\Common\Internal\Filters
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\Common\Internal\Filters;
  24. /**
  25. * The exponential retry policy.
  26. *
  27. * @category Microsoft
  28. * @package WindowsAzure\Common\Internal\Filters
  29. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  30. * @copyright 2012 Microsoft Corporation
  31. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  32. * @version Release: @package_version@
  33. * @link https://github.com/windowsazure/azure-sdk-for-php
  34. */
  35. class ExponentialRetryPolicy extends RetryPolicy
  36. {
  37. /**
  38. * @var integer
  39. */
  40. private $_deltaBackoffIntervalInMs;
  41. /**
  42. * @var integer
  43. */
  44. private $_maximumAttempts;
  45. /**
  46. * @var integer
  47. */
  48. private $_resolvedMaxBackoff;
  49. /**
  50. * @var integer
  51. */
  52. private $_resolvedMinBackoff;
  53. /**
  54. * @var array
  55. */
  56. private $_retryableStatusCodes;
  57. /**
  58. * Initializes new object from ExponentialRetryPolicy.
  59. *
  60. * @param array $retryableStatusCodes The retryable status codes.
  61. * @param integer $deltaBackoff The backoff time delta.
  62. * @param integer $maximumAttempts The number of max attempts.
  63. */
  64. public function __construct($retryableStatusCodes,
  65. $deltaBackoff = parent::DEFAULT_CLIENT_BACKOFF,
  66. $maximumAttempts = parent::DEFAULT_CLIENT_RETRY_COUNT
  67. ) {
  68. $this->_deltaBackoffIntervalInMs = $deltaBackoff;
  69. $this->_maximumAttempts = $maximumAttempts;
  70. $this->_resolvedMaxBackoff = parent::DEFAULT_MAX_BACKOFF;
  71. $this->_resolvedMinBackoff = parent::DEFAULT_MIN_BACKOFF;
  72. $this->_retryableStatusCodes = $retryableStatusCodes;
  73. sort($retryableStatusCodes);
  74. }
  75. /**
  76. * Indicates if there should be a retry or not.
  77. *
  78. * @param integer $retryCount The retry count.
  79. * @param \HTTP_Request2_Response $response The HTTP response object.
  80. *
  81. * @return boolean
  82. */
  83. public function shouldRetry($retryCount, $response)
  84. {
  85. if ( $retryCount >= $this->_maximumAttempts
  86. || array_search($response->getStatus(), $this->_retryableStatusCodes)
  87. || is_null($response)
  88. ) {
  89. return false;
  90. } else {
  91. return true;
  92. }
  93. }
  94. /**
  95. * Calculates the backoff for the retry policy.
  96. *
  97. * @param integer $retryCount The retry count.
  98. * @param \HTTP_Request2_Response $response The HTTP response object.
  99. *
  100. * @return integer
  101. */
  102. public function calculateBackoff($retryCount, $response)
  103. {
  104. // Calculate backoff Interval between 80% and 120% of the desired
  105. // backoff, multiply by 2^n -1 for
  106. // exponential
  107. $incrementDelta = (int) (pow(2, $retryCount) - 1);
  108. $boundedRandDelta = (int) ($this->_deltaBackoffIntervalInMs * 0.8)
  109. + mt_rand(
  110. 0,
  111. (int) ($this->_deltaBackoffIntervalInMs * 1.2)
  112. - (int) ($this->_deltaBackoffIntervalInMs * 0.8)
  113. );
  114. $incrementDelta *= $boundedRandDelta;
  115. // Enforce max / min backoffs
  116. return min(
  117. $this->_resolvedMinBackoff + $incrementDelta,
  118. $this->_resolvedMaxBackoff
  119. );
  120. }
  121. }