Debian Codegraph @

src/globus_sdk/transport/retry_config.py

raw
1 from __future__ import annotations
2
3 import contextlib
4 import dataclasses
5 import random
6 import typing as t
7
8 from .retry import RetryCheckCollection, RetryContext
9
10
11 def _exponential_backoff(ctx: RetryContext) -> float:
12 # respect any explicit backoff set on the context
13 if ctx.backoff is not None:
14 return ctx.backoff
15 # exponential backoff with jitter
16 return t.cast(float, (0.25 + 0.5 * random.random()) * (2**ctx.attempt))
17
18
19 @dataclasses.dataclass
20 class RetryConfig:
21 """
22 Configuration for a client which is going to retry requests.
23
24 :param max_retries: The maximum number of retries allowed.
25 :param max_sleep: The maximum sleep time between retries (in seconds). If the
26 computed sleep time or the backoff requested by a retry check exceeds this
27 value, this amount of time will be used instead.
28 :param backoff: A function which determines how long to sleep between calls
29 based on the RetryContext. Defaults to exponential backoff with jitter based on
30 the context ``attempt`` number.
31 :param retry_after_status_codes: HTTP status codes for responses which may have
32 a Retry-After header.
33 :param transient_error_status_codes: HTTP status codes for error responses which
34 should generally be retried.
35 :param expired_authorization_status_codes: HTTP status codes indicating that
36 authorization info was missing or expired.
37 :param checks: The check callbacks which will run in order to evaluate
38 responses and exceptions, as a ``RetryCheckCollection``.
39 """
40
41 max_retries: int = 5
42 max_sleep: float | int = 10
43 backoff: t.Callable[[RetryContext], float] = _exponential_backoff
44 retry_after_status_codes: tuple[int, ...] = (429, 503)
45 transient_error_status_codes: tuple[int, ...] = (429, 500, 502, 503, 504)
46 expired_authorization_status_codes: tuple[int, ...] = (401,)
47
48 checks: RetryCheckCollection = dataclasses.field(
49 default_factory=RetryCheckCollection
50 )
51
52 @contextlib.contextmanager
53 def tune(
54 self,
55 *,
56 backoff: t.Callable[[RetryContext], float] | None = None,
57 max_sleep: float | int | None = None,
58 max_retries: int | None = None,
59 ) -> t.Iterator[None]:
60 """
61 Temporarily adjust some of the request retry settings.
62 This method works as a context manager, and will reset settings to their
63 original values after it exits.
64
65 :param backoff: A function which determines how long to sleep between
66 calls based on the RetryContext
67 :param max_sleep: The maximum sleep time between retries (in seconds). If the
68 computed sleep time or the backoff requested by a retry check exceeds this
69 value, this amount of time will be used instead
70 :param max_retries: The maximum number of retries allowed by this transport
71
72 **Example Usage**
73
74 This can be used with any client class to temporarily set values in the context
75 of one or more HTTP requests. For example, to disable retries:
76
77 >>> client = ... # any client class
78 >>> with client.retry_config.tune(max_retries=0):
79 >>> foo = client.get_foo()
80
81 See also: :meth:`RequestsTransport.tune`.
82 """
83 saved_settings = (
84 self.backoff,
85 self.max_sleep,
86 self.max_retries,
87 )
88 if backoff is not None:
89 self.backoff = backoff
90 if max_sleep is not None:
91 self.max_sleep = max_sleep
92 if max_retries is not None:
93 self.max_retries = max_retries
94 yield
95 (
96 self.backoff,
97 self.max_sleep,
98 self.max_retries,
99 ) = saved_settings