Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amount of retries configurable #10

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions shipbob/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import time
import math
from datetime import datetime, timedelta
from typing import Iterator, List

Expand All @@ -21,6 +22,12 @@ class ShipBobClient(Session):

ACCESS_TOKEN = os.getenv("SHIPBOB_ACCESS_TOKEN")

MAX_RETRIES_AFTER_RATE_LIMIT: int = os.getenv("SHIPBOB_MAX_RETRIES_AFTER_RATE_LIMIT")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having a limit of amount of times to retry, wouldn't it be better/easier to track the amount of time since the last successful request?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lullis it is being handled this is maximum number of retries to perform and I am mainting the separate counter RETRIES_AFTER_RATE_LIMIT to count number of time since last successful request


MAX_SLEEP_TIME_AFTER_RATE_LIMIT_IN_SECONDS: int = os.getenv(
"SHIPBOB_MAX_SLEEP_TIME_AFTER_RATE_LIMIT_IN_SECONDS"
)

MAX_PAGE_SIZE: int = 250
MAX_REQUESTS_PER_MINUTE: int = 150 # As per ShipBob docs, 150 reqs/min sliding window

Expand All @@ -35,6 +42,14 @@ def __init__(self):

self._request_bucket = []

self.RETRIES_AFTER_RATE_LIMIT: int = 0

if not self.MAX_RETRIES_AFTER_RATE_LIMIT:
self.MAX_RETRIES_AFTER_RATE_LIMIT = 1

if not self.MAX_SLEEP_TIME_AFTER_RATE_LIMIT_IN_SECONDS:
self.MAX_SLEEP_TIME_AFTER_RATE_LIMIT_IN_SECONDS = 60

def _clear_request_bucket(self):
now = datetime.now()
one_minute_old = now - timedelta(seconds=60)
Expand All @@ -60,10 +75,24 @@ def request(self, method, url, retry_on_rate_limit=True, *args, **kwargs):
except ValueError:
retry_after: int = 10
logger.warning(f"Waiting {retry_after} seconds to try again...")
time.sleep(retry_after)
return self.request(method, url, retry_on_rate_limit=False, *args, **kwargs)

if self.MAX_SLEEP_TIME_AFTER_RATE_LIMIT_IN_SECONDS < retry_after * math.pow(
2, self.RETRIES_AFTER_RATE_LIMIT - 1
):
logger.warning(
f"Threshold waiting time of \
{self.MAX_SLEEP_TIME_AFTER_RATE_LIMIT_IN_SECONDS} \
after rate limit exceeded."
)
raise e

time.sleep(retry_after * math.pow(2, self.RETRIES_AFTER_RATE_LIMIT))
self.RETRIES_AFTER_RATE_LIMIT = self.RETRIES_AFTER_RATE_LIMIT + 1
if self.MAX_RETRIES_AFTER_RATE_LIMIT >= self.RETRIES_AFTER_RATE_LIMIT:
return self.request(method, url, retry_on_rate_limit=True, *args, **kwargs)
else:
raise e
self.RETRIES_AFTER_RATE_LIMIT = 0
return response

def get_orders(self, **params) -> List[Order]:
Expand Down