Skip to content

Commit d0a1483

Browse files
authored
Make retry backoff and status codes customizable
1 parent 8b52134 commit d0a1483

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,
@@ -42,6 +43,8 @@ def __init__(
4243
timeout: Optional[int] = None,
4344
verify: Union[bool, str] = True,
4445
retries: int = 0,
46+
retry_backoff_factor: float = 0.1,
47+
retry_status_forcelist: Collection[int] = _default_retry_codes,
4548
method: str = "POST",
4649
**kwargs: Any,
4750
):
@@ -61,6 +64,13 @@ def __init__(
6164
the server's TLS certificate, or a string, in which case it must be a path
6265
to a CA bundle to use. (Default: True).
6366
:param retries: Pre-setup of the requests' Session for performing retries
67+
:param retry_backoff_factor: A backoff factor to apply between attempts after
68+
the second try. urllib3 will sleep for:
69+
{backoff factor} * (2 ** ({number of previous retries}))
70+
:param retry_status_forcelist: A set of integer HTTP status codes that we
71+
should force a retry on. A retry is initiated if the request method is
72+
in allowed_methods and the response status code is in status_forcelist.
73+
(Default: [429, 500, 502, 503, 504])
6474
:param method: HTTP method used for requests. (Default: POST).
6575
:param kwargs: Optional arguments that ``request`` takes.
6676
These can be seen at the `requests`_ source code or the official `docs`_
@@ -76,6 +86,8 @@ def __init__(
7686
self.default_timeout = timeout
7787
self.verify = verify
7888
self.retries = retries
89+
self.retry_backoff_factor = retry_backoff_factor
90+
self.retry_status_forcelist = retry_status_forcelist
7991
self.method = method
8092
self.kwargs = kwargs
8193

@@ -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)