Skip to content

Make retry backoff and status codes customizable #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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