|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""DynamoDB's retry defaults, for DynamoDB and DynamoDB Streams clients. |
| 4 | +
|
| 5 | +As in the Java generator's ``AwsDynamoDbRetryIntegration``, clients apply a |
| 6 | +plugin that gives the standard and adaptive retry modes DynamoDB's defaults: 4 |
| 7 | +attempts and a 25 ms backoff scale. A retry strategy or attempt count the user |
| 8 | +sets is kept. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +from collections.abc import Mapping |
| 14 | +from typing import TYPE_CHECKING, Final |
| 15 | + |
| 16 | +from .. import model as _model |
| 17 | +from ..dependencies import SMITHY_CORE |
| 18 | +from ..integrations import ClientIntegration, ClientMode |
| 19 | +from ..symbols import TypeReference |
| 20 | +from ._dependencies import SMITHY_AWS_CORE |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from ..clients import ClientPlan |
| 24 | + from ..generation import ClientSettings, GenerationContext |
| 25 | + |
| 26 | +__all__ = ["DynamoDbRetryIntegration"] |
| 27 | + |
| 28 | + |
| 29 | +class DynamoDbRetryIntegration(ClientIntegration): |
| 30 | + """DynamoDB's retry defaults, for the DynamoDB and DynamoDB Streams SDK IDs.""" |
| 31 | + |
| 32 | + def matches(self, service: _model.ServiceShape) -> bool: |
| 33 | + trait = service.get_trait(_SERVICE) |
| 34 | + return isinstance(trait, Mapping) and trait.get("sdkId") in _SDK_IDS |
| 35 | + |
| 36 | + def client_plugins( |
| 37 | + self, plan: ClientPlan, mode: ClientMode, package: str |
| 38 | + ) -> tuple[TypeReference, ...]: |
| 39 | + del plan |
| 40 | + return (TypeReference(_PLUGIN, _module(package, mode)),) |
| 41 | + |
| 42 | + def write_files( |
| 43 | + self, |
| 44 | + context: GenerationContext[ClientSettings], |
| 45 | + plan: ClientPlan, |
| 46 | + modes: tuple[ClientMode, ClientMode], |
| 47 | + ) -> None: |
| 48 | + del plan |
| 49 | + package = context.settings.import_name |
| 50 | + for mode in modes: |
| 51 | + module = _module(package, mode) |
| 52 | + writer = context.files.python_module( |
| 53 | + f"src/{module.replace('.', '/')}.py", module |
| 54 | + ) |
| 55 | + config = TypeReference( |
| 56 | + mode.config, f"{package}.{mode.directory.replace('/', '.')}config" |
| 57 | + ) |
| 58 | + writer.line("_DYNAMODB_DEFAULT_MAX_ATTEMPTS = 4") |
| 59 | + writer.line("_DYNAMODB_DEFAULT_BACKOFF_SCALE = 0.025") |
| 60 | + writer.line("_DYNAMODB_DEFAULT_MAX_BACKOFF = 20") |
| 61 | + writer.line() |
| 62 | + writer.line() |
| 63 | + writer.line("def ", _PLUGIN, "(config: ", config, ") -> None:") |
| 64 | + with writer.indent(): |
| 65 | + writer.line( |
| 66 | + '"""Apply DynamoDB\'s standard and adaptive mode retry defaults ' |
| 67 | + 'for any option left unset."""' |
| 68 | + ) |
| 69 | + writer.line("retry_strategy = config.retry_strategy") |
| 70 | + writer.line( |
| 71 | + "if retry_strategy is not None and not isinstance(retry_strategy, ", |
| 72 | + _OPTIONS, |
| 73 | + "):", |
| 74 | + ) |
| 75 | + with writer.indent(): |
| 76 | + writer.line("return") |
| 77 | + writer.line() |
| 78 | + writer.line("if isinstance(retry_strategy, ", _OPTIONS, "):") |
| 79 | + with writer.indent(): |
| 80 | + writer.line( |
| 81 | + "# Explicit options take precedence over separately " |
| 82 | + "resolved fields." |
| 83 | + ) |
| 84 | + writer.line("retry_mode = retry_strategy.retry_mode") |
| 85 | + writer.line("max_attempts = retry_strategy.max_attempts") |
| 86 | + writer.line("else:") |
| 87 | + with writer.indent(): |
| 88 | + writer.line( |
| 89 | + "# Fall back to the config's independently resolved retry " |
| 90 | + "fields." |
| 91 | + ) |
| 92 | + writer.line('retry_mode = config.retry_mode or "standard"') |
| 93 | + writer.line("max_attempts = config.max_attempts") |
| 94 | + writer.line( |
| 95 | + 'if config.source_of("max_attempts") == ', |
| 96 | + _CONFIG_SOURCE, |
| 97 | + ".DEFAULT:", |
| 98 | + ) |
| 99 | + with writer.indent(): |
| 100 | + writer.line("max_attempts = None") |
| 101 | + writer.line() |
| 102 | + writer.line('if retry_mode not in ("standard", "adaptive"):') |
| 103 | + with writer.indent(): |
| 104 | + writer.line("return") |
| 105 | + writer.line() |
| 106 | + writer.line( |
| 107 | + "strategy_cls = ", |
| 108 | + _ADAPTIVE, |
| 109 | + ' if retry_mode == "adaptive" else ', |
| 110 | + _STANDARD, |
| 111 | + ) |
| 112 | + writer.line("config.retry_strategy = strategy_cls(") |
| 113 | + with writer.indent(): |
| 114 | + writer.line("max_attempts=(") |
| 115 | + with writer.indent(): |
| 116 | + writer.line("max_attempts") |
| 117 | + writer.line("if max_attempts is not None") |
| 118 | + writer.line("else _DYNAMODB_DEFAULT_MAX_ATTEMPTS") |
| 119 | + writer.line("),") |
| 120 | + writer.line("backoff_strategy=", _BACKOFF, "(") |
| 121 | + with writer.indent(): |
| 122 | + writer.line( |
| 123 | + "backoff_scale_value=_DYNAMODB_DEFAULT_BACKOFF_SCALE," |
| 124 | + ) |
| 125 | + writer.line("max_backoff=_DYNAMODB_DEFAULT_MAX_BACKOFF,") |
| 126 | + writer.line("jitter_type=", _JITTER, ".FULL,") |
| 127 | + writer.line("),") |
| 128 | + writer.line(")") |
| 129 | + |
| 130 | + |
| 131 | +def _module(package: str, mode: ClientMode) -> str: |
| 132 | + return f"{package}.{mode.directory.replace('/', '.')}retries" |
| 133 | + |
| 134 | + |
| 135 | +def _retries(name: str) -> TypeReference: |
| 136 | + return TypeReference(name, "smithy_core.retries", dependencies=(SMITHY_CORE,)) |
| 137 | + |
| 138 | + |
| 139 | +_SERVICE: Final = _model.ShapeId("aws.api", "service") |
| 140 | +_SDK_IDS: Final = frozenset({"DynamoDB", "DynamoDB Streams"}) |
| 141 | +_PLUGIN: Final = "dynamodb_retry_plugin" |
| 142 | +_OPTIONS: Final = _retries("RetryStrategyOptions") |
| 143 | +_STANDARD: Final = _retries("StandardRetryStrategy") |
| 144 | +_ADAPTIVE: Final = _retries("AdaptiveRetryStrategy") |
| 145 | +_BACKOFF: Final = _retries("ExponentialRetryBackoffStrategy") |
| 146 | +_JITTER: Final = _retries("ExponentialBackoffJitterType") |
| 147 | +_CONFIG_SOURCE: Final = TypeReference( |
| 148 | + "ConfigSource", "smithy_aws_core.config.types", dependencies=(SMITHY_AWS_CORE,) |
| 149 | +) |
0 commit comments