Skip to content

Commit f52ff7e

Browse files
committed
Slight logic change, more logging
1 parent d55a955 commit f52ff7e

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/ghga_service_commons/transports/config.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class RatelimitingTransportConfig(BaseSettings):
3636
"""TODO"""
3737

3838
jitter: NonNegativeFloat = Field(
39-
default=0.05, description="Max amoun of jitter to add to each request"
39+
default=0.001, description="Max amount of jitter to add to each request"
4040
)
41-
reset_after: int | None = Field(
42-
default=None,
41+
reset_after: int = Field(
42+
default=1,
4343
description="Amount of requests after which the stored delay from a 429 response is ignored again. If set to `None`, it's never forgotten.",
4444
)
4545

@@ -67,7 +67,5 @@ class CompositeConfig(RatelimitingTransportConfig, RetryTransportConfig):
6767
"""TOOD"""
6868

6969

70-
class CompositeCacheConfig(
71-
CacheTransportConfig, RatelimitingTransportConfig, RetryTransportConfig
72-
):
70+
class CompositeCacheConfig(CompositeConfig, CacheTransportConfig):
7371
"""TOOD"""

src/ghga_service_commons/transports/factory.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from hishel import AsyncCacheTransport, AsyncInMemoryStorage
1919
from httpx import AsyncHTTPTransport
2020

21+
from .config import CompositeCacheConfig, CompositeConfig
2122
from .ratelimiting import AsyncRatelimitingTransport
2223
from .retry import AsyncRetryTransport
2324

@@ -26,7 +27,7 @@ class CompositeTransportFactory:
2627
"""TODO"""
2728

2829
@classmethod
29-
def _create_common_transport_layers(cls, config, transport):
30+
def _create_common_transport_layers(cls, config: CompositeConfig, transport):
3031
"""TODO"""
3132
retry_transport = AsyncRetryTransport(config=config, transport=transport)
3233
ratelimiting_transport = AsyncRatelimitingTransport(
@@ -35,14 +36,16 @@ def _create_common_transport_layers(cls, config, transport):
3536
return ratelimiting_transport
3637

3738
@classmethod
38-
def create_ratelimiting_retry_transport(cls, config) -> AsyncRatelimitingTransport:
39+
def create_ratelimiting_retry_transport(
40+
cls, config: CompositeConfig
41+
) -> AsyncRatelimitingTransport:
3942
"""TODO"""
4043
base_transport = AsyncHTTPTransport()
4144
return cls._create_common_transport_layers(config, base_transport)
4245

4346
@classmethod
4447
def create_ratelimiting_retry_transport_with_cache(
45-
cls, config
48+
cls, config: CompositeCacheConfig
4649
) -> AsyncRatelimitingTransport:
4750
"""TODO"""
4851
base_transport = AsyncHTTPTransport()

src/ghga_service_commons/transports/ratelimiting.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def __init__(
3636
self, config: RatelimitingTransportConfig, transport: httpx.AsyncBaseTransport
3737
) -> None:
3838
self._jitter = config.jitter
39-
self._num_requests = 0
40-
self._reset_after: int | None = config.reset_after
4139
self._transport = transport
42-
self._last_call_time = datetime.now(timezone.utc)
40+
self._num_requests = 0
41+
self._reset_after: int = config.reset_after
42+
self._last_request_time = datetime.now(timezone.utc)
4343
self._wait_time: float = 0
4444

4545
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
@@ -52,7 +52,7 @@ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
5252
:rtype: httpx.Response
5353
"""
5454
# Caculate seconds since the last request has been fired and corresponding wait time
55-
time_elapsed = (self._last_call_time - datetime.now(timezone.utc)).seconds
55+
time_elapsed = (self._last_request_time - datetime.now(timezone.utc)).seconds
5656
remaining_wait = max(0, self._wait_time - time_elapsed)
5757

5858
# Add jitter to both cases and sleep
@@ -63,16 +63,17 @@ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
6363
random.uniform(remaining_wait, remaining_wait + self._jitter) # noqa: S311
6464
)
6565

66-
# Update timestamp and delegate call
67-
self._last_call_time = datetime.now(timezone.utc)
66+
# Delegate call and update timestamp
6867
response = await self._transport.handle_async_request(request=request)
68+
self._last_request_time = datetime.now(timezone.utc)
6969

7070
# Update state
7171
self._num_requests += 1
7272
if response.status_code == 429:
7373
retry_after = response.headers.get("Retry-After")
7474
if retry_after:
7575
self._wait_time = float(retry_after)
76+
log.info("Received retry after response: %.3f s", self._wait_time)
7677
else:
7778
log.warning(
7879
"Retry-After header not present in 429 response, using fallback instead."

0 commit comments

Comments
 (0)