Skip to content

Commit

Permalink
connection: Allow disabling retry on 400 HTTP status code
Browse files Browse the repository at this point in the history
We probably want the retry on by default to retry on running services,
but in some cases such as osc diff it only adds delays while handling
a quite expected errror state.
  • Loading branch information
dmach committed Apr 28, 2023
1 parent 49cfd02 commit b0629f6
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions osc/connection.py
Expand Up @@ -135,20 +135,20 @@ def http_request_wrap_file(func):
Turn file path into a file object and close it automatically
by using a context manager.
"""
def new_func(method, url, headers=None, data=None, file=None):
def new_func(method, url, headers=None, data=None, file=None, retry_on_400: bool = True):
if file:
with open(file, "rb") as f:
return func(method, url, headers, data, file=f)
return func(method, url, headers, data, f, retry_on_400)
else:
return func(method, url, headers, data, file)
return func(method, url, headers, data, file, retry_on_400)

new_func.__name__ = func.__name__
new_func.__doc__ = func.__doc__
return new_func


@http_request_wrap_file
def http_request(method: str, url: str, headers=None, data=None, file=None):
def http_request(method: str, url: str, headers=None, data=None, file=None, retry_on_400: bool = True):
"""
Send a HTTP request to a server.
Expand All @@ -168,6 +168,7 @@ def http_request(method: str, url: str, headers=None, data=None, file=None):
:param headers: Dictionary of custom headers to send.
:param data: Data to send in the request body (conflicts with `file`).
:param file: Path to a file to send as data in the request body (conflicts with `data`).
:param retry_on_400: Whether to retry on receiving HTTP status code 400.
"""

purl = urllib3.util.parse_url(url)
Expand Down Expand Up @@ -227,16 +228,22 @@ def http_request(method: str, url: str, headers=None, data=None, file=None):
else:
retries_kwargs = {"method_whitelist": None}


status_forcelist = (
500, # Internal Server Error
502, # Bad Gateway
503, # Service Unavailable
504, # Gateway Timeout
)
if retry_on_400:
status_forcelist = (
400, # Bad Request; retry on 400: service in progress
) + status_forcelist

pool_kwargs["retries"] = urllib3.Retry(
total=int(conf.config["http_retries"]),
backoff_factor=2,
status_forcelist=(
400, # Bad Request; retry on 400: service in progress
500, # Internal Server Error
502, # Bad Gateway
503, # Service Unavailable
504, # Gateway Timeout
),
status_forcelist=status_forcelist,
# don't raise because we want an actual response rather than a MaxRetryError with "too many <status_code> error responses" message
raise_on_status=False,
**retries_kwargs,
Expand Down

0 comments on commit b0629f6

Please sign in to comment.