Skip to content

Commit 67fc321

Browse files
committed
Add missing files
1 parent 87db6bd commit 67fc321

File tree

4 files changed

+95
-9
lines changed

4 files changed

+95
-9
lines changed

pinecone/openapi_support/rest_aiohttp.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@ def __init__(self, configuration: Configuration) -> None:
3030
else:
3131
self._session = aiohttp.ClientSession(connector=conn)
3232

33-
jitter_retry = JitterRetry(
34-
attempts=5,
35-
start_timeout=0.1,
36-
max_timeout=3.0,
37-
statuses={500, 502, 503, 504},
38-
methods=None, # retry on all methods
39-
exceptions={aiohttp.ClientError, aiohttp.ServerDisconnectedError},
40-
)
41-
self._retry_client = RetryClient(client_session=self._session, retry_options=jitter_retry)
33+
if configuration.retries is not None:
34+
retry_options = configuration.retries
35+
else:
36+
retry_options = JitterRetry(
37+
attempts=5,
38+
start_timeout=0.1,
39+
max_timeout=3.0,
40+
statuses={500, 502, 503, 504},
41+
methods=None, # retry on all methods
42+
exceptions={aiohttp.ClientError, aiohttp.ServerDisconnectedError},
43+
)
44+
self._retry_client = RetryClient(client_session=self._session, retry_options=retry_options)
4245

4346
async def close(self):
4447
await self._retry_client.close()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import random
2+
from typing import Optional
3+
from aiohttp_retry import RetryOptionsBase, EvaluateResponseCallbackType, ClientResponse
4+
import logging
5+
6+
logger = logging.getLogger(__name__)
7+
8+
9+
class JitterRetry(RetryOptionsBase):
10+
"""https://github.com/inyutin/aiohttp_retry/issues/44."""
11+
12+
def __init__(
13+
self,
14+
attempts: Optional[int] = 3, # How many times we should retry
15+
start_timeout: Optional[float] = 0.1, # Base timeout time, then it exponentially grow
16+
max_timeout: Optional[float] = 5.0, # Max possible timeout between tries
17+
statuses: Optional[set[int]] = None, # On which statuses we should retry
18+
exceptions: Optional[set[type[Exception]]] = None, # On which exceptions we should retry
19+
methods: Optional[set[str]] = None, # On which HTTP methods we should retry
20+
random_interval_size: Optional[float] = 2.0, # size of interval for random component
21+
retry_all_server_errors: bool = True,
22+
evaluate_response_callback: Optional[EvaluateResponseCallbackType] = None,
23+
) -> None:
24+
super().__init__(
25+
attempts=attempts,
26+
statuses=statuses,
27+
exceptions=exceptions,
28+
methods=methods,
29+
retry_all_server_errors=retry_all_server_errors,
30+
evaluate_response_callback=evaluate_response_callback,
31+
)
32+
33+
self._start_timeout: float = start_timeout
34+
self._max_timeout: float = max_timeout
35+
self._random_interval_size = random_interval_size
36+
37+
def get_timeout(
38+
self,
39+
attempt: int,
40+
response: Optional[ClientResponse] = None, # noqa: ARG002
41+
) -> float:
42+
logger.debug(f"JitterRetry get_timeout: attempt={attempt}, response={response}")
43+
"""Return timeout with exponential backoff."""
44+
jitter = random.uniform(0, 0.1)
45+
timeout = self._start_timeout * (2 ** (attempt - 1))
46+
return min(timeout + jitter, self._max_timeout)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import random
2+
from urllib3.util.retry import Retry
3+
import logging
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
class JitterRetry(Retry):
9+
"""
10+
Retry with exponential back‑off with jitter.
11+
12+
The Retry class is being extended as built-in support for jitter was added only in urllib3 2.0.0.
13+
Jitter logic is following the official implementation with a constant jitter factor: https://github.com/urllib3/urllib3/blob/main/src/urllib3/util/retry.py
14+
"""
15+
16+
def get_backoff_time(self) -> float:
17+
backoff_value = super().get_backoff_time()
18+
jitter = random.random() * 0.25
19+
backoff_value += jitter
20+
logger.debug(f"Calculating retry backoff: {backoff_value} (jitter: {jitter})")
21+
return backoff_value

scripts/test-async-retry.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import dotenv
2+
import asyncio
3+
import logging
4+
from pinecone import PineconeAsyncio
5+
6+
dotenv.load_dotenv()
7+
8+
logging.basicConfig(level=logging.DEBUG)
9+
10+
11+
async def main():
12+
async with PineconeAsyncio(host="http://localhost:8000") as pc:
13+
await pc.db.index.list()
14+
15+
16+
asyncio.run(main())

0 commit comments

Comments
 (0)