-
Notifications
You must be signed in to change notification settings - Fork 34
refactor(retries): added new retry policy to prevent retry amplification #134
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ venv* | |
| .tox | ||
| build/ | ||
| dist | ||
| .python-version | ||
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,123 @@ | ||
| from concurrent import futures | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import grpc | ||
| import pytest | ||
|
|
||
| from yandex.cloud.vpc.v1.network_pb2 import Network | ||
| from yandex.cloud.vpc.v1.network_service_pb2 import GetNetworkRequest | ||
| from yandex.cloud.vpc.v1.network_service_pb2_grpc import ( | ||
| NetworkServiceStub, | ||
| add_NetworkServiceServicer_to_server, | ||
| ) | ||
| from yandexcloud import SDK, RetryPolicy | ||
| from yandexcloud._channels import Channels | ||
|
|
||
| INSECURE_SERVICE_PORT = "50051" | ||
| SERVICE_ADDR = "localhost" | ||
|
|
||
|
|
||
| def side_effect_internal(_, context): | ||
| context.set_code(grpc.StatusCode.INTERNAL) | ||
|
|
||
|
|
||
| def side_effect_unavailable(_, context): | ||
| context.set_code(grpc.StatusCode.UNAVAILABLE) | ||
|
|
||
|
|
||
| class VPCServiceMock: | ||
| def __init__(self, fn): | ||
| self.Get = Mock(return_value=Network(id="12342314")) | ||
| self.Create = Mock() | ||
| self.Update = Mock() | ||
| self.Delete = Mock() | ||
| self.ListSubnets = Mock() | ||
| self.ListSecurityGroups = Mock() | ||
| self.ListRouteTables = Mock() | ||
| self.ListOperations = Mock() | ||
| self.Move = Mock() | ||
| self.List = Mock() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_channel(): | ||
| with patch.multiple( | ||
| Channels, | ||
| _get_creds=lambda self, endpoint: grpc.local_channel_credentials(), | ||
| _get_endpoints=lambda self: { | ||
| "vpc": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| "iam": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| }, | ||
| ) as channel_patch: | ||
| yield channel_patch | ||
|
|
||
|
|
||
| def grpc_server(side_effect): | ||
| service = VPCServiceMock(side_effect) | ||
| server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) | ||
| server.add_insecure_port("localhost:" + INSECURE_SERVICE_PORT) | ||
| add_NetworkServiceServicer_to_server(service, server) | ||
| server.start() | ||
| return server, service | ||
|
|
||
|
|
||
| def test_default_retries(mock_channel): | ||
| server, service = grpc_server(side_effect_unavailable) | ||
|
|
||
| sdk = SDK( | ||
| retry_policy=RetryPolicy(), | ||
| endpoint=f"localhost:{INSECURE_SERVICE_PORT}", | ||
| endpoints={ | ||
| "vpc": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| "iam": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| }, | ||
| ) | ||
| network_client = sdk.client(NetworkServiceStub, insecure=True) | ||
| try: | ||
| request = GetNetworkRequest(network_id="asdf") | ||
| network_client.Get(request) | ||
| except grpc.RpcError: | ||
| assert service.Get.call_count == 4 | ||
|
|
||
| server.stop(0) | ||
|
|
||
|
|
||
| def test_custom_retries(mock_channel): | ||
| server, service = grpc_server(side_effect_internal) | ||
|
|
||
| sdk = SDK( | ||
| retry_policy=RetryPolicy(status_codes=(grpc.StatusCode.INTERNAL,), max_attempts=4), | ||
| endpoint=f"localhost:{INSECURE_SERVICE_PORT}", | ||
| endpoints={ | ||
| "vpc": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| "iam": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| }, | ||
| ) | ||
| network_client = sdk.client(NetworkServiceStub, insecure=True) | ||
| try: | ||
| request = GetNetworkRequest(network_id="asdf") | ||
| network_client.Get(request) | ||
| except grpc.RpcError: | ||
| assert service.Get.call_count == 4 | ||
|
|
||
| server.stop(0) | ||
|
|
||
|
|
||
| def test_no_retries(mock_channel): | ||
| server, service = grpc_server(side_effect_internal) | ||
|
|
||
| sdk = SDK( | ||
| endpoint=f"localhost:{INSECURE_SERVICE_PORT}", | ||
| endpoints={ | ||
| "vpc": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| "iam": SERVICE_ADDR + ":" + INSECURE_SERVICE_PORT, | ||
| }, | ||
| ) | ||
| network_client = sdk.client(NetworkServiceStub, insecure=True) | ||
| try: | ||
| request = GetNetworkRequest(network_id="asdf") | ||
| network_client.Get(request) | ||
| except grpc.RpcError: | ||
| assert service.Get.call_count == 1 | ||
|
|
||
| server.stop(0) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,31 @@ | ||
| import json | ||
| from typing import Tuple | ||
|
|
||
| import grpc | ||
|
|
||
|
|
||
| class RetryPolicy: | ||
| def __init__( | ||
| self, | ||
| max_attempts: int = 4, | ||
| status_codes: Tuple["grpc.StatusCode"] = (grpc.StatusCode.UNAVAILABLE,), | ||
| ): | ||
| self.__policy = { | ||
PeppaTheC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "methodConfig": [ | ||
| { | ||
| "name": [{}], | ||
| "retryPolicy": { | ||
| "maxAttempts": max_attempts, | ||
| "initialBackoff": "0.1s", | ||
| "maxBackoff": "20s", | ||
| "backoffMultiplier": 2, | ||
| "retryableStatusCodes": [status.name for status in status_codes], | ||
| }, | ||
| } | ||
| ], | ||
| "retryThrottling": {"maxTokens": 100, "tokenRatio": 0.1}, | ||
| "waitForReady": True, | ||
| } | ||
|
|
||
| def to_json(self) -> str: | ||
| return json.dumps(self.__policy) | ||
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.