|
| 1 | +import logging |
| 2 | +import random |
| 3 | +from datetime import datetime, timedelta |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +import grpc |
| 7 | + |
| 8 | +from .default_eligibility_strategy import DefaultEligibilityStrategy |
| 9 | +from .eligibility_strategy import EligibilityStrategy |
| 10 | +from .retry_strategy import RetryStrategy |
| 11 | +from .retryable_props import RetryableProps |
| 12 | + |
| 13 | +logger = logging.getLogger("fixed-timeout-retry-strategy") |
| 14 | + |
| 15 | + |
| 16 | +class FixedTimeoutRetryStrategy(RetryStrategy): |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + *, |
| 20 | + retry_timeout_millis: int, |
| 21 | + retry_delay_interval_millis: int, |
| 22 | + eligibility_strategy: DefaultEligibilityStrategy = DefaultEligibilityStrategy(), |
| 23 | + ): |
| 24 | + self._eligibility_strategy: EligibilityStrategy = eligibility_strategy |
| 25 | + self._retry_timeout_millis: int = retry_timeout_millis |
| 26 | + self._retry_delay_interval_millis: int = retry_delay_interval_millis |
| 27 | + |
| 28 | + def determine_when_to_retry(self, props: RetryableProps) -> Optional[float]: |
| 29 | + """Determines whether a grpc call can be retried and how long to wait before that retry. |
| 30 | +
|
| 31 | + Args: |
| 32 | + props (RetryableProps): Information about the grpc call, its last invocation, and how many times the call |
| 33 | + has been made. |
| 34 | +
|
| 35 | + :Returns |
| 36 | + The time in seconds before the next retry should occur or None if no retry should be attempted. |
| 37 | + """ |
| 38 | + logger.debug( |
| 39 | + "Determining whether request is eligible for retry; status code: %s, request type: %s, attemptNumber: %d", |
| 40 | + props.grpc_status, # type: ignore[misc] |
| 41 | + props.grpc_method, |
| 42 | + props.attempt_number, |
| 43 | + ) |
| 44 | + |
| 45 | + if props.overall_deadline is None: |
| 46 | + logger.debug("Overall deadline is None; not retrying.") |
| 47 | + return None |
| 48 | + |
| 49 | + # If a retry attempt's timeout has passed but the client's overall timeout has not yet passed, |
| 50 | + # we should reset the deadline and retry. |
| 51 | + if ( |
| 52 | + props.attempt_number > 0 |
| 53 | + and props.grpc_status == grpc.StatusCode.DEADLINE_EXCEEDED # type: ignore[misc] |
| 54 | + and props.overall_deadline > datetime.now() |
| 55 | + ): |
| 56 | + return self.get_jitter_in_millis(props) |
| 57 | + |
| 58 | + if self._eligibility_strategy.is_eligible_for_retry(props) is False: |
| 59 | + logger.debug( |
| 60 | + "Request path: %s; retryable status code: %s. Request is not retryable.", |
| 61 | + props.grpc_method, |
| 62 | + props.grpc_status, # type: ignore[misc] |
| 63 | + ) |
| 64 | + return None |
| 65 | + |
| 66 | + return self.get_jitter_in_millis(props) |
| 67 | + |
| 68 | + def get_jitter_in_millis(self, props: RetryableProps) -> float: |
| 69 | + timeout_with_jitter = self.add_jitter(self._retry_delay_interval_millis) |
| 70 | + logger.debug( |
| 71 | + "Determined request is retryable; retrying after %d ms: [method: %s, status: %s, attempt: %d]", |
| 72 | + timeout_with_jitter, |
| 73 | + props.grpc_method, |
| 74 | + props.grpc_status, # type: ignore[misc] |
| 75 | + props.attempt_number, |
| 76 | + ) |
| 77 | + return timeout_with_jitter / 1000.0 |
| 78 | + |
| 79 | + def add_jitter(self, base_delay: int) -> int: |
| 80 | + return int((0.2 * random.random() + 0.9) * float(base_delay)) |
| 81 | + |
| 82 | + def calculate_retry_deadline(self, overall_deadline: datetime) -> Optional[float]: |
| 83 | + """Calculates the deadline for a retry attempt using the retry timeout, but clips it to the overall deadline if the overall deadline is sooner. |
| 84 | +
|
| 85 | + Args: |
| 86 | + overall_deadline (datetime): The overall deadline for the operation. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + float: The calculated retry deadline. |
| 90 | + """ |
| 91 | + logger.debug( |
| 92 | + f"Calculating retry deadline:\nnow: {datetime.now()}\noverall deadline: {overall_deadline}\n" |
| 93 | + + f"retry timeout millis: {self._retry_timeout_millis}" |
| 94 | + ) |
| 95 | + if datetime.now() + timedelta(milliseconds=self._retry_timeout_millis) > overall_deadline: |
| 96 | + return (overall_deadline - datetime.now()).total_seconds() * 1000 |
| 97 | + return self._retry_timeout_millis |
0 commit comments