SSE server reliability improvements

This commit is contained in:
Ian Renton
2026-06-05 16:06:33 +01:00
parent af1974f36d
commit a2dff07c0e
12 changed files with 93 additions and 42 deletions

View File

@@ -98,6 +98,9 @@ class APIAlertsStreamHandler(tornado_eventsource.handler.EventSourceHandler):
self._heartbeat = tornado.ioloop.PeriodicCallback(self._callback, SSE_HANDLER_QUEUE_CHECK_INTERVAL)
self._heartbeat.start()
# Flush headers immediately so nginx doesn't time out waiting for a response
self.write_message(msg="", event="keepalive")
except Exception as e:
logging.warning("Exception when serving SSE socket", e)
@@ -122,14 +125,19 @@ class APIAlertsStreamHandler(tornado_eventsource.handler.EventSourceHandler):
try:
if self._alert_queue:
while not self._alert_queue.empty():
alert = self._alert_queue.get()
# If the new alert matches our param filters, send it to the client. If not, ignore it.
if alert_allowed_by_query(alert, self._query_params):
if self._credentials:
alert = copy.deepcopy(alert)
alert.infer_missing(self._credentials)
self.write_message(msg=json.dumps(alert, default=serialize_everything))
if not self._alert_queue.empty():
while not self._alert_queue.empty():
alert = self._alert_queue.get()
# If the new alert matches our param filters, send it to the client. If not, ignore it.
if alert_allowed_by_query(alert, self._query_params):
if self._credentials:
alert = copy.deepcopy(alert)
alert.infer_missing(self._credentials)
self.write_message(msg=json.dumps(alert, default=serialize_everything))
else:
# Send a keepalive comment if the queue was empty
self.write_message(msg="", event="keepalive")
if self._alert_queue not in self._sse_alert_queues:
logging.error("Web server cleared up a queue of an active connection!")