-
Notifications
You must be signed in to change notification settings - Fork 7
chore: fixed timeout retry strategy #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dba119
chore: adding fixed timeout retry strategy
pgautier404 c8d8732
chore: grab client timeout out of client call details
pgautier404 4f606e2
chore: update synch retry interceptor
pgautier404 dd2e23b
chore: linting
pgautier404 948841f
chore: remove unnecessary decoding logic and leave a comment
pgautier404 4af3f4d
chore: add comment about client call details value
pgautier404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import logging | ||
| import random | ||
| from datetime import datetime, timedelta | ||
| from typing import Optional | ||
|
|
||
| import grpc | ||
|
|
||
| from .default_eligibility_strategy import DefaultEligibilityStrategy | ||
| from .eligibility_strategy import EligibilityStrategy | ||
| from .retry_strategy import RetryStrategy | ||
| from .retryable_props import RetryableProps | ||
|
|
||
| logger = logging.getLogger("fixed-timeout-retry-strategy") | ||
|
|
||
|
|
||
| class FixedTimeoutRetryStrategy(RetryStrategy): | ||
| def __init__( | ||
| self, | ||
| *, | ||
| retry_timeout_millis: int, | ||
| retry_delay_interval_millis: int, | ||
| eligibility_strategy: DefaultEligibilityStrategy = DefaultEligibilityStrategy(), | ||
| ): | ||
| self._eligibility_strategy: EligibilityStrategy = eligibility_strategy | ||
| self._retry_timeout_millis: int = retry_timeout_millis | ||
| self._retry_delay_interval_millis: int = retry_delay_interval_millis | ||
|
|
||
| def determine_when_to_retry(self, props: RetryableProps) -> Optional[float]: | ||
| """Determines whether a grpc call can be retried and how long to wait before that retry. | ||
|
|
||
| Args: | ||
| props (RetryableProps): Information about the grpc call, its last invocation, and how many times the call | ||
| has been made. | ||
|
|
||
| :Returns | ||
| The time in seconds before the next retry should occur or None if no retry should be attempted. | ||
| """ | ||
| logger.debug( | ||
| "Determining whether request is eligible for retry; status code: %s, request type: %s, attemptNumber: %d", | ||
| props.grpc_status, # type: ignore[misc] | ||
| props.grpc_method, | ||
| props.attempt_number, | ||
| ) | ||
|
|
||
| if props.overall_deadline is None: | ||
| logger.debug("Overall deadline is None; not retrying.") | ||
| return None | ||
|
|
||
| # If a retry attempt's timeout has passed but the client's overall timeout has not yet passed, | ||
| # we should reset the deadline and retry. | ||
| if ( | ||
| props.attempt_number > 0 | ||
| and props.grpc_status == grpc.StatusCode.DEADLINE_EXCEEDED # type: ignore[misc] | ||
| and props.overall_deadline > datetime.now() | ||
| ): | ||
| return self.get_jitter_in_millis(props) | ||
|
|
||
| if self._eligibility_strategy.is_eligible_for_retry(props) is False: | ||
| logger.debug( | ||
| "Request path: %s; retryable status code: %s. Request is not retryable.", | ||
| props.grpc_method, | ||
| props.grpc_status, # type: ignore[misc] | ||
| ) | ||
| return None | ||
|
|
||
| return self.get_jitter_in_millis(props) | ||
|
|
||
| def get_jitter_in_millis(self, props: RetryableProps) -> float: | ||
| timeout_with_jitter = self.add_jitter(self._retry_delay_interval_millis) | ||
| logger.debug( | ||
| "Determined request is retryable; retrying after %d ms: [method: %s, status: %s, attempt: %d]", | ||
| timeout_with_jitter, | ||
| props.grpc_method, | ||
| props.grpc_status, # type: ignore[misc] | ||
| props.attempt_number, | ||
| ) | ||
| return timeout_with_jitter / 1000.0 | ||
|
|
||
| def add_jitter(self, base_delay: int) -> int: | ||
| return int((0.2 * random.random() + 0.9) * float(base_delay)) | ||
|
|
||
| def calculate_retry_deadline(self, overall_deadline: datetime) -> Optional[float]: | ||
| """Calculates the deadline for a retry attempt using the retry timeout, but clips it to the overall deadline if the overall deadline is sooner. | ||
|
|
||
| Args: | ||
| overall_deadline (datetime): The overall deadline for the operation. | ||
|
|
||
| Returns: | ||
| float: The calculated retry deadline. | ||
| """ | ||
| logger.debug( | ||
| f"Calculating retry deadline:\nnow: {datetime.now()}\noverall deadline: {overall_deadline}\n" | ||
| + f"retry timeout millis: {self._retry_timeout_millis}" | ||
| ) | ||
| if datetime.now() + timedelta(milliseconds=self._retry_timeout_millis) > overall_deadline: | ||
| return (overall_deadline - datetime.now()).total_seconds() * 1000 | ||
| return self._retry_timeout_millis |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.