Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit d9b8602

Browse files
fix: Exponential backoff for retry requests
1 parent b005af1 commit d9b8602

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

postgrest/_async/request_builder.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import time
4+
35
from json import JSONDecodeError
46
from typing import Any, Generic, Optional, TypeVar, Union
57

@@ -48,6 +50,10 @@ def __init__(
4850
self.attempt = 1
4951
# For GET and HEAD requests, the body should be None.
5052
self.json = None if http_method in {"GET", "HEAD"} else json
53+
# Exponential backoff for retries.
54+
self.exponential_backoff = lambda: time.sleep(
55+
min(1000 * 2**self.attempt, 30000) / 1000
56+
)
5157

5258
async def execute(self) -> APIResponse[_ReturnT]:
5359
"""Execute the query.
@@ -88,6 +94,7 @@ async def execute(self) -> APIResponse[_ReturnT]:
8894
# This is because the DML operation may have already executed successfully on the database,
8995
# but the client lost the connection before receiving confirmation.
9096
if self.attempt < self.max_retries and self.http_method == "GET":
97+
self.exponential_backoff()
9198
self.attempt += 1
9299
await self.execute()
93100
else:

0 commit comments

Comments
 (0)