Skip to content

Commit bffa854

Browse files
committed
fixup! Fix: Frequent timeouts from TDR (#7452)
1 parent cee4d2f commit bffa854

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/azul/chalice.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,21 +265,26 @@ def _security_headers_middleware(self, event, get_response):
265265

266266
def _retry_after(self, event, get_response):
267267
"""
268-
Add a retry-after header to the response based on the response type.
268+
Add a retry-after header to the response based on its type.
269269
"""
270270
response = get_response(event)
271271
status = response.status_code
272-
if status in [307, 503]:
272+
if status == 503:
273273
response.headers.setdefault('Retry-After', '30')
274-
if status == 307:
275-
# When a client's request fails due to an internal request to TDR
276-
# timing out, we want the client to retry their request, so we
277-
# return a 307 response along with a location header containing the
278-
# URL the client had just requested.
279-
location = furl(self.base_url,
280-
path=event.context['path'],
281-
args=event.query_params)
282-
response.headers.setdefault('Location', location.url)
274+
elif status == 307:
275+
response.headers.setdefault('Retry-After', '5')
276+
# When a client's request fails due to an internal request to Terra
277+
# timing out, instead of returning a 503 which causes the Data
278+
# Browser to show an error and also raises CloudWatch alarm, we
279+
# return a 307 to indicate the client should retry their request.
280+
if 'Location' not in response.headers:
281+
if event.query_params is None:
282+
args = None
283+
else:
284+
assert isinstance(event.query_params, MultiDict)
285+
args = {k: event.query_params.getlist(k) for k in event.query_params}
286+
url = furl(self.base_url, path=event.context['path'], args=args)
287+
response.headers['Location'] = str(url)
283288
return response
284289

285290
def _http_cache_for(self, seconds: int):

0 commit comments

Comments
 (0)