def _on_connection_close(self, conn, **kwargs):
if conn.id in self.conns:
del self.conns[conn.id]
for callback in conn.callback_queue:
try:
callback(conn, nsq.ConnectionClosedError())
except Exception:
logging.exception('[%s] uncaught exception in callback', conn.id)
logging.warning('[%s] connection closed', conn.id)
logging.info('[%s] attempting to reconnect in %0.2fs', conn.id, self.reconnect_interval)
reconnect_callback = functools.partial(self.connect_to_nsqd,
host=conn.host, port=conn.port)
self.io_loop.add_timeout(time.time() + self.reconnect_interval, reconnect_callback)
the pynsq will reconnect after the self.reconnect_interval, I can't understand the meanings. I want to know why the connect closed?
def _check_last_recv_timestamps(self):
now = time.time()
def is_stale(conn):
timestamp = conn.last_recv_timestamp
return (now - timestamp) > ((conn.heartbeat_interval * 2) / 1000.0)
# first get the list of stale connections, then close
# (`conn.close()` may modify the list of connections while we're iterating)
stale_connections = [conn for conn in self.conns.values() if is_stale(conn)]
for conn in stale_connections:
timestamp = conn.last_recv_timestamp
# this connection hasnt received data for more than
# the configured heartbeat interval, close it
logging.warning('[%s:%s] connection is stale (%.02fs), closing',
conn.id, self.name, (now - timestamp))
conn.close()
when there is no recv data more than two heartbreak interval, the connection will be close. why do this?