retry_policy.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. from aliyunsdkcore.retry.retry_condition import *
  15. from aliyunsdkcore.retry.backoff_strategy import *
  16. class RetryPolicy(RetryCondition, BackoffStrategy):
  17. def __init__(self, retry_condition, backoff_strategy):
  18. self.retry_condition = retry_condition
  19. self.backoff_strategy = backoff_strategy
  20. def should_retry(self, retry_policy_context):
  21. return self.retry_condition.should_retry(retry_policy_context)
  22. def compute_delay_before_next_retry(self, retry_policy_context):
  23. return self.backoff_strategy.compute_delay_before_next_retry(
  24. retry_policy_context)
  25. PREDEFINED_DEFAULT_RETRY_POLICY = RetryPolicy(DefaultConfigRetryCondition(),
  26. DefaultMixedBackoffStrategy())
  27. NO_RETRY_POLICY = RetryPolicy(NoRetryCondition(), NoDelayStrategy())
  28. def get_default_retry_policy(max_retry_times=None):
  29. return RetryPolicy(DefaultConfigRetryCondition(max_retry_times=max_retry_times),
  30. DefaultMixedBackoffStrategy())