Skip to content

Commit dd748a8

Browse files
committed
More updates with feedback
1 parent ff2c368 commit dd748a8

File tree

5 files changed

+20
-43
lines changed

5 files changed

+20
-43
lines changed

codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private void generateService(PythonWriter writer) {
8484
}
8585

8686
writer.addDependency(SmithyPythonDependency.SMITHY_CORE);
87-
writer.addImport("smithy_core.retries", "CachingRetryStrategyResolver");
87+
writer.addImport("smithy_core.retries", "RetryStrategyResolver");
8888
writer.write("""
8989
def __init__(self, config: $1T | None = None, plugins: list[$2T] | None = None):
9090
self._config = config or $1T()
@@ -98,7 +98,7 @@ def __init__(self, config: $1T | None = None, plugins: list[$2T] | None = None):
9898
for plugin in client_plugins:
9999
plugin(self._config)
100100
101-
self._retry_strategy_resolver = CachingRetryStrategyResolver()
101+
self._retry_strategy_resolver = RetryStrategyResolver()
102102
""", configSymbol, pluginSymbol, writer.consumer(w -> writeDefaultPlugins(w, defaultPlugins)));
103103

104104
var topDownIndex = TopDownIndex.of(model);

codegen/core/src/main/java/software/amazon/smithy/python/codegen/HttpProtocolTestGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ private void generateRequestTest(OperationShape operation, HttpRequestTestCase t
181181
} else {
182182
path = "";
183183
}
184-
writer.addImport("smithy_core.retries", "RetryStrategyOptions");
184+
writer.addImport("smithy_core.retries", "SimpleRetryStrategy");
185185
writeClientBlock(context.symbolProvider().toSymbol(service), testCase, Optional.of(() -> {
186186
writer.write("""
187187
config = $T(
188188
endpoint_uri="https://$L/$L",
189189
transport = $T(),
190-
retry_strategy=RetryStrategyOptions(max_attempts=1),
190+
retry_strategy=SimpleRetryStrategy(max_attempts=1),
191191
)
192192
""",
193193
CodegenUtils.getConfigSymbol(context.settings()),

packages/smithy-core/src/smithy_core/interfaces/retries.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
3-
from __future__ import annotations
4-
53
from dataclasses import dataclass
6-
from typing import TYPE_CHECKING, Literal, Protocol, runtime_checkable
7-
8-
if TYPE_CHECKING:
9-
from smithy_core.retries import RetryStrategyOptions
4+
from typing import Protocol, runtime_checkable
105

116

127
@runtime_checkable
@@ -57,9 +52,6 @@ class RetryToken(Protocol):
5752
"""Delay in seconds to wait before the retry attempt."""
5853

5954

60-
RetryStrategyType = Literal["simple"]
61-
62-
6355
@runtime_checkable
6456
class RetryStrategy(Protocol):
6557
"""Issuer of :py:class:`RetryToken`s."""
@@ -109,14 +101,3 @@ def record_success(self, *, token: RetryToken) -> None:
109101
:param token: The token used for the previous successful attempt.
110102
"""
111103
...
112-
113-
114-
class RetryStrategyResolver[RS: RetryStrategy](Protocol):
115-
"""Used to resolve a RetryStrategy from retry options."""
116-
117-
async def resolve_retry_strategy(self, *, options: RetryStrategyOptions) -> RS:
118-
"""Resolve the retry strategy from the provided options.
119-
120-
:param options: The retry strategy options to use for creating the strategy.
121-
"""
122-
...

packages/smithy-core/src/smithy_core/retries.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33
import random
4-
import threading
54
from collections.abc import Callable
65
from dataclasses import dataclass
76
from enum import Enum
7+
from functools import lru_cache
8+
from typing import Literal
89

910
from .exceptions import RetryError
1011
from .interfaces import retries as retries_interface
11-
from .interfaces.retries import RetryStrategy, RetryStrategyResolver, RetryStrategyType
12+
from .interfaces.retries import RetryStrategy
13+
14+
RetryStrategyType = Literal["simple"]
1215

1316

1417
@dataclass(kw_only=True, frozen=True)
@@ -22,33 +25,26 @@ class RetryStrategyOptions:
2225
"""Maximum number of attempts (initial attempt plus retries)."""
2326

2427

25-
class CachingRetryStrategyResolver(RetryStrategyResolver[RetryStrategy]):
28+
class RetryStrategyResolver:
2629
"""Retry strategy resolver that caches retry strategies based on configuration options.
2730
2831
This resolver caches retry strategy instances based on their configuration to reuse existing
29-
instances of RetryStrategy with the same settings.
32+
instances of RetryStrategy with the same settings. Uses LRU cache for thread-safe caching.
3033
"""
3134

32-
def __init__(self) -> None:
33-
self._cache: dict[RetryStrategyOptions, RetryStrategy] = {}
34-
self._lock = threading.Lock()
35-
3635
async def resolve_retry_strategy(
3736
self, *, options: RetryStrategyOptions
3837
) -> RetryStrategy:
3938
"""Resolve a retry strategy from the provided options, using cache when possible.
4039
4140
:param options: The retry strategy options to use for creating the strategy.
4241
"""
43-
with self._lock:
44-
if options not in self._cache:
45-
self._cache[options] = self._create_retry_strategy(
46-
options.retry_mode, options.max_attempts
47-
)
48-
return self._cache[options]
42+
return self._create_retry_strategy(options.retry_mode, options.max_attempts)
4943

44+
@staticmethod
45+
@lru_cache
5046
def _create_retry_strategy(
51-
self, retry_mode: RetryStrategyType, max_attempts: int
47+
retry_mode: RetryStrategyType, max_attempts: int
5248
) -> RetryStrategy:
5349
match retry_mode:
5450
case "simple":

packages/smithy-core/tests/unit/test_retries.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44
from smithy_core.exceptions import CallError, RetryError
55
from smithy_core.retries import (
6-
CachingRetryStrategyResolver,
6+
RetryStrategyResolver,
77
ExponentialRetryBackoffStrategy,
88
RetryStrategyOptions,
99
SimpleRetryStrategy,
@@ -109,7 +109,7 @@ def test_simple_retry_does_not_retry_unsafe() -> None:
109109

110110

111111
async def test_caching_retry_strategy_default_resolution() -> None:
112-
resolver = CachingRetryStrategyResolver()
112+
resolver = RetryStrategyResolver()
113113
options = RetryStrategyOptions()
114114

115115
strategy = await resolver.resolve_retry_strategy(options=options)
@@ -119,7 +119,7 @@ async def test_caching_retry_strategy_default_resolution() -> None:
119119

120120

121121
async def test_caching_retry_strategy_resolver_creates_strategies_by_options() -> None:
122-
resolver = CachingRetryStrategyResolver()
122+
resolver = RetryStrategyResolver()
123123

124124
options1 = RetryStrategyOptions(max_attempts=3)
125125
options2 = RetryStrategyOptions(max_attempts=5)
@@ -132,7 +132,7 @@ async def test_caching_retry_strategy_resolver_creates_strategies_by_options() -
132132

133133

134134
async def test_caching_retry_strategy_resolver_caches_strategies() -> None:
135-
resolver = CachingRetryStrategyResolver()
135+
resolver = RetryStrategyResolver()
136136

137137
options = RetryStrategyOptions(max_attempts=5)
138138
strategy1 = await resolver.resolve_retry_strategy(options=options)

0 commit comments

Comments
 (0)