backoff_strategy.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright 2019 Alibaba Cloud Inc. All rights reserved.
  2. #
  3. # 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. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import random
  15. from aliyunsdkcore.retry.retry_condition import RetryCondition
  16. class BackoffStrategy(object):
  17. def compute_delay_before_next_retry(self, retry_policy_context):
  18. """Compute delay for request need to be retried, in milliseconds"""
  19. pass
  20. class FixedDelayStrategy(BackoffStrategy):
  21. def __init__(self, fixed_delay):
  22. self.fixed_delay = fixed_delay
  23. def compute_delay_before_next_retry(self, retry_policy_context):
  24. return self.fixed_delay
  25. class NoDelayStrategy(FixedDelayStrategy):
  26. def __init__(self):
  27. FixedDelayStrategy.__init__(self, 0)
  28. class ExponentialBackoffStrategy(BackoffStrategy):
  29. MAX_RETRY_LIMIT = 30 # to avoid integer overflow during delay calculation
  30. def __init__(self, base_delay_in_milliseconds, max_delay_in_milliseconds):
  31. self.base_delay_in_milliseconds = base_delay_in_milliseconds
  32. self.max_delay_in_milliseconds = max_delay_in_milliseconds
  33. def compute_delay_before_next_retry(self, retry_policy_context):
  34. retries = min(self.MAX_RETRY_LIMIT, retry_policy_context.retries_attempted)
  35. delay = min(self.max_delay_in_milliseconds, self.base_delay_in_milliseconds << retries)
  36. return delay
  37. class JitteredExponentialBackoffStrategy(ExponentialBackoffStrategy):
  38. def compute_delay_before_next_retry(self, retry_policy_context):
  39. delay = ExponentialBackoffStrategy.compute_delay_before_next_retry(self,
  40. retry_policy_context)
  41. return delay / 2 + random.randint(0, int(delay / 2))
  42. class DefaultMixedBackoffStrategy(BackoffStrategy):
  43. # in milliseconds
  44. SDK_DEFAULT_BASE_DELAY = 100
  45. SDK_DEFAULT_TROTTLED_BASE_DELAY = 500
  46. SDK_DEFAULT_MAX_BACKOFF = 20 * 1000
  47. def __init__(self):
  48. self._default_backoff_strategy = ExponentialBackoffStrategy(
  49. self.SDK_DEFAULT_BASE_DELAY,
  50. self.SDK_DEFAULT_MAX_BACKOFF
  51. )
  52. self._default_throttled_backoff_strategy = JitteredExponentialBackoffStrategy(
  53. self.SDK_DEFAULT_TROTTLED_BASE_DELAY,
  54. self.SDK_DEFAULT_MAX_BACKOFF
  55. )
  56. def compute_delay_before_next_retry(self, retry_policy_context):
  57. retryable = retry_policy_context.retryable
  58. if retryable & RetryCondition.SHOULD_RETRY_WITH_THROTTLING_BACKOFF:
  59. return self._default_throttled_backoff_strategy.compute_delay_before_next_retry(
  60. retry_policy_context)
  61. else:
  62. return self._default_backoff_strategy.compute_delay_before_next_retry(
  63. retry_policy_context)