1
1
import io
2
2
import json
3
3
import logging
4
- from typing import Any , Dict , Optional , Tuple , Type , Union
4
+ from typing import Any , Collection , Dict , Optional , Tuple , Type , Union
5
5
6
6
import requests
7
7
from graphql import DocumentNode , ExecutionResult , print_ast
@@ -31,6 +31,7 @@ class RequestsHTTPTransport(Transport):
31
31
"""
32
32
33
33
file_classes : Tuple [Type [Any ], ...] = (io .IOBase ,)
34
+ _default_retry_codes = (429 , 500 , 502 , 503 , 504 )
34
35
35
36
def __init__ (
36
37
self ,
@@ -42,6 +43,8 @@ def __init__(
42
43
timeout : Optional [int ] = None ,
43
44
verify : Union [bool , str ] = True ,
44
45
retries : int = 0 ,
46
+ retry_backoff_factor : float = 0.1 ,
47
+ retry_status_forcelist : Collection [int ] = _default_retry_codes ,
45
48
method : str = "POST" ,
46
49
** kwargs : Any ,
47
50
):
@@ -61,6 +64,13 @@ def __init__(
61
64
the server's TLS certificate, or a string, in which case it must be a path
62
65
to a CA bundle to use. (Default: True).
63
66
: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])
64
74
:param method: HTTP method used for requests. (Default: POST).
65
75
:param kwargs: Optional arguments that ``request`` takes.
66
76
These can be seen at the `requests`_ source code or the official `docs`_
@@ -76,6 +86,8 @@ def __init__(
76
86
self .default_timeout = timeout
77
87
self .verify = verify
78
88
self .retries = retries
89
+ self .retry_backoff_factor = retry_backoff_factor
90
+ self .retry_status_forcelist = retry_status_forcelist
79
91
self .method = method
80
92
self .kwargs = kwargs
81
93
@@ -95,8 +107,8 @@ def connect(self):
95
107
adapter = HTTPAdapter (
96
108
max_retries = Retry (
97
109
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 ,
100
112
allowed_methods = None ,
101
113
)
102
114
)
0 commit comments