From d5dcf80cf7b666ab0dbbb4299fd6e9a13f80ad6f Mon Sep 17 00:00:00 2001 From: cdeler Date: Mon, 26 Oct 2020 21:44:02 +0300 Subject: [PATCH] Cancel old keepalive-trigger before setting new one. (#832) --- uvicorn/protocols/http/h11_impl.py | 7 ++++++- uvicorn/protocols/http/httptools_impl.py | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/uvicorn/protocols/http/h11_impl.py b/uvicorn/protocols/http/h11_impl.py index fb70038ef..6a362ff20 100644 --- a/uvicorn/protocols/http/h11_impl.py +++ b/uvicorn/protocols/http/h11_impl.py @@ -153,11 +153,14 @@ def connection_lost(self, exc): def eof_received(self): pass - def data_received(self, data): + def _unset_keepalive_if_required(self): if self.timeout_keep_alive_task is not None: self.timeout_keep_alive_task.cancel() self.timeout_keep_alive_task = None + def data_received(self, data): + self._unset_keepalive_if_required() + self.conn.receive_data(data) self.handle_events() @@ -299,6 +302,8 @@ def on_response_complete(self): return # Set a short Keep-Alive timeout. + self._unset_keepalive_if_required() + self.timeout_keep_alive_task = self.loop.call_later( self.timeout_keep_alive, self.timeout_keep_alive_handler ) diff --git a/uvicorn/protocols/http/httptools_impl.py b/uvicorn/protocols/http/httptools_impl.py index 1642b7abb..15262326b 100644 --- a/uvicorn/protocols/http/httptools_impl.py +++ b/uvicorn/protocols/http/httptools_impl.py @@ -153,11 +153,14 @@ def connection_lost(self, exc): def eof_received(self): pass - def data_received(self, data): + def _unset_keepalive_if_required(self): if self.timeout_keep_alive_task is not None: self.timeout_keep_alive_task.cancel() self.timeout_keep_alive_task = None + def data_received(self, data): + self._unset_keepalive_if_required() + try: self.parser.feed_data(data) except httptools.HttpParserError: @@ -301,6 +304,8 @@ def on_response_complete(self): return # Set a short Keep-Alive timeout. + self._unset_keepalive_if_required() + self.timeout_keep_alive_task = self.loop.call_later( self.timeout_keep_alive, self.timeout_keep_alive_handler )