From 6e375850c1e53230657df40c7607634ed330e228 Mon Sep 17 00:00:00 2001 From: "n.feofanov" Date: Wed, 4 Oct 2023 14:30:10 +0300 Subject: [PATCH] update sanic/http protocols `close` methods --- sanic/server/protocols/base_protocol.py | 5 +++-- sanic/server/protocols/http_protocol.py | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/sanic/server/protocols/base_protocol.py b/sanic/server/protocols/base_protocol.py index bf1b3c15..2e8a115e 100644 --- a/sanic/server/protocols/base_protocol.py +++ b/sanic/server/protocols/base_protocol.py @@ -90,8 +90,9 @@ class SanicProtocol(asyncio.Protocol): if self.transport: self.transport.close() if timeout is None: - timeout = self.app.config.GRACEFUL_SHUTDOWN_TIMEOUT - self.loop.call_later(timeout, self.abort) + self.abort() + else: + self.loop.call_later(timeout, self.abort) def abort(self): """ diff --git a/sanic/server/protocols/http_protocol.py b/sanic/server/protocols/http_protocol.py index 6e30fc0f..689db2ac 100644 --- a/sanic/server/protocols/http_protocol.py +++ b/sanic/server/protocols/http_protocol.py @@ -109,6 +109,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta): "_http", "_exception", "recv_buffer", + "_callback_check_timeouts", ) def __init__( @@ -135,6 +136,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta): if "requests_count" not in self.state: self.state["requests_count"] = 0 self._exception = None + self._callback_check_timeouts = None async def connection_task(self): # no cov """ @@ -214,7 +216,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta): ) / 2 ) - self.loop.call_later(max(0.1, interval), self.check_timeouts) + self._callback_check_timeouts = self.loop.call_later(max(0.1, interval), self.check_timeouts) return cancel_msg_args = () if sys.version_info >= (3, 9): @@ -223,6 +225,19 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta): except Exception: error_logger.exception("protocol.check_timeouts") + def close(self, timeout: Optional[float] = None): + """ + Requires to prevent checking timeouts for closed connections + """ + if timeout is not None: + super().close(timeout=timeout) + return + if self._callback_check_timeouts: + self._callback_check_timeouts.cancel() + if self.transport: + self.transport.close() + self.abort() + async def send(self, data): # no cov """ Writes HTTP data with backpressure control.