Skip to content

Commit 1ec059c

Browse files
committed
add Writer.close() and Client.close(), improve Reader.close()
make Reader.close() prevent nsqd_tcp_addresses from reconnecting Writer.close() also closes conns and stops from reconnecting Client.close() stops the periodic _check_last_recv_timestamps()
1 parent b6d2bc7 commit 1ec059c

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

nsq/client.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import time
44
import logging
55

6-
import tornado.ioloop
6+
from tornado.ioloop import IOLoop, PeriodicCallback
77

88
logger = logging.getLogger(__name__)
99

1010

1111
class Client(object):
1212
def __init__(self, **kwargs):
13-
self.io_loop = tornado.ioloop.IOLoop.current()
14-
tornado.ioloop.PeriodicCallback(self._check_last_recv_timestamps, 60 * 1000).start()
13+
self.io_loop = IOLoop.current()
14+
self.periodic_check = PeriodicCallback(self._check_last_recv_timestamps, 60 * 1000)
15+
self.periodic_check.start()
1516

1617
def _on_connection_identify(self, conn, data, **kwargs):
1718
logger.info('[%s:%s] IDENTIFY sent %r' % (conn.id, self.name, data))
@@ -75,3 +76,6 @@ def heartbeat(self, conn):
7576
:param conn: the :class:`nsq.AsyncConn` over which the heartbeat was received
7677
"""
7778
pass
79+
80+
def close(self):
81+
self.periodic_check.stop()

nsq/reader.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ def __init__(
221221
self.backoff_block = False
222222
self.backoff_block_completed = True
223223

224+
self._closed = False
224225
self.conns = {}
225226
self.connection_attempts = {}
226227
self.http_client = tornado.httpclient.AsyncHTTPClient()
@@ -264,15 +265,19 @@ def _run(self):
264265

265266
def close(self):
266267
"""
267-
Closes all connections stops all periodic callbacks
268+
Closes all connections and stops all periodic callbacks
268269
"""
270+
self._closed = True
271+
269272
for conn in self.conns.values():
270273
conn.close()
271274

272275
self.redist_periodic.stop()
273276
if self.query_periodic is not None:
274277
self.query_periodic.stop()
275278

279+
super(Reader, self).close()
280+
276281
def set_message_handler(self, message_handler):
277282
"""
278283
Assigns the callback method to be executed for each message received
@@ -494,8 +499,8 @@ def connect_to_nsqd(self, host, port):
494499
# only attempt to re-connect once every 10s per destination
495500
# this throttles reconnects to failed endpoints
496501
now = time.time()
497-
last_connect_attempt = self.connection_attempts.get(conn.id)
498-
if last_connect_attempt and last_connect_attempt > now - 10:
502+
last_connect_attempt = self.connection_attempts.get(conn.id, 0)
503+
if last_connect_attempt > now - 10:
499504
return
500505
self.connection_attempts[conn.id] = now
501506

@@ -559,6 +564,9 @@ def _on_connection_close(self, conn, **kwargs):
559564
self.io_loop.remove_timeout(conn.rdy_timeout)
560565
conn.rdy_timeout = None
561566

567+
if self._closed:
568+
return
569+
562570
if not self.lookupd_http_addresses:
563571
# automatically reconnect to nsqd addresses when not using lookupd
564572
logger.info('[%s:%s] attempting to reconnect in 15s', conn.id, self.name)

nsq/writer.py

+15
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def __init__(self, nsqd_tcp_addresses, reconnect_interval=15.0, name=None, **kwa
104104

105105
self.name = name or nsqd_tcp_addresses[0]
106106
self.nsqd_tcp_addresses = nsqd_tcp_addresses
107+
self._closed = False
107108
self.conns = {}
108109

109110
# Verify keyword arguments
@@ -245,6 +246,9 @@ def _on_connection_close(self, conn, **kwargs):
245246
logger.exception('[%s] uncaught exception in callback', conn.id)
246247

247248
logger.warning('[%s] connection closed', conn.id)
249+
if self._closed:
250+
return
251+
248252
logger.info('[%s] attempting to reconnect in %0.2fs', conn.id, self.reconnect_interval)
249253
reconnect_callback = functools.partial(self.connect_to_nsqd,
250254
host=conn.host, port=conn.port)
@@ -254,3 +258,14 @@ def _finish_pub(self, conn, data, command, topic, msg):
254258
if isinstance(data, protocol.Error):
255259
logger.error('[%s] failed to %s (%s, %s), data is %s',
256260
conn.id if conn else 'NA', command, topic, msg, data)
261+
262+
def close(self):
263+
"""
264+
Closes all connections and stops all periodic callbacks
265+
"""
266+
self._closed = True
267+
268+
for conn in self.conns.values():
269+
conn.close()
270+
271+
super(Writer, self).close()

0 commit comments

Comments
 (0)