@@ -88,7 +88,7 @@ def raise_on_error(r: Optional[Response], verb="???", **kwargs):
8888class ResilientSession (Session ):
8989 """This class is supposed to retry requests that do return temporary errors.
9090
91- At this moment it supports: 502, 503, 504
91+ At this moment it supports: 429
9292 """
9393
9494 def __init__ (self , timeout = None ):
@@ -113,11 +113,14 @@ def __recoverable(
113113 f"Got ConnectionError [{ response } ] errno:{ response .errno } on { request } { url } \n { vars (response )} \n { response .__dict__ } "
114114 )
115115 if isinstance (response , Response ):
116- if response .status_code in [502 , 503 , 504 , 401 ]:
117- # 401 UNAUTHORIZED still randomly returned by Atlassian Cloud as of 2017-01-16
116+ if response .status_code in [429 ]:
117+ rate_limit_remaining = response . headers [ "X-RateLimit-Remaining" ]
118118 msg = f"{ response .status_code } { response .reason } "
119- # 2019-07-25: Disabled recovery for codes above^
120- return False
119+ logging .warning (
120+ f"""Request rate limited by Jira: number of tokens remaining { rate_limit_remaining } . Consider adding exemption for the user as explained in
121+ https://confluence.atlassian.com/adminjiraserver/improving-instance-stability-with-rate-limiting-983794911.html
122+ """
123+ )
121124 elif not (
122125 response .status_code == 200
123126 and len (response .content ) == 0
@@ -128,7 +131,7 @@ def __recoverable(
128131 else :
129132 msg = "Atlassian's bug https://jira.atlassian.com/browse/JRA-41559"
130133
131- # Exponential backoff with full jitter .
134+ # Exponential backoff with full jijitter .
132135 delay = min (self .max_retry_delay , 10 * 2 ** counter ) * random .random ()
133136 logging .warning (
134137 "Got recoverable error from %s %s, will retry [%s/%s] in %ss. Err: %s"
@@ -137,6 +140,7 @@ def __recoverable(
137140 if isinstance (response , Response ):
138141 logging .debug ("response.headers: %s" , response .headers )
139142 logging .debug ("response.body: %s" , response .content )
143+ logging .debug (msg )
140144 time .sleep (delay )
141145 return True
142146
0 commit comments