@@ -178,6 +178,11 @@ class Retry:
178178 Sequence of headers to remove from the request when a response
179179 indicating a redirect is returned before firing off the redirected
180180 request.
181+
182+ :param int retry_after_max: Number of seconds to allow as the maximum for
183+ Retry-After headers. Defaults to :attr:`Retry.DEFAULT_RETRY_AFTER_MAX`.
184+ Any Retry-After headers larger than this value will be limited to this
185+ value.
181186 """
182187
183188 #: Default methods to be used for ``allowed_methods``
@@ -196,6 +201,10 @@ class Retry:
196201 #: Default maximum backoff time.
197202 DEFAULT_BACKOFF_MAX = 120
198203
204+ # This is undocumented in the RFC. Setting to 6 hours matches other popular libraries.
205+ #: Default maximum allowed value for Retry-After headers in seconds
206+ DEFAULT_RETRY_AFTER_MAX : typing .Final [int ] = 21600
207+
199208 # Backward compatibility; assigned outside of the class.
200209 DEFAULT : typing .ClassVar [Retry ]
201210
@@ -219,6 +228,7 @@ def __init__(
219228 str
220229 ] = DEFAULT_REMOVE_HEADERS_ON_REDIRECT ,
221230 backoff_jitter : float = 0.0 ,
231+ retry_after_max : int = DEFAULT_RETRY_AFTER_MAX ,
222232 ) -> None :
223233 self .total = total
224234 self .connect = connect
@@ -235,6 +245,7 @@ def __init__(
235245 self .allowed_methods = allowed_methods
236246 self .backoff_factor = backoff_factor
237247 self .backoff_max = backoff_max
248+ self .retry_after_max = retry_after_max
238249 self .raise_on_redirect = raise_on_redirect
239250 self .raise_on_status = raise_on_status
240251 self .history = history or ()
@@ -256,6 +267,7 @@ def new(self, **kw: typing.Any) -> Self:
256267 status_forcelist = self .status_forcelist ,
257268 backoff_factor = self .backoff_factor ,
258269 backoff_max = self .backoff_max ,
270+ retry_after_max = self .retry_after_max ,
259271 raise_on_redirect = self .raise_on_redirect ,
260272 raise_on_status = self .raise_on_status ,
261273 history = self .history ,
@@ -320,6 +332,10 @@ def parse_retry_after(self, retry_after: str) -> float:
320332
321333 seconds = max (seconds , 0 )
322334
335+ # Check the seconds do not exceed the specified maximum
336+ if seconds > self .retry_after_max :
337+ seconds = self .retry_after_max
338+
323339 return seconds
324340
325341 def get_retry_after (self , response : BaseHTTPResponse ) -> float | None :
0 commit comments