update sanic/http protocols close methods

This commit is contained in:
n.feofanov 2023-10-04 14:30:10 +03:00
parent a5a9658896
commit 6e375850c1
2 changed files with 19 additions and 3 deletions

View File

@ -90,8 +90,9 @@ class SanicProtocol(asyncio.Protocol):
if self.transport: if self.transport:
self.transport.close() self.transport.close()
if timeout is None: if timeout is None:
timeout = self.app.config.GRACEFUL_SHUTDOWN_TIMEOUT self.abort()
self.loop.call_later(timeout, self.abort) else:
self.loop.call_later(timeout, self.abort)
def abort(self): def abort(self):
""" """

View File

@ -109,6 +109,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta):
"_http", "_http",
"_exception", "_exception",
"recv_buffer", "recv_buffer",
"_callback_check_timeouts",
) )
def __init__( def __init__(
@ -135,6 +136,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta):
if "requests_count" not in self.state: if "requests_count" not in self.state:
self.state["requests_count"] = 0 self.state["requests_count"] = 0
self._exception = None self._exception = None
self._callback_check_timeouts = None
async def connection_task(self): # no cov async def connection_task(self): # no cov
""" """
@ -214,7 +216,7 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta):
) )
/ 2 / 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 return
cancel_msg_args = () cancel_msg_args = ()
if sys.version_info >= (3, 9): if sys.version_info >= (3, 9):
@ -223,6 +225,19 @@ class HttpProtocol(HttpProtocolMixin, SanicProtocol, metaclass=TouchUpMeta):
except Exception: except Exception:
error_logger.exception("protocol.check_timeouts") 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 async def send(self, data): # no cov
""" """
Writes HTTP data with backpressure control. Writes HTTP data with backpressure control.