Skip to content

Commit

Permalink
Make retry backoff and status codes customizable (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmeeedddsss authored Jul 13, 2023
1 parent 8b52134 commit f35970f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions gql/transport/requests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import io
import json
import logging
from typing import Any, Dict, Optional, Tuple, Type, Union
from typing import Any, Collection, Dict, Optional, Tuple, Type, Union

import requests
from graphql import DocumentNode, ExecutionResult, print_ast
Expand Down Expand Up @@ -31,6 +31,7 @@ class RequestsHTTPTransport(Transport):
"""

file_classes: Tuple[Type[Any], ...] = (io.IOBase,)
_default_retry_codes = (429, 500, 502, 503, 504)

def __init__(
self,
Expand All @@ -43,6 +44,8 @@ def __init__(
verify: Union[bool, str] = True,
retries: int = 0,
method: str = "POST",
retry_backoff_factor: float = 0.1,
retry_status_forcelist: Collection[int] = _default_retry_codes,
**kwargs: Any,
):
"""Initialize the transport with the given request parameters.
Expand All @@ -62,6 +65,13 @@ def __init__(
to a CA bundle to use. (Default: True).
:param retries: Pre-setup of the requests' Session for performing retries
:param method: HTTP method used for requests. (Default: POST).
:param retry_backoff_factor: A backoff factor to apply between attempts after
the second try. urllib3 will sleep for:
{backoff factor} * (2 ** ({number of previous retries}))
:param retry_status_forcelist: A set of integer HTTP status codes that we
should force a retry on. A retry is initiated if the request method is
in allowed_methods and the response status code is in status_forcelist.
(Default: [429, 500, 502, 503, 504])
:param kwargs: Optional arguments that ``request`` takes.
These can be seen at the `requests`_ source code or the official `docs`_
Expand All @@ -77,6 +87,8 @@ def __init__(
self.verify = verify
self.retries = retries
self.method = method
self.retry_backoff_factor = retry_backoff_factor
self.retry_status_forcelist = retry_status_forcelist
self.kwargs = kwargs

self.session = None
Expand All @@ -95,8 +107,8 @@ def connect(self):
adapter = HTTPAdapter(
max_retries=Retry(
total=self.retries,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504],
backoff_factor=self.retry_backoff_factor,
status_forcelist=self.retry_status_forcelist,
allowed_methods=None,
)
)
Expand Down

0 comments on commit f35970f

Please sign in to comment.