|
11 | 11 | ]
|
12 | 12 | import logging
|
13 | 13 | import os
|
14 |
| -import re |
15 | 14 | from dataclasses import dataclass
|
16 |
| -from typing import ( |
17 |
| - TYPE_CHECKING, |
18 |
| - ClassVar, |
19 |
| - Generic, |
20 |
| - Literal, |
21 |
| - Optional, |
22 |
| - Pattern, |
23 |
| - TypeVar, |
24 |
| - Union, |
25 |
| - cast, |
26 |
| -) |
| 15 | +from typing import TYPE_CHECKING, Generic, Literal, Optional, TypeVar, Union, cast |
27 | 16 |
|
28 | 17 | import boto3
|
29 | 18 | from aibs_informatics_core.models.aws.core import AWSRegion
|
|
32 | 21 | from boto3 import Session
|
33 | 22 | from boto3.resources.base import ServiceResource
|
34 | 23 | from botocore.client import BaseClient, ClientError
|
| 24 | +from botocore.config import Config |
35 | 25 | from botocore.session import Session as BotocoreSession
|
36 | 26 |
|
37 | 27 | if TYPE_CHECKING: # pragma: no cover
|
@@ -249,8 +239,22 @@ def get_client(
|
249 | 239 | region_name = get_region(region=region or kwargs.get("region_name"))
|
250 | 240 | if region_name:
|
251 | 241 | kwargs["region_name"] = region_name
|
| 242 | + |
| 243 | + # If config for our client is not set, we want to set it to use "standard" mode |
| 244 | + # (default is "legacy") and increase the number of retries to 5 (default is 3) |
| 245 | + # See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html#available-retry-modes |
| 246 | + config: Optional[Config] = kwargs.pop("config", None) |
| 247 | + default_config = Config( |
| 248 | + connect_timeout=120, read_timeout=120, retries={"max_attempts": 6, "mode": "standard"} |
| 249 | + ) |
| 250 | + if config is None: |
| 251 | + config = default_config |
| 252 | + else: |
| 253 | + # Have values in pre-existing config (if it exists) take precedence over default_config |
| 254 | + config = default_config.merge(other_config=config) |
| 255 | + |
252 | 256 | session = session or boto3.Session()
|
253 |
| - return session.client(service, **kwargs) |
| 257 | + return session.client(service, config=config, **kwargs) |
254 | 258 |
|
255 | 259 |
|
256 | 260 | @cache
|
@@ -280,8 +284,22 @@ def get_resource(
|
280 | 284 | region_name = get_region(region=region or kwargs.get("region_name"))
|
281 | 285 | if region_name:
|
282 | 286 | kwargs["region_name"] = region_name
|
| 287 | + |
| 288 | + # If config for our client is not set, we want to set it to use "standard" mode |
| 289 | + # (default is "legacy") and increase the number of retries to 5 (default is 3) |
| 290 | + # See: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/retries.html#available-retry-modes |
| 291 | + config: Optional[Config] = kwargs.pop("config", None) |
| 292 | + default_config = Config( |
| 293 | + connect_timeout=120, read_timeout=120, retries={"max_attempts": 6, "mode": "standard"} |
| 294 | + ) |
| 295 | + if config is None: |
| 296 | + config = default_config |
| 297 | + else: |
| 298 | + # Have values in pre-existing config (if it exists) take precedence over default_config |
| 299 | + config = default_config.merge(other_config=config) |
| 300 | + |
283 | 301 | session = session or boto3.Session()
|
284 |
| - return session.resource(service, **kwargs) |
| 302 | + return session.resource(service, config=config, **kwargs) |
285 | 303 |
|
286 | 304 |
|
287 | 305 | @dataclass
|
|
0 commit comments