refactor: set Retry-After header directly
This commit is contained in:
parent
2f30dac5d8
commit
76eb3297cd
4 changed files with 4 additions and 19 deletions
|
|
@ -394,12 +394,6 @@ def handle_exception(e):
|
||||||
elif http_status_code == 429:
|
elif http_status_code == 429:
|
||||||
response = frappe.rate_limiter.respond()
|
response = frappe.rate_limiter.respond()
|
||||||
|
|
||||||
elif http_status_code == 503:
|
|
||||||
retry_after = getattr(e, "retry_after", 10)
|
|
||||||
response = frappe.utils.response.report_error(503)
|
|
||||||
if response:
|
|
||||||
response.headers["Retry-After"] = str(retry_after)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
response = ErrorPage(
|
response = ErrorPage(
|
||||||
http_status_code=http_status_code, title=_("Server Error"), message=_("Uncaught Exception")
|
http_status_code=http_status_code, title=_("Server Error"), message=_("Uncaught Exception")
|
||||||
|
|
|
||||||
|
|
@ -72,9 +72,9 @@ def concurrent_limit(limit: int | None = None, wait_timeout: int | None = None):
|
||||||
if not acquired:
|
if not acquired:
|
||||||
from frappe.exceptions import ServiceUnavailableError
|
from frappe.exceptions import ServiceUnavailableError
|
||||||
|
|
||||||
retry_after = max(1, int(effective_wait))
|
|
||||||
exc = ServiceUnavailableError(frappe._("Server is busy. Please try again in a few seconds."))
|
exc = ServiceUnavailableError(frappe._("Server is busy. Please try again in a few seconds."))
|
||||||
exc.retry_after = retry_after
|
retry_after = max(1, int(effective_wait))
|
||||||
|
frappe.local.response_headers.set("Retry-After", str(retry_after))
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -87,14 +87,7 @@ class TooManyRequestsError(Exception):
|
||||||
|
|
||||||
|
|
||||||
class ServiceUnavailableError(Exception):
|
class ServiceUnavailableError(Exception):
|
||||||
"""Raised when a concurrency limit is exceeded for an endpoint.
|
|
||||||
|
|
||||||
Set :attr:`retry_after` (seconds) before raising so that the response
|
|
||||||
includes a ``Retry-After`` header.
|
|
||||||
"""
|
|
||||||
|
|
||||||
http_status_code = 503
|
http_status_code = 503
|
||||||
retry_after: int = 10
|
|
||||||
|
|
||||||
|
|
||||||
class ImproperDBConfigurationError(Exception):
|
class ImproperDBConfigurationError(Exception):
|
||||||
|
|
|
||||||
|
|
@ -91,9 +91,8 @@ class TestConcurrentLimit(IntegrationTestCase):
|
||||||
del frappe.local.request
|
del frappe.local.request
|
||||||
frappe.cache.delete(key)
|
frappe.cache.delete(key)
|
||||||
|
|
||||||
def test_service_unavailable_has_correct_http_status_and_retry_after(self):
|
def test_service_unavailable_has_correct_http_status(self):
|
||||||
"""The raised exception must carry http_status_code=503 and retry_after
|
"""The raised exception must carry http_status_code=503."""
|
||||||
equal to the configured wait_timeout."""
|
|
||||||
TIMEOUT = 1
|
TIMEOUT = 1
|
||||||
|
|
||||||
@concurrent_limit(limit=1, wait_timeout=TIMEOUT)
|
@concurrent_limit(limit=1, wait_timeout=TIMEOUT)
|
||||||
|
|
@ -110,7 +109,6 @@ class TestConcurrentLimit(IntegrationTestCase):
|
||||||
fn()
|
fn()
|
||||||
exc = ctx.exception
|
exc = ctx.exception
|
||||||
self.assertEqual(exc.http_status_code, 503)
|
self.assertEqual(exc.http_status_code, 503)
|
||||||
self.assertEqual(exc.retry_after, TIMEOUT)
|
|
||||||
finally:
|
finally:
|
||||||
del frappe.local.request
|
del frappe.local.request
|
||||||
frappe.cache.delete(key)
|
frappe.cache.delete(key)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue