Skip to content

Commit f35970f

Browse files
authored
Make retry backoff and status codes customizable (#421)
1 parent 8b52134 commit f35970f

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

gql/transport/requests.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import io
22
import json
33
import logging
4-
from typing import Any, Dict, Optional, Tuple, Type, Union
4+
from typing import Any, Collection, Dict, Optional, Tuple, Type, Union
55

66
import requests
77
from graphql import DocumentNode, ExecutionResult, print_ast
@@ -31,6 +31,7 @@ class RequestsHTTPTransport(Transport):
3131
"""
3232

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

3536
def __init__(
3637
self,
@@ -43,6 +44,8 @@ def __init__(
4344
verify: Union[bool, str] = True,
4445
retries: int = 0,
4546
method: str = "POST",
47+
retry_backoff_factor: float = 0.1,
48+
retry_status_forcelist: Collection[int] = _default_retry_codes,
4649
**kwargs: Any,
4750
):
4851
"""Initialize the transport with the given request parameters.
@@ -62,6 +65,13 @@ def __init__(
6265
to a CA bundle to use. (Default: True).
6366
:param retries: Pre-setup of the requests' Session for performing retries
6467
:param method: HTTP method used for requests. (Default: POST).
68+
:param retry_backoff_factor: A backoff factor to apply between attempts after
69+
the second try. urllib3 will sleep for:
70+
{backoff factor} * (2 ** ({number of previous retries}))
71+
:param retry_status_forcelist: A set of integer HTTP status codes that we
72+
should force a retry on. A retry is initiated if the request method is
73+
in allowed_methods and the response status code is in status_forcelist.
74+
(Default: [429, 500, 502, 503, 504])
6575
:param kwargs: Optional arguments that ``request`` takes.
6676
These can be seen at the `requests`_ source code or the official `docs`_
6777
@@ -77,6 +87,8 @@ def __init__(
7787
self.verify = verify
7888
self.retries = retries
7989
self.method = method
90+
self.retry_backoff_factor = retry_backoff_factor
91+
self.retry_status_forcelist = retry_status_forcelist
8092
self.kwargs = kwargs
8193

8294
self.session = None
@@ -95,8 +107,8 @@ def connect(self):
95107
adapter = HTTPAdapter(
96108
max_retries=Retry(
97109
total=self.retries,
98-
backoff_factor=0.1,
99-
status_forcelist=[500, 502, 503, 504],
110+
backoff_factor=self.retry_backoff_factor,
111+
status_forcelist=self.retry_status_forcelist,
100112
allowed_methods=None,
101113
)
102114
)

0 commit comments

Comments
 (0)