capo-sagemaker-runtime-http2 0.12.0__tar.gz → 0.14.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {capo_sagemaker_runtime_http2-0.12.0/src/capo_sagemaker_runtime_http2.egg-info → capo_sagemaker_runtime_http2-0.14.0}/PKG-INFO +3 -1
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/README.md +2 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/pyproject.toml +1 -1
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_auth/_sigv4.py +5 -2
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_services/_aws_config.py +15 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_services/_pipeline.py +207 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_services/async_sage_maker_runtime_http2.py +14 -1
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_services/sage_maker_runtime_http2.py +14 -1
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0/src/capo_sagemaker_runtime_http2.egg-info}/PKG-INFO +3 -1
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/tests/test_sigv4.py +33 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/LICENSE +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/setup.cfg +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/__init__.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_async.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_auth/_identity.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_auth/_providers.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_auth/_signers.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_auth/_zapros_handler.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_aws.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_body.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_iter.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_operations/amazon_sage_maker_runtime_http2/invoke_endpoint_with_bidirectional_stream.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_pagination.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_protocol/__init__.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_protocol/errors.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_protocol/eventstream.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_protocol/serialize.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_protocol/xml.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_rule_engine/__init__.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_rule_engine/_aws_partition.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_rule_engine/_endpoint_rule_set.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/_rule_engine/_endpoint_runtime.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/__init__.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/_base.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/input_validation_error.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/internal_server_error.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/internal_stream_failure.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/model_error.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/model_stream_error.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/errors/service_unavailable_error.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/py.typed +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/_prelude/blob.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/_prelude/timestamp.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/_unknown.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/invoke_endpoint_with_bidirectional_stream_input.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/invoke_endpoint_with_bidirectional_stream_output.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/request_payload_part.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/request_stream_event.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/response_payload_part.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/response_stream_event.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2/types/sensitive_blob.py +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2.egg-info/SOURCES.txt +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2.egg-info/dependency_links.txt +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2.egg-info/requires.txt +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/src/capo_sagemaker_runtime_http2.egg-info/top_level.txt +0 -0
- {capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/tests/test_body.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: capo-sagemaker-runtime-http2
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.14.0
|
|
4
4
|
Summary: Typed, async Python SDK for AWS Sagemaker Runtime Http2.
|
|
5
5
|
Project-URL: Homepage, https://capo-sdk.dev/
|
|
6
6
|
Project-URL: Documentation, https://capo-sdk.dev/services/sagemaker-runtime-http2/
|
|
@@ -108,6 +108,8 @@ The SDK retries failed operations automatically. Retry behaviour follows the Smi
|
|
|
108
108
|
|
|
109
109
|
The number of attempts defaults to 3 and can be changed at the client level via `retry_max_attempts`, or per call via `config_overrides`.
|
|
110
110
|
|
|
111
|
+
`retry_mode="adaptive"` (or `AWS_RETRY_MODE=adaptive`, or `retry_mode = adaptive` in the AWS profile) also slows the whole client down once the service starts throttling it, then speeds back up as requests succeed. The default is `standard`.
|
|
112
|
+
|
|
111
113
|
```python
|
|
112
114
|
from capo_sagemaker_runtime_http2 import AsyncSageMakerRuntimeHTTP2Client
|
|
113
115
|
|
|
@@ -76,6 +76,8 @@ The SDK retries failed operations automatically. Retry behaviour follows the Smi
|
|
|
76
76
|
|
|
77
77
|
The number of attempts defaults to 3 and can be changed at the client level via `retry_max_attempts`, or per call via `config_overrides`.
|
|
78
78
|
|
|
79
|
+
`retry_mode="adaptive"` (or `AWS_RETRY_MODE=adaptive`, or `retry_mode = adaptive` in the AWS profile) also slows the whole client down once the service starts throttling it, then speeds back up as requests succeed. The default is `standard`.
|
|
80
|
+
|
|
79
81
|
```python
|
|
80
82
|
from capo_sagemaker_runtime_http2 import AsyncSageMakerRuntimeHTTP2Client
|
|
81
83
|
|
|
@@ -471,12 +471,15 @@ def presign_sigv4(
|
|
|
471
471
|
*,
|
|
472
472
|
expires_in: int = 3600,
|
|
473
473
|
now: _dt.datetime | None = None,
|
|
474
|
+
payload_hash: str = "UNSIGNED-PAYLOAD",
|
|
474
475
|
) -> Request:
|
|
475
476
|
"""Return a new ``Request`` whose URL carries SigV4 query-string auth.
|
|
476
477
|
|
|
477
478
|
The signature travels in the URL (``X-Amz-*`` query params), so the result
|
|
478
479
|
is usable standalone (browser, curl). Payload is signed as
|
|
479
|
-
``UNSIGNED-PAYLOAD``, so the body is not bound by the signature
|
|
480
|
+
``UNSIGNED-PAYLOAD``, so the body is not bound by the signature, unless
|
|
481
|
+
``payload_hash`` says otherwise: an RDS or DSQL auth token signs the
|
|
482
|
+
SHA-256 of an empty body instead.
|
|
480
483
|
|
|
481
484
|
``expires_in`` is the validity window in seconds; range 1..604800 (7 days),
|
|
482
485
|
bounded by the max lifetime of the derived signing key.
|
|
@@ -542,7 +545,7 @@ def presign_sigv4(
|
|
|
542
545
|
f"{canonical_query}\n"
|
|
543
546
|
f"{canonical_headers}\n"
|
|
544
547
|
f"{signed_headers}\n"
|
|
545
|
-
f"
|
|
548
|
+
f"{payload_hash}"
|
|
546
549
|
)
|
|
547
550
|
|
|
548
551
|
string_to_sign = (
|
|
@@ -31,6 +31,7 @@ class AwsSettings:
|
|
|
31
31
|
use_fips: bool
|
|
32
32
|
use_dual_stack: bool
|
|
33
33
|
max_attempts: int
|
|
34
|
+
retry_mode: str
|
|
34
35
|
|
|
35
36
|
|
|
36
37
|
def _env_bool(name: str) -> bool | None:
|
|
@@ -109,12 +110,22 @@ def load_aws_settings() -> AwsSettings:
|
|
|
109
110
|
use_dual_stack = False
|
|
110
111
|
max_attempts_raw = os.environ.get("AWS_MAX_ATTEMPTS") or profile.get("max_attempts")
|
|
111
112
|
max_attempts = int(max_attempts_raw) if max_attempts_raw else 3
|
|
113
|
+
retry_mode = (
|
|
114
|
+
(os.environ.get("AWS_RETRY_MODE") or profile.get("retry_mode") or "standard")
|
|
115
|
+
.strip()
|
|
116
|
+
.lower()
|
|
117
|
+
)
|
|
118
|
+
if retry_mode not in ("legacy", "standard", "adaptive"):
|
|
119
|
+
raise ValueError(
|
|
120
|
+
f"invalid retry_mode {retry_mode!r}: expected 'legacy', 'standard' or 'adaptive'"
|
|
121
|
+
)
|
|
112
122
|
return AwsSettings(
|
|
113
123
|
region=region,
|
|
114
124
|
endpoint=endpoint,
|
|
115
125
|
use_fips=use_fips,
|
|
116
126
|
use_dual_stack=use_dual_stack,
|
|
117
127
|
max_attempts=max_attempts,
|
|
128
|
+
retry_mode=retry_mode,
|
|
118
129
|
)
|
|
119
130
|
|
|
120
131
|
|
|
@@ -134,6 +145,8 @@ def aws_config() -> Interceptor[TInput, TOutput]:
|
|
|
134
145
|
options.use_dual_stack = settings.use_dual_stack
|
|
135
146
|
if options.retry_max_attempts is None:
|
|
136
147
|
options.retry_max_attempts = settings.max_attempts
|
|
148
|
+
if options.retry_mode is None:
|
|
149
|
+
options.retry_mode = settings.retry_mode
|
|
137
150
|
return next(request)
|
|
138
151
|
|
|
139
152
|
return interceptor
|
|
@@ -155,6 +168,8 @@ def aaws_config() -> AsyncInterceptor[TInput, TOutput]:
|
|
|
155
168
|
options.use_dual_stack = settings.use_dual_stack
|
|
156
169
|
if options.retry_max_attempts is None:
|
|
157
170
|
options.retry_max_attempts = settings.max_attempts
|
|
171
|
+
if options.retry_mode is None:
|
|
172
|
+
options.retry_mode = settings.retry_mode
|
|
158
173
|
return await next(request)
|
|
159
174
|
|
|
160
175
|
return interceptor
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
|
+
import math
|
|
4
5
|
import random
|
|
5
6
|
import threading
|
|
6
7
|
import time
|
|
@@ -88,15 +89,134 @@ class ThreadSafeRetryQuota(RetryQuota):
|
|
|
88
89
|
super().release(last_retry_cost)
|
|
89
90
|
|
|
90
91
|
|
|
92
|
+
class RateLimiter:
|
|
93
|
+
"""Send tokens shared by all operations of one client in adaptive retry mode: the rate drops on throttling and regrows on success."""
|
|
94
|
+
|
|
95
|
+
MIN_FILL_RATE: float = 0.5
|
|
96
|
+
MIN_CAPACITY: float = 1.0
|
|
97
|
+
BETA: float = 0.7
|
|
98
|
+
SCALE_CONSTANT: float = 0.4
|
|
99
|
+
SMOOTHING: float = 0.8
|
|
100
|
+
MAX_RATE_SCALE: float = 2.0
|
|
101
|
+
|
|
102
|
+
def __init__(self, clock: Callable[[], float] = time.monotonic) -> None:
|
|
103
|
+
self._clock = clock
|
|
104
|
+
now = clock()
|
|
105
|
+
self._enabled: bool = False
|
|
106
|
+
self._fill_rate: float = 1.0
|
|
107
|
+
self._max_capacity: float = 1.0
|
|
108
|
+
self._capacity: float = 0.0
|
|
109
|
+
self._last_refill: float = now
|
|
110
|
+
self._last_max_rate: float = 0.0
|
|
111
|
+
self._last_throttle: float = now
|
|
112
|
+
self._measured_rate: float = 0.0
|
|
113
|
+
self._rate_bucket: float = math.floor(now)
|
|
114
|
+
self._request_count: int = 0
|
|
115
|
+
|
|
116
|
+
def acquire(self) -> float:
|
|
117
|
+
"""Take a send token: return 0, or the seconds to wait before asking again."""
|
|
118
|
+
if not self._enabled:
|
|
119
|
+
return 0.0
|
|
120
|
+
self._refill(self._clock())
|
|
121
|
+
if self._capacity >= 1:
|
|
122
|
+
self._capacity -= 1
|
|
123
|
+
return 0.0
|
|
124
|
+
return (1 - self._capacity) / self._fill_rate
|
|
125
|
+
|
|
126
|
+
def update(self, is_throttling: bool) -> None:
|
|
127
|
+
"""Adjust the send rate after an attempt: cut it on throttling, regrow it otherwise."""
|
|
128
|
+
now = self._clock()
|
|
129
|
+
self._measure(now)
|
|
130
|
+
self._adjust(is_throttling, now)
|
|
131
|
+
|
|
132
|
+
def throttled_mid_stream(self) -> None:
|
|
133
|
+
"""Cut the send rate for throttling reported inside an open event stream. No new request was sent, so the measured rate is left alone."""
|
|
134
|
+
self._adjust(True, self._clock())
|
|
135
|
+
|
|
136
|
+
def _adjust(self, is_throttling: bool, now: float) -> None:
|
|
137
|
+
if is_throttling:
|
|
138
|
+
rate = (
|
|
139
|
+
min(self._measured_rate, self._fill_rate)
|
|
140
|
+
if self._enabled
|
|
141
|
+
else self._measured_rate
|
|
142
|
+
)
|
|
143
|
+
self._last_max_rate = rate
|
|
144
|
+
self._last_throttle = now
|
|
145
|
+
new_rate = rate * self.BETA
|
|
146
|
+
self._enabled = True
|
|
147
|
+
else:
|
|
148
|
+
window = (self._last_max_rate * (1 - self.BETA) / self.SCALE_CONSTANT) ** (
|
|
149
|
+
1 / 3
|
|
150
|
+
)
|
|
151
|
+
new_rate = (
|
|
152
|
+
self.SCALE_CONSTANT * (now - self._last_throttle - window) ** 3
|
|
153
|
+
+ self._last_max_rate
|
|
154
|
+
)
|
|
155
|
+
self._set_rate(min(new_rate, self.MAX_RATE_SCALE * self._measured_rate), now)
|
|
156
|
+
if is_throttling:
|
|
157
|
+
logger.debug(
|
|
158
|
+
"throttled, send rate lowered to %.2f requests/s",
|
|
159
|
+
self._fill_rate,
|
|
160
|
+
extra={"send_rate": self._fill_rate},
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
def _refill(self, now: float) -> None:
|
|
164
|
+
self._capacity = min(
|
|
165
|
+
self._max_capacity,
|
|
166
|
+
self._capacity + (now - self._last_refill) * self._fill_rate,
|
|
167
|
+
)
|
|
168
|
+
self._last_refill = now
|
|
169
|
+
|
|
170
|
+
def _measure(self, now: float) -> None:
|
|
171
|
+
# Requests per second, counted in half-second buckets and smoothed.
|
|
172
|
+
bucket = math.floor(now * 2) / 2
|
|
173
|
+
self._request_count += 1
|
|
174
|
+
if bucket > self._rate_bucket:
|
|
175
|
+
current = self._request_count / (bucket - self._rate_bucket)
|
|
176
|
+
self._measured_rate = current * self.SMOOTHING + self._measured_rate * (
|
|
177
|
+
1 - self.SMOOTHING
|
|
178
|
+
)
|
|
179
|
+
self._request_count = 0
|
|
180
|
+
self._rate_bucket = bucket
|
|
181
|
+
|
|
182
|
+
def _set_rate(self, rate: float, now: float) -> None:
|
|
183
|
+
self._refill(now)
|
|
184
|
+
self._fill_rate = max(rate, self.MIN_FILL_RATE)
|
|
185
|
+
self._max_capacity = max(rate, self.MIN_CAPACITY)
|
|
186
|
+
self._capacity = min(self._capacity, self._max_capacity)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class ThreadSafeRateLimiter(RateLimiter):
|
|
190
|
+
"""A ``RateLimiter`` that sync clients can share across threads."""
|
|
191
|
+
|
|
192
|
+
def __init__(self, clock: Callable[[], float] = time.monotonic) -> None:
|
|
193
|
+
super().__init__(clock)
|
|
194
|
+
self._lock: threading.Lock = threading.Lock()
|
|
195
|
+
|
|
196
|
+
def acquire(self) -> float:
|
|
197
|
+
with self._lock:
|
|
198
|
+
return super().acquire()
|
|
199
|
+
|
|
200
|
+
def update(self, is_throttling: bool) -> None:
|
|
201
|
+
with self._lock:
|
|
202
|
+
super().update(is_throttling)
|
|
203
|
+
|
|
204
|
+
def throttled_mid_stream(self) -> None:
|
|
205
|
+
with self._lock:
|
|
206
|
+
super().throttled_mid_stream()
|
|
207
|
+
|
|
208
|
+
|
|
91
209
|
@dataclass
|
|
92
210
|
class OperationOptions:
|
|
93
211
|
client: Client
|
|
94
212
|
retry_quota: RetryQuota
|
|
213
|
+
rate_limiter: RateLimiter
|
|
95
214
|
use_dual_stack: bool | None = None
|
|
96
215
|
use_fips: bool | None = None
|
|
97
216
|
endpoint: str | None = None
|
|
98
217
|
region: str | None = None
|
|
99
218
|
retry_max_attempts: int | None = None
|
|
219
|
+
retry_mode: str | None = None
|
|
100
220
|
credentials_provider: IdentityProvider[Credentials] | None = None
|
|
101
221
|
anonymous: bool | None = None
|
|
102
222
|
|
|
@@ -105,11 +225,13 @@ class OperationOptions:
|
|
|
105
225
|
class AsyncOperationOptions:
|
|
106
226
|
client: AsyncClient
|
|
107
227
|
retry_quota: RetryQuota
|
|
228
|
+
rate_limiter: RateLimiter
|
|
108
229
|
use_dual_stack: bool | None = None
|
|
109
230
|
use_fips: bool | None = None
|
|
110
231
|
endpoint: str | None = None
|
|
111
232
|
region: str | None = None
|
|
112
233
|
retry_max_attempts: int | None = None
|
|
234
|
+
retry_mode: str | None = None
|
|
113
235
|
credentials_provider: IdentityProvider[Credentials] | None = None
|
|
114
236
|
anonymous: bool | None = None
|
|
115
237
|
|
|
@@ -428,6 +550,91 @@ def aretry() -> AsyncInterceptor[TInput, TOutput]:
|
|
|
428
550
|
return interceptor
|
|
429
551
|
|
|
430
552
|
|
|
553
|
+
def rate_limit() -> Interceptor[TInput, TOutput]:
|
|
554
|
+
def interceptor(
|
|
555
|
+
request: OperationRequest[TInput], next: NextFn[TInput, TOutput]
|
|
556
|
+
) -> OperationResponse[TOutput]:
|
|
557
|
+
if request.options.retry_mode != "adaptive":
|
|
558
|
+
return next(request)
|
|
559
|
+
limiter = request.options.rate_limiter
|
|
560
|
+
while (wait := limiter.acquire()) > 0:
|
|
561
|
+
time.sleep(wait)
|
|
562
|
+
try:
|
|
563
|
+
response = next(request)
|
|
564
|
+
except Exception as exc:
|
|
565
|
+
limiter.update(isinstance(exc, ServiceError) and exc.is_throttling_error)
|
|
566
|
+
raise
|
|
567
|
+
limiter.update(False)
|
|
568
|
+
return response
|
|
569
|
+
|
|
570
|
+
return interceptor
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
def arate_limit() -> AsyncInterceptor[TInput, TOutput]:
|
|
574
|
+
async def interceptor(
|
|
575
|
+
request: AsyncOperationRequest[TInput], next: AsyncNextFn[TInput, TOutput]
|
|
576
|
+
) -> AsyncOperationResponse[TOutput]:
|
|
577
|
+
if request.options.retry_mode != "adaptive":
|
|
578
|
+
return await next(request)
|
|
579
|
+
limiter = request.options.rate_limiter
|
|
580
|
+
while (wait := limiter.acquire()) > 0:
|
|
581
|
+
await anysleep(wait)
|
|
582
|
+
try:
|
|
583
|
+
response = await next(request)
|
|
584
|
+
except Exception as exc:
|
|
585
|
+
limiter.update(isinstance(exc, ServiceError) and exc.is_throttling_error)
|
|
586
|
+
raise
|
|
587
|
+
limiter.update(False)
|
|
588
|
+
return response
|
|
589
|
+
|
|
590
|
+
return interceptor
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def _on_event_stream_error(
|
|
594
|
+
exc: ServiceError, options: OperationOptions | AsyncOperationOptions
|
|
595
|
+
) -> None:
|
|
596
|
+
# An error event arrives after a 200 response, so raise_error never saw it:
|
|
597
|
+
# mark it by its code here. It is not retried, but adaptive mode slows down.
|
|
598
|
+
_apply_retry_rules(exc, exc.response)
|
|
599
|
+
if exc.is_throttling_error and options.retry_mode == "adaptive":
|
|
600
|
+
options.rate_limiter.throttled_mid_stream()
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
def watch_event_stream(output: Any, member: str, options: OperationOptions) -> None:
|
|
604
|
+
"""Wrap the event stream ``output[member]`` so the errors it raises get their retry flags and reach the rate limiter."""
|
|
605
|
+
if member not in output:
|
|
606
|
+
return
|
|
607
|
+
events = output[member]
|
|
608
|
+
|
|
609
|
+
def watched() -> Iterator[Any]:
|
|
610
|
+
try:
|
|
611
|
+
yield from events
|
|
612
|
+
except ServiceError as exc:
|
|
613
|
+
_on_event_stream_error(exc, options)
|
|
614
|
+
raise
|
|
615
|
+
|
|
616
|
+
output[member] = watched()
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
def awatch_event_stream(
|
|
620
|
+
output: Any, member: str, options: AsyncOperationOptions
|
|
621
|
+
) -> None:
|
|
622
|
+
"""Wrap the event stream ``output[member]`` so the errors it raises get their retry flags and reach the rate limiter."""
|
|
623
|
+
if member not in output:
|
|
624
|
+
return
|
|
625
|
+
events = output[member]
|
|
626
|
+
|
|
627
|
+
async def watched() -> AsyncIterator[Any]:
|
|
628
|
+
try:
|
|
629
|
+
async for event in events:
|
|
630
|
+
yield event
|
|
631
|
+
except ServiceError as exc:
|
|
632
|
+
_on_event_stream_error(exc, options)
|
|
633
|
+
raise
|
|
634
|
+
|
|
635
|
+
output[member] = watched()
|
|
636
|
+
|
|
637
|
+
|
|
431
638
|
def log_operation(operation_logging: OperationLogging) -> Interceptor[TInput, TOutput]:
|
|
432
639
|
operation = operation_logging.operation
|
|
433
640
|
redact_input = operation_logging.redact_input
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import warnings
|
|
4
4
|
from collections.abc import AsyncGenerator, AsyncIterator
|
|
5
5
|
from contextlib import asynccontextmanager
|
|
6
|
-
from typing import TYPE_CHECKING, Any, Iterable, Optional
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Iterable, Literal, Optional
|
|
7
7
|
|
|
8
8
|
from typing_extensions import Self, TypedDict
|
|
9
9
|
from zapros import AsyncBaseHandler, AsyncClient
|
|
@@ -28,10 +28,13 @@ from capo_sagemaker_runtime_http2._services._pipeline import (
|
|
|
28
28
|
AsyncOperationRequest,
|
|
29
29
|
AsyncOperationResponse,
|
|
30
30
|
OperationLogging,
|
|
31
|
+
RateLimiter,
|
|
31
32
|
RetryQuota,
|
|
32
33
|
aexecute_pipeline,
|
|
33
34
|
alog_operation,
|
|
35
|
+
arate_limit,
|
|
34
36
|
aretry,
|
|
37
|
+
awatch_event_stream,
|
|
35
38
|
)
|
|
36
39
|
|
|
37
40
|
if TYPE_CHECKING:
|
|
@@ -43,6 +46,7 @@ if TYPE_CHECKING:
|
|
|
43
46
|
class AsyncSageMakerRuntimeHTTP2ClientConfig(TypedDict, total=False, closed=True):
|
|
44
47
|
operation_interceptors: Iterable[AsyncInterceptor[Any, Any]]
|
|
45
48
|
retry_max_attempts: int | None
|
|
49
|
+
retry_mode: Literal["standard", "adaptive"] | None
|
|
46
50
|
use_dual_stack: bool | None
|
|
47
51
|
use_fips: bool | None
|
|
48
52
|
endpoint: str | None
|
|
@@ -58,6 +62,7 @@ class AsyncSageMakerRuntimeHTTP2Client:
|
|
|
58
62
|
http_handler: HTTP handler for sending requests. If not provided, creates a default handler.
|
|
59
63
|
operation_interceptors: Interceptors that wrap every operation call. If not provided, defaults to an empty list.
|
|
60
64
|
retry_max_attempts: Maximum number of times to retry a failed operation. Defaults to 3.
|
|
65
|
+
retry_mode: ``"standard"`` (the default) or ``"adaptive"``, which also slows the whole client down when the service throttles it.
|
|
61
66
|
use_dual_stack: The value of the ``AWS::UseDualStack`` endpoint parameter.
|
|
62
67
|
use_fips: The value of the ``AWS::UseFIPS`` endpoint parameter.
|
|
63
68
|
endpoint: The value of the ``SDK::Endpoint`` endpoint parameter.
|
|
@@ -72,6 +77,7 @@ class AsyncSageMakerRuntimeHTTP2Client:
|
|
|
72
77
|
http_handler: AsyncBaseHandler | None = None,
|
|
73
78
|
operation_interceptors: Iterable[AsyncInterceptor[Any, Any]] | None = None,
|
|
74
79
|
retry_max_attempts: int | None = None,
|
|
80
|
+
retry_mode: Literal["standard", "adaptive"] | None = None,
|
|
75
81
|
use_dual_stack: bool | None = None,
|
|
76
82
|
use_fips: bool | None = None,
|
|
77
83
|
endpoint: str | None = None,
|
|
@@ -88,6 +94,8 @@ class AsyncSageMakerRuntimeHTTP2Client:
|
|
|
88
94
|
self._client = client.wrap_with_middleware(lambda next: AuthMiddleware(next))
|
|
89
95
|
# One retry quota for all of this client's operations.
|
|
90
96
|
self._retry_quota = RetryQuota()
|
|
97
|
+
# One send-rate limiter too, used when retry_mode is adaptive.
|
|
98
|
+
self._rate_limiter = RateLimiter()
|
|
91
99
|
if credentials is not None and credentials_provider is not None:
|
|
92
100
|
warnings.warn(
|
|
93
101
|
"Both credentials and credentials_provider given; provider takes precedence"
|
|
@@ -105,6 +113,7 @@ class AsyncSageMakerRuntimeHTTP2Client:
|
|
|
105
113
|
{
|
|
106
114
|
"operation_interceptors": operation_interceptors or [],
|
|
107
115
|
"retry_max_attempts": retry_max_attempts,
|
|
116
|
+
"retry_mode": retry_mode,
|
|
108
117
|
"use_dual_stack": use_dual_stack,
|
|
109
118
|
"use_fips": use_fips,
|
|
110
119
|
"endpoint": endpoint,
|
|
@@ -129,12 +138,15 @@ class AsyncSageMakerRuntimeHTTP2Client:
|
|
|
129
138
|
if operation_logging is not None:
|
|
130
139
|
interceptors_.append(alog_operation(operation_logging))
|
|
131
140
|
interceptors_.append(aretry())
|
|
141
|
+
interceptors_.append(arate_limit())
|
|
132
142
|
options_: AsyncOperationOptions = AsyncOperationOptions(
|
|
133
143
|
client=self._client,
|
|
134
144
|
retry_quota=self._retry_quota,
|
|
145
|
+
rate_limiter=self._rate_limiter,
|
|
135
146
|
retry_max_attempts=overrides.get(
|
|
136
147
|
"retry_max_attempts", self._config.get("retry_max_attempts")
|
|
137
148
|
),
|
|
149
|
+
retry_mode=overrides.get("retry_mode", self._config.get("retry_mode")),
|
|
138
150
|
use_dual_stack=overrides.get(
|
|
139
151
|
"use_dual_stack", self._config.get("use_dual_stack")
|
|
140
152
|
),
|
|
@@ -191,6 +203,7 @@ class AsyncSageMakerRuntimeHTTP2Client:
|
|
|
191
203
|
) = await capo_sagemaker_runtime_http2._operations.amazon_sage_maker_runtime_http2.invoke_endpoint_with_bidirectional_stream.async_invoke_endpoint_with_bidirectional_stream(
|
|
192
204
|
req.options, req.input
|
|
193
205
|
)
|
|
206
|
+
awatch_event_stream(output, "body", req.options)
|
|
194
207
|
return AsyncOperationResponse(output=output, response=http_response)
|
|
195
208
|
|
|
196
209
|
def _redact_input(
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import warnings
|
|
4
4
|
from collections.abc import Generator, Iterator
|
|
5
5
|
from contextlib import contextmanager
|
|
6
|
-
from typing import TYPE_CHECKING, Any, Iterable, Optional
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Iterable, Literal, Optional
|
|
7
7
|
|
|
8
8
|
from typing_extensions import Self, TypedDict
|
|
9
9
|
from zapros import BaseHandler, Client
|
|
@@ -28,10 +28,13 @@ from capo_sagemaker_runtime_http2._services._pipeline import (
|
|
|
28
28
|
OperationOptions,
|
|
29
29
|
OperationRequest,
|
|
30
30
|
OperationResponse,
|
|
31
|
+
ThreadSafeRateLimiter,
|
|
31
32
|
ThreadSafeRetryQuota,
|
|
32
33
|
execute_pipeline,
|
|
33
34
|
log_operation,
|
|
35
|
+
rate_limit,
|
|
34
36
|
retry,
|
|
37
|
+
watch_event_stream,
|
|
35
38
|
)
|
|
36
39
|
|
|
37
40
|
if TYPE_CHECKING:
|
|
@@ -43,6 +46,7 @@ if TYPE_CHECKING:
|
|
|
43
46
|
class SageMakerRuntimeHTTP2ClientConfig(TypedDict, total=False, closed=True):
|
|
44
47
|
operation_interceptors: Iterable[Interceptor[Any, Any]]
|
|
45
48
|
retry_max_attempts: int | None
|
|
49
|
+
retry_mode: Literal["standard", "adaptive"] | None
|
|
46
50
|
use_dual_stack: bool | None
|
|
47
51
|
use_fips: bool | None
|
|
48
52
|
endpoint: str | None
|
|
@@ -58,6 +62,7 @@ class SageMakerRuntimeHTTP2Client:
|
|
|
58
62
|
http_handler: HTTP handler for sending requests. If not provided, creates a default handler.
|
|
59
63
|
operation_interceptors: Interceptors that wrap every operation call. If not provided, defaults to an empty list.
|
|
60
64
|
retry_max_attempts: Maximum number of times to retry a failed operation. Defaults to 3.
|
|
65
|
+
retry_mode: ``"standard"`` (the default) or ``"adaptive"``, which also slows the whole client down when the service throttles it.
|
|
61
66
|
use_dual_stack: The value of the ``AWS::UseDualStack`` endpoint parameter.
|
|
62
67
|
use_fips: The value of the ``AWS::UseFIPS`` endpoint parameter.
|
|
63
68
|
endpoint: The value of the ``SDK::Endpoint`` endpoint parameter.
|
|
@@ -72,6 +77,7 @@ class SageMakerRuntimeHTTP2Client:
|
|
|
72
77
|
http_handler: BaseHandler | None = None,
|
|
73
78
|
operation_interceptors: Iterable[Interceptor[Any, Any]] | None = None,
|
|
74
79
|
retry_max_attempts: int | None = None,
|
|
80
|
+
retry_mode: Literal["standard", "adaptive"] | None = None,
|
|
75
81
|
use_dual_stack: bool | None = None,
|
|
76
82
|
use_fips: bool | None = None,
|
|
77
83
|
endpoint: str | None = None,
|
|
@@ -88,6 +94,8 @@ class SageMakerRuntimeHTTP2Client:
|
|
|
88
94
|
self._client = client.wrap_with_middleware(lambda next: AuthMiddleware(next))
|
|
89
95
|
# One retry quota for all of this client's operations.
|
|
90
96
|
self._retry_quota = ThreadSafeRetryQuota()
|
|
97
|
+
# One send-rate limiter too, used when retry_mode is adaptive.
|
|
98
|
+
self._rate_limiter = ThreadSafeRateLimiter()
|
|
91
99
|
if credentials is not None and credentials_provider is not None:
|
|
92
100
|
warnings.warn(
|
|
93
101
|
"Both credentials and credentials_provider given; provider takes precedence"
|
|
@@ -105,6 +113,7 @@ class SageMakerRuntimeHTTP2Client:
|
|
|
105
113
|
{
|
|
106
114
|
"operation_interceptors": operation_interceptors or [],
|
|
107
115
|
"retry_max_attempts": retry_max_attempts,
|
|
116
|
+
"retry_mode": retry_mode,
|
|
108
117
|
"use_dual_stack": use_dual_stack,
|
|
109
118
|
"use_fips": use_fips,
|
|
110
119
|
"endpoint": endpoint,
|
|
@@ -129,12 +138,15 @@ class SageMakerRuntimeHTTP2Client:
|
|
|
129
138
|
if operation_logging is not None:
|
|
130
139
|
interceptors_.append(log_operation(operation_logging))
|
|
131
140
|
interceptors_.append(retry())
|
|
141
|
+
interceptors_.append(rate_limit())
|
|
132
142
|
options_: OperationOptions = OperationOptions(
|
|
133
143
|
client=self._client,
|
|
134
144
|
retry_quota=self._retry_quota,
|
|
145
|
+
rate_limiter=self._rate_limiter,
|
|
135
146
|
retry_max_attempts=overrides.get(
|
|
136
147
|
"retry_max_attempts", self._config.get("retry_max_attempts")
|
|
137
148
|
),
|
|
149
|
+
retry_mode=overrides.get("retry_mode", self._config.get("retry_mode")),
|
|
138
150
|
use_dual_stack=overrides.get(
|
|
139
151
|
"use_dual_stack", self._config.get("use_dual_stack")
|
|
140
152
|
),
|
|
@@ -190,6 +202,7 @@ class SageMakerRuntimeHTTP2Client:
|
|
|
190
202
|
req.options, req.input
|
|
191
203
|
)
|
|
192
204
|
)
|
|
205
|
+
watch_event_stream(output, "body", req.options)
|
|
193
206
|
return OperationResponse(output=output, response=http_response)
|
|
194
207
|
|
|
195
208
|
def _redact_input(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: capo-sagemaker-runtime-http2
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.14.0
|
|
4
4
|
Summary: Typed, async Python SDK for AWS Sagemaker Runtime Http2.
|
|
5
5
|
Project-URL: Homepage, https://capo-sdk.dev/
|
|
6
6
|
Project-URL: Documentation, https://capo-sdk.dev/services/sagemaker-runtime-http2/
|
|
@@ -108,6 +108,8 @@ The SDK retries failed operations automatically. Retry behaviour follows the Smi
|
|
|
108
108
|
|
|
109
109
|
The number of attempts defaults to 3 and can be changed at the client level via `retry_max_attempts`, or per call via `config_overrides`.
|
|
110
110
|
|
|
111
|
+
`retry_mode="adaptive"` (or `AWS_RETRY_MODE=adaptive`, or `retry_mode = adaptive` in the AWS profile) also slows the whole client down once the service starts throttling it, then speeds back up as requests succeed. The default is `standard`.
|
|
112
|
+
|
|
111
113
|
```python
|
|
112
114
|
from capo_sagemaker_runtime_http2 import AsyncSageMakerRuntimeHTTP2Client
|
|
113
115
|
|
{capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/tests/test_sigv4.py
RENAMED
|
@@ -36,6 +36,7 @@ from capo_sagemaker_runtime_http2._auth._sigv4 import (
|
|
|
36
36
|
_derive_signing_key,
|
|
37
37
|
_trim_header_value,
|
|
38
38
|
_uri_encode,
|
|
39
|
+
presign_sigv4,
|
|
39
40
|
sign_sigv4,
|
|
40
41
|
)
|
|
41
42
|
from capo_sagemaker_runtime_http2._protocol.eventstream import Message, MessageDecoder, encode_headers
|
|
@@ -356,6 +357,38 @@ def test_s3_get_object_example():
|
|
|
356
357
|
# ---------------------------------------------------------------------------
|
|
357
358
|
|
|
358
359
|
|
|
360
|
+
def test_presign_signs_the_given_payload_hash():
|
|
361
|
+
"""An RDS IAM auth token signs the SHA-256 of an empty body, not
|
|
362
|
+
UNSIGNED-PAYLOAD. The vector is botocore's ``generate_db_auth_token`` for
|
|
363
|
+
the same host, user, region, credentials and clock."""
|
|
364
|
+
req = Request(
|
|
365
|
+
URL(
|
|
366
|
+
"https://db.abc.us-east-1.rds.amazonaws.com:5432/"
|
|
367
|
+
"?Action=connect&DBUser=app+user%40x"
|
|
368
|
+
),
|
|
369
|
+
"GET",
|
|
370
|
+
)
|
|
371
|
+
ctx: SigV4AuthContext = {
|
|
372
|
+
**_TEST_SUITE_CTX,
|
|
373
|
+
"signing_region": "eu-west-1",
|
|
374
|
+
"signing_name": "rds-db",
|
|
375
|
+
}
|
|
376
|
+
presigned = presign_sigv4(
|
|
377
|
+
req,
|
|
378
|
+
ctx,
|
|
379
|
+
expires_in=900,
|
|
380
|
+
now=dt.datetime(2026, 11, 21, 12, 0, tzinfo=dt.timezone.utc),
|
|
381
|
+
payload_hash=hashlib.sha256(b"").hexdigest(),
|
|
382
|
+
)
|
|
383
|
+
assert str(presigned.url) == (
|
|
384
|
+
"https://db.abc.us-east-1.rds.amazonaws.com:5432/?Action=connect"
|
|
385
|
+
"&DBUser=app%20user%40x&X-Amz-Algorithm=AWS4-HMAC-SHA256"
|
|
386
|
+
"&X-Amz-Credential=AKIDEXAMPLE%2F20261121%2Feu-west-1%2Frds-db%2Faws4_request"
|
|
387
|
+
"&X-Amz-Date=20261121T120000Z&X-Amz-Expires=900&X-Amz-SignedHeaders=host"
|
|
388
|
+
"&X-Amz-Signature=c949b1e47c86c7b6d7d8d2390316cd9c4c2f801a203e802b1bc961ebabefd08e"
|
|
389
|
+
)
|
|
390
|
+
|
|
391
|
+
|
|
359
392
|
def test_session_token_added_and_signed():
|
|
360
393
|
ctx: SigV4AuthContext = {**_TEST_SUITE_CTX, "session_token": "TOKEN123"}
|
|
361
394
|
req = _make_request(
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{capo_sagemaker_runtime_http2-0.12.0 → capo_sagemaker_runtime_http2-0.14.0}/tests/test_body.py
RENAMED
|
File without changes
|