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

Fix retrying for ignored error codes #249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
53 changes: 23 additions & 30 deletions pybit/_http_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class _V5HTTPManager:
init=False,
)
ignore_codes: dict = field(
default_factory=dict,
init=False,
default_factory=set
)
max_retries: bool = field(default=3)
retry_delay: bool = field(default=3)
Expand Down Expand Up @@ -331,27 +330,25 @@ def _submit_request(self, method=None, path=None, query=None, auth=False):
resp_headers=s.headers,
)

ret_code = "retCode"
ret_msg = "retMsg"
ret_code = s_json["retCode"]
ret_msg = s_json["retMsg"]

# If Bybit returns an error, raise.
if s_json[ret_code]:
# Handle error codes
if ret_code and ret_code not in self.ignore_codes:
# Generate error message.
error_msg = f"{s_json[ret_msg]} (ErrCode: {s_json[ret_code]})"

# Set default retry delay.
delay_time = self.retry_delay
error_msg = f"{ret_msg} (ErrCode: {ret_code})"

# Retry non-fatal whitelisted error requests.
if s_json[ret_code] in self.retry_codes:
if ret_code in self.retry_codes:
delay_time = self.retry_delay
# 10002, recv_window error; add 2.5 seconds and retry.
if s_json[ret_code] == 10002:
if ret_code == 10002:
error_msg += ". Added 2.5 seconds to recv_window"
recv_window += 2500

# 10006, rate limit error; wait until
# X-Bapi-Limit-Reset-Timestamp and retry.
elif s_json[ret_code] == 10006:
elif ret_code == 10006:
self.logger.error(
f"{error_msg}. Hit the API rate limit. "
f"Sleeping, then trying again. Request: {path}"
Expand All @@ -371,27 +368,23 @@ def _submit_request(self, method=None, path=None, query=None, auth=False):
self.logger.error(f"{error_msg}. {retries_remaining}")
time.sleep(delay_time)
continue

elif s_json[ret_code] in self.ignore_codes:
pass

else:
raise InvalidRequestError(
request=f"{method} {path}: {req_params}",
message=s_json[ret_msg],
status_code=s_json[ret_code],
message=ret_msg,
status_code=ret_code,
time=dt.utcnow().strftime("%H:%M:%S"),
resp_headers=s.headers,
)
else:
if self.log_requests:
self.logger.debug(
f"Response headers: {s.headers}"
)

if self.log_requests:
self.logger.debug(
f"Response headers: {s.headers}"
)

if self.return_response_headers:
return s_json, s.elapsed, s.headers,
elif self.record_request_time:
return s_json, s.elapsed
else:
return s_json
if self.return_response_headers:
return s_json, s.elapsed, s.headers,
elif self.record_request_time:
return s_json, s.elapsed
else:
return s_json