Remove update_current_time refresh (#1502)

This commit is contained in:
Markus Lång 2019-03-03 19:22:26 +02:00 committed by Stephen Sadowski
parent 4260528645
commit e5c7589fc0

View File

@ -34,9 +34,6 @@ except ImportError:
pass pass
current_time = None
class Signal: class Signal:
stopped = False stopped = False
@ -171,7 +168,7 @@ class HttpProtocol(asyncio.Protocol):
self.request_timeout, self.request_timeout_callback self.request_timeout, self.request_timeout_callback
) )
self.transport = transport self.transport = transport
self._last_request_time = current_time self._last_request_time = time()
def connection_lost(self, exc): def connection_lost(self, exc):
self.connections.discard(self) self.connections.discard(self)
@ -197,7 +194,7 @@ class HttpProtocol(asyncio.Protocol):
# exactly what this timeout is checking for. # exactly what this timeout is checking for.
# Check if elapsed time since request initiated exceeds our # Check if elapsed time since request initiated exceeds our
# configured maximum request timeout value # configured maximum request timeout value
time_elapsed = current_time - self._last_request_time time_elapsed = time() - self._last_request_time
if time_elapsed < self.request_timeout: if time_elapsed < self.request_timeout:
time_left = self.request_timeout - time_elapsed time_left = self.request_timeout - time_elapsed
self._request_timeout_handler = self.loop.call_later( self._request_timeout_handler = self.loop.call_later(
@ -213,7 +210,7 @@ class HttpProtocol(asyncio.Protocol):
def response_timeout_callback(self): def response_timeout_callback(self):
# Check if elapsed time since response was initiated exceeds our # Check if elapsed time since response was initiated exceeds our
# configured maximum request timeout value # configured maximum request timeout value
time_elapsed = current_time - self._last_request_time time_elapsed = time() - self._last_request_time
if time_elapsed < self.response_timeout: if time_elapsed < self.response_timeout:
time_left = self.response_timeout - time_elapsed time_left = self.response_timeout - time_elapsed
self._response_timeout_handler = self.loop.call_later( self._response_timeout_handler = self.loop.call_later(
@ -234,7 +231,7 @@ class HttpProtocol(asyncio.Protocol):
:return: None :return: None
""" """
time_elapsed = current_time - self._last_response_time time_elapsed = time() - self._last_response_time
if time_elapsed < self.keep_alive_timeout: if time_elapsed < self.keep_alive_timeout:
time_left = self.keep_alive_timeout - time_elapsed time_left = self.keep_alive_timeout - time_elapsed
self._keep_alive_timeout_handler = self.loop.call_later( self._keep_alive_timeout_handler = self.loop.call_later(
@ -362,7 +359,7 @@ class HttpProtocol(asyncio.Protocol):
self._response_timeout_handler = self.loop.call_later( self._response_timeout_handler = self.loop.call_later(
self.response_timeout, self.response_timeout_callback self.response_timeout, self.response_timeout_callback
) )
self._last_request_time = current_time self._last_request_time = time()
self._request_handler_task = self.loop.create_task( self._request_handler_task = self.loop.create_task(
self.request_handler( self.request_handler(
self.request, self.write_response, self.stream_response self.request, self.write_response, self.stream_response
@ -449,7 +446,7 @@ class HttpProtocol(asyncio.Protocol):
self._keep_alive_timeout_handler = self.loop.call_later( self._keep_alive_timeout_handler = self.loop.call_later(
self.keep_alive_timeout, self.keep_alive_timeout_callback self.keep_alive_timeout, self.keep_alive_timeout_callback
) )
self._last_response_time = current_time self._last_response_time = time()
self.cleanup() self.cleanup()
async def drain(self): async def drain(self):
@ -502,7 +499,7 @@ class HttpProtocol(asyncio.Protocol):
self._keep_alive_timeout_handler = self.loop.call_later( self._keep_alive_timeout_handler = self.loop.call_later(
self.keep_alive_timeout, self.keep_alive_timeout_callback self.keep_alive_timeout, self.keep_alive_timeout_callback
) )
self._last_response_time = current_time self._last_response_time = time()
self.cleanup() self.cleanup()
def write_error(self, exception): def write_error(self, exception):
@ -595,18 +592,6 @@ class HttpProtocol(asyncio.Protocol):
self.transport = None self.transport = None
def update_current_time(loop):
"""Cache the current time, since it is needed at the end of every
keep-alive request to update the request timeout time
:param loop:
:return:
"""
global current_time
current_time = time()
loop.call_later(1, partial(update_current_time, loop))
def trigger_events(events, loop): def trigger_events(events, loop):
"""Trigger event callbacks (functions or async) """Trigger event callbacks (functions or async)
@ -751,10 +736,6 @@ def serve(
**asyncio_server_kwargs **asyncio_server_kwargs
) )
# Instead of pulling time at the end of every request,
# pull it once per minute
loop.call_soon(partial(update_current_time, loop))
if run_async: if run_async:
return server_coroutine return server_coroutine