stop keepalive loop promptly on finish

rather than waiting until keepalive_interval, which leaves idle coroutines for at least 8 seconds
This commit is contained in:
Min RK
2018-09-24 15:05:39 +02:00
parent 33c6e68b5e
commit fcbc6e06c8

View File

@@ -443,9 +443,12 @@ class SpawnProgressAPIHandler(APIHandler):
# raise Finish to halt the handler
raise web.Finish()
_finished = False
def initialize(self):
super().initialize()
self._finish_future = asyncio.Future()
def on_finish(self):
self._finished = True
self._finish_future.set_result(None)
async def keepalive(self):
"""Write empty lines periodically
@@ -453,12 +456,17 @@ class SpawnProgressAPIHandler(APIHandler):
to avoid being closed by intermediate proxies
when there's a large gap between events.
"""
while not self._finished:
while not self._finish_future.done():
try:
self.write("\n\n")
await self.flush()
except (StreamClosedError, RuntimeError):
return
await asyncio.sleep(self.keepalive_interval)
await asyncio.wait(
[self._finish_future],
timeout=self.keepalive_interval,
)
@admin_or_self
async def get(self, username, server_name=''):