Skip to content

Track retry mode and gzip feature ids #3481

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 4 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion botocore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from botocore.model import ServiceModel
from botocore.paginate import Paginator
from botocore.retries import adaptive, standard
from botocore.useragent import UserAgentString
from botocore.useragent import UserAgentString, register_feature_id
from botocore.utils import (
CachedProperty,
EventbridgeSignerSetter,
Expand Down Expand Up @@ -246,6 +246,9 @@ def _register_retries(self, client):
self._register_v2_adaptive_retries(client)
elif retry_mode == 'legacy':
self._register_legacy_retries(client)
else:
return
register_feature_id(f'RETRY_MODE_{retry_mode.upper()}')

def _register_v2_standard_retries(self, client):
max_attempts = client.meta.config.retries.get('total_max_attempts')
Expand Down
2 changes: 2 additions & 0 deletions botocore/compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from gzip import compress as gzip_compress

from botocore.compat import urlencode
from botocore.useragent import register_feature_id
from botocore.utils import determine_content_length

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,6 +89,7 @@ def _get_body_size(body):


def _gzip_compress_body(body):
register_feature_id('GZIP_REQUEST_COMPRESSION')
if isinstance(body, str):
return gzip_compress(body.encode('utf-8'))
elif isinstance(body, (bytes, bytearray)):
Expand Down
4 changes: 4 additions & 0 deletions botocore/useragent.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
_USERAGENT_FEATURE_MAPPINGS = {
'WAITER': 'B',
'PAGINATOR': 'C',
"RETRY_MODE_LEGACY": "D",
"RETRY_MODE_STANDARD": "E",
"RETRY_MODE_ADAPTIVE": "F",
'S3_TRANSFER': 'G',
'GZIP_REQUEST_COMPRESSION': 'L',
'PROTOCOL_RPC_V2_CBOR': 'M',
'ENDPOINT_OVERRIDE': 'N',
'ACCOUNT_ID_MODE_PREFERRED': 'P',
Expand Down
25 changes: 25 additions & 0 deletions tests/functional/test_compress.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from botocore.compress import COMPRESSION_MAPPING
from botocore.config import Config
from tests import ALL_SERVICES, ClientHTTPStubber, patch_load_service_model
from tests.functional.test_useragent import (
get_captured_ua_strings,
parse_registered_feature_ids,
)

FAKE_MODEL = {
"version": "2.0",
Expand Down Expand Up @@ -127,3 +131,24 @@ def test_compression(patched_session, monkeypatch):
serialized_body = f"{additional_params}&{serialized_params}"
actual_body = gzip.decompress(http_stubber.requests[0].body)
assert serialized_body.encode('utf-8') == actual_body


def test_user_agent_has_gzip_feature_id(patched_session, monkeypatch):
patch_load_service_model(
patched_session, monkeypatch, FAKE_MODEL, FAKE_RULESET
)
client = patched_session.create_client(
"otherservice",
region_name="us-west-2",
config=Config(request_min_compression_size_bytes=100),
)
with ClientHTTPStubber(client, strict=True) as http_stubber:
http_stubber.add_response(status=200, body=b"<response/>")
params_list = [
{"MockOpParam": f"MockOpParamValue{i}"} for i in range(1, 21)
]
# The mock operation registers `'GZIP_REQUEST_COMPRESSION': 'L'`
client.mock_operation(MockOpParamList=params_list)
ua_string = get_captured_ua_strings(http_stubber)[0]
feature_list = parse_registered_feature_ids(ua_string)
assert 'L' in feature_list
38 changes: 38 additions & 0 deletions tests/functional/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
from botocore.config import Config
from botocore.exceptions import ClientError
from tests import BaseSessionTest, ClientHTTPStubber, mock
from tests.functional.test_useragent import (
get_captured_ua_strings,
parse_registered_feature_ids,
)

RETRY_MODES = ('legacy', 'standard', 'adaptive')

Expand Down Expand Up @@ -49,6 +53,18 @@ def assert_will_retry_n_times(
yield
self.assertEqual(len(http_stubber.requests), num_responses)

def _get_feature_id_lists_from_retries(self, client):
with ClientHTTPStubber(client) as http_stubber:
# Add two failed responses followed by a success
http_stubber.add_response(status=502, body=b'{}')
http_stubber.add_response(status=502, body=b'{}')
http_stubber.add_response(status=200, body=b'{}')
client.list_tables()
ua_strings = get_captured_ua_strings(http_stubber)
return [
parse_registered_feature_ids(ua_string) for ua_string in ua_strings
]


class TestRetryHeader(BaseRetryTest):
def _retry_headers_test_cases(self):
Expand Down Expand Up @@ -264,6 +280,12 @@ def test_can_clobber_max_attempts_on_session(self):
with self.assert_will_retry_n_times(client, 0):
client.list_repositories()

def test_user_agent_has_legacy_mode_feature_id(self):
client = self.session.create_client('dynamodb', self.region)
feature_lists = self._get_feature_id_lists_from_retries(client)
# Confirm all requests register `'RETRY_MODE_LEGACY': 'D'`
assert all('D' in feature_list for feature_list in feature_lists)


class TestRetriesV2(BaseRetryTest):
def create_client_with_retry_mode(
Expand Down Expand Up @@ -349,3 +371,19 @@ def test_can_exhaust_default_retry_quota(self):
self.assertTrue(
e.exception.response['ResponseMetadata'].get('RetryQuotaReached')
)

def test_user_agent_has_standard_mode_feature_id(self):
client = self.create_client_with_retry_mode(
'dynamodb', retry_mode='standard'
)
feature_lists = self._get_feature_id_lists_from_retries(client)
# Confirm all requests register `'RETRY_MODE_STANDARD': 'E'`
assert all('E' in feature_list for feature_list in feature_lists)

def test_user_agent_has_adaptive_mode_feature_id(self):
client = self.create_client_with_retry_mode(
'dynamodb', retry_mode='adaptive'
)
feature_lists = self._get_feature_id_lists_from_retries(client)
# Confirm all requests register `'RETRY_MODE_ADAPTIVE': 'F'`
assert all('F' in feature_list for feature_list in feature_lists)