Skip to content

Commit 8705eb6

Browse files
committed
DRAFT feat: allow setting or unsetting the boto retry configuration
DRAFT
1 parent f0bc917 commit 8705eb6

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

pynamodb/connection/base.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"""
22
Lowest level connection
33
"""
4+
import sys
45
import logging
56
import uuid
67
from threading import local
7-
from typing import Any, Dict, List, Mapping, Optional, Sequence, cast
8+
from typing import Any, Dict, List, Mapping, Optional, Sequence, Union, cast
9+
if sys.version_info >= (3, 8):
10+
from typing import Literal
11+
else:
12+
from typing_extensions import Literal
813

914
import botocore.client
1015
import botocore.exceptions
@@ -247,6 +252,7 @@ def __init__(self,
247252
read_timeout_seconds: Optional[float] = None,
248253
connect_timeout_seconds: Optional[float] = None,
249254
max_retry_attempts: Optional[int] = None,
255+
retry_configuration: Optional[Union[Literal["LEGACY"], dict]] = None,
250256
max_pool_connections: Optional[int] = None,
251257
extra_headers: Optional[Mapping[str, str]] = None,
252258
aws_access_key_id: Optional[str] = None,
@@ -277,6 +283,11 @@ def __init__(self,
277283
else:
278284
self._max_retry_attempts_exception = get_settings_value('max_retry_attempts')
279285

286+
if retry_configuration is not None:
287+
self._retry_configuration = retry_configuration
288+
else:
289+
self._retry_configuration = get_settings_value('retry_configuration')
290+
280291
if max_pool_connections is not None:
281292
self._max_pool_connections = max_pool_connections
282293
else:
@@ -399,15 +410,22 @@ def client(self) -> BotocoreBaseClientPrivate:
399410
# if the client does not have credentials, we create a new client
400411
# otherwise the client is permanently poisoned in the case of metadata service flakiness when using IAM roles
401412
if not self._client or (self._client._request_signer and not self._client._request_signer._credentials):
413+
# Check if we are using the "LEGACY" retry mode to keep previous PynamoDB
414+
# retry behavior, or if we are using the new retry configuration settings.
415+
if self._retry_configuration != "LEGACY":
416+
retries = self._retry_configuration
417+
else:
418+
retries = {
419+
'total_max_attempts': 1 + self._max_retry_attempts_exception,
420+
'mode': 'standard',
421+
}
422+
402423
config = botocore.client.Config(
403424
parameter_validation=False, # Disable unnecessary validation for performance
404425
connect_timeout=self._connect_timeout_seconds,
405426
read_timeout=self._read_timeout_seconds,
406427
max_pool_connections=self._max_pool_connections,
407-
retries={
408-
'total_max_attempts': 1 + self._max_retry_attempts_exception,
409-
'mode': 'standard',
410-
}
428+
retries=retries
411429
)
412430
self._client = cast(BotocoreBaseClientPrivate, self.session.create_client(SERVICE_NAME, self.region, endpoint_url=self.host, config=config))
413431

pynamodb/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'region': None,
1616
'max_pool_connections': 10,
1717
'extra_headers': None,
18+
'retry_configuration': 'LEGACY'
1819
}
1920

2021
OVERRIDE_SETTINGS_PATH = getenv('PYNAMODB_CONFIG', '/etc/pynamodb/global_default_settings.py')

0 commit comments

Comments
 (0)