Skip to content

Commit

Permalink
application: use EventletTimeoutMiddleware if using eventlet
Browse files Browse the repository at this point in the history
also catch EventletTimeout exceptions, turning these into 504s,
which are the closest thing to an application timeout standard
codes seem to cover

60s timeout to start with as this matches our cloudfront/ALB
timeouts and so should present the same behaviour to external
users
  • Loading branch information
risicle committed Oct 3, 2024
1 parent f14015c commit d3cd6fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from notifications_utils.clients.signing.signing_client import Signing
from notifications_utils.clients.statsd.statsd_client import StatsdClient
from notifications_utils.clients.zendesk.zendesk_client import ZendeskClient
from notifications_utils.eventlet import EventletTimeout
from notifications_utils.local_vars import LazyLocalGetter
from sqlalchemy import event
from werkzeug.exceptions import HTTPException as WerkzeugHTTPException
Expand Down Expand Up @@ -420,6 +421,11 @@ def exception(error):
code = getattr(error, "code", 500)
return jsonify(result="error", message=msg), code

@app.errorhandler(EventletTimeout)
def eventlet_timeout(error):
app.logger.exception(error)
return jsonify(result="error", message="Timeout serving request"), 504

@app.errorhandler(WerkzeugHTTPException)
def werkzeug_exception(e):
return make_response(jsonify(result="error", message=e.description), e.code, e.get_headers())
Expand Down
5 changes: 5 additions & 0 deletions application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from app import create_app # noqa
from app.notify_api_flask_app import NotifyApiFlaskApp # noqa

from notifications_utils.eventlet import EventletTimeoutMiddleware, using_eventlet # noqa

application = NotifyApiFlaskApp("app")

create_app(application)

if using_eventlet:
application = EventletTimeoutMiddleware(application, timeout_seconds=60)

0 comments on commit d3cd6fa

Please sign in to comment.