Skip to content

Commit

Permalink
revert(backoff)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovuruska committed May 4, 2024
1 parent 198f49a commit 60401bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
27 changes: 20 additions & 7 deletions deepinfra/clients/deepinfra.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import time
import requests

from deepinfra.exceptions import MaxRetriesExceededError
from deepinfra.constants import (
MAX_RETRIES,
USER_AGENT,
Expand All @@ -19,6 +19,10 @@ def __init__(self, url, auth_token):
self.url = url
self.auth_token = auth_token

def backoff_delay(self, attempt):
delay = self.initial_backoff if attempt == 1 else self.subsequent_backoff
time.sleep(delay)

def post(self, data, config=None):
"""
Performs a POST request.
Expand All @@ -36,9 +40,18 @@ def post(self, data, config=None):
"User-Agent": USER_AGENT,
"Authorization": f"Bearer {self.auth_token}",
}
try:
response = requests.post(self.url, data=data, headers=headers)
response.raise_for_status()
return response
except requests.RequestException as error:
raise error
for attempt in range(self.max_retries + 1):
try:
response = requests.post(self.url, data=data, headers=headers)
response.raise_for_status()
return response
except requests.RequestException as error:
if attempt < self.max_retries:
print(
f"Request failed, retrying... Attempt {attempt + 1}/{self.max_retries}"
)
self.backoff_delay(attempt + 1)
else:
raise error

raise MaxRetriesExceededError()
2 changes: 1 addition & 1 deletion deepinfra/constants/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This module contains the constants used by the DeepInfra API client.
"""

MAX_RETRIES = 0
MAX_RETRIES = 5
INITIAL_BACKOFF = 5000
SUBSEQUENT_BACKOFF = 2000
USER_AGENT = "DeepInfra Python API Client"
Expand Down
Empty file.
12 changes: 12 additions & 0 deletions deepinfra/exceptions/max_retries_exceeded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
This error is raised when the maximum number of retries is exceeded by DeepInfraClient.
"""


class MaxRetriesExceededError(Exception):
"""
This error is raised when the maximum number of retries is exceeded by DeepInfraClient.
"""

def __init__(self, message="Maximum retries exceeded"):
super().__init__(message)

0 comments on commit 60401bf

Please sign in to comment.