tornado: Use spiffy new `call_later` rather than `add_timeout`.

This method was new in Tornado 4.0.  It saves us from having to get
the time ourselves and do the arithmetic -- which not only makes the
code a bit shorter, but also easier to get right.  Tornado docs (see
http://www.tornadoweb.org/en/stable/ioloop.html) say we should have
been getting the time from `ioloop.time()` rather than hardcoding
`time.time()`, because the loop could e.g. be running on the
`time.monotonic()` clock.
This commit is contained in:
Greg Price 2017-11-29 15:15:49 -08:00 committed by Tim Abbott
parent 73886f57d2
commit c32b16715d
4 changed files with 7 additions and 7 deletions

View File

@ -380,7 +380,7 @@ def shutdown_handler(*args, **kwargs):
# type: (*Any, **Any) -> None
io_loop = IOLoop.instance()
if io_loop._callbacks:
io_loop.add_timeout(time.time() + 1, shutdown_handler)
io_loop.call_later(1, shutdown_handler)
else:
io_loop.stop()

View File

@ -221,9 +221,9 @@ class TornadoQueueClient(SimpleQueueClient):
self._reconnect()
except pika.exceptions.AMQPConnectionError:
self.log.critical("Failed to reconnect to RabbitMQ, retrying...")
ioloop.IOLoop.instance().add_timeout(time.time() + retry_seconds, on_timeout)
ioloop.IOLoop.instance().call_later(retry_seconds, on_timeout)
ioloop.IOLoop.instance().add_timeout(time.time() + retry_seconds, on_timeout)
ioloop.IOLoop.instance().call_later(retry_seconds, on_timeout)
def ensure_queue(self, queue_name: str, callback: Callable[[], None]) -> None:
def finish(frame: Any) -> None:

View File

@ -78,7 +78,7 @@ class ClientDescriptor:
self.client_gravatar = client_gravatar
self.all_public_streams = all_public_streams
self.client_type_name = client_type_name
self._timeout_handle = None # type: Any # TODO: should be return type of ioloop.add_timeout
self._timeout_handle = None # type: Any # TODO: should be return type of ioloop.call_later
self.narrow = narrow
self.narrow_filter = build_narrow_filter(narrow)
@ -189,9 +189,9 @@ class ClientDescriptor:
# All clients get heartbeat events
self.add_event(dict(type='heartbeat'))
ioloop = tornado.ioloop.IOLoop.instance()
heartbeat_time = time.time() + HEARTBEAT_MIN_FREQ_SECS + random.randint(0, 10)
interval = HEARTBEAT_MIN_FREQ_SECS + random.randint(0, 10)
if self.client_type_name != 'API: heartbeat test':
self._timeout_handle = ioloop.add_timeout(heartbeat_time, timeout_callback)
self._timeout_handle = ioloop.call_later(interval, timeout_callback)
def disconnect_handler(self, client_closed: bool=False) -> None:
if self.current_handler_id:

View File

@ -103,7 +103,7 @@ class SocketConnection(sockjs.tornado.SockJSConnection):
self.close_info = CloseErrorInfo(408, "Timeout while waiting for authentication")
self.close()
self.timeout_handle = ioloop.add_timeout(time.time() + 10, auth_timeout)
self.timeout_handle = ioloop.call_later(10, auth_timeout)
write_log_line(log_data, path='/socket/open', method='SOCKET',
remote_ip=info.ip, email='unknown', client_name='?')