Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add/use IDENTIFY command for compatibility with nsqd 0.2.16

  • Loading branch information...
commit 074474afe5fe664faf3e9ba66dadb51e46833cd2 1 parent 6eb3a20
@mreiferson mreiferson authored
Showing with 24 additions and 9 deletions.
  1. +2 −1  nsq/NSQReader.py
  2. +22 −8 nsq/nsq.py
View
3  nsq/NSQReader.py
@@ -315,7 +315,8 @@ def _connect_callback(self, conn, task):
channel = self.channel
initial_ready = self.connection_max_in_flight()
try:
- conn.send(nsq.subscribe(self.topic, channel, self.short_hostname, self.hostname))
+ conn.send(nsq.identify({'short_id': self.short_hostname, 'long_id': self.hostname}))
+ conn.send(nsq.subscribe(self.topic, channel))
conn.send(nsq.ready(initial_ready))
conn.ready = initial_ready
conn.is_sending_ready = False
View
30 nsq/nsq.py
@@ -1,5 +1,10 @@
import struct
import re
+try:
+ import simplejson as json
+except ImportError:
+ import json
+
MAGIC_V2 = " V2"
NL = "\n"
@@ -28,25 +33,34 @@ def decode_message(data):
body = data[26:]
return Message(id, body, timestamp, attempts)
-def _command(cmd, *params):
- return "%s %s%s" % (cmd, ' '.join(params), NL)
+def _command(cmd, body, *params):
+ body_data = ''
+ params_data = ''
+ if body:
+ body_data = struct.pack('>l', len(body)) + body
+ if len(params):
+ params_data = ' ' + ' '.join(params)
+ return "%s%s%s%s" % (cmd, params_data, NL, body_data)
-def subscribe(topic, channel, short_id, long_id):
+def subscribe(topic, channel):
assert valid_topic_name(topic)
assert valid_channel_name(channel)
- return _command('SUB', topic, channel, short_id, long_id)
+ return _command('SUB', None, topic, channel)
+
+def identify(data):
+ return _command('IDENTIFY', json.dumps(data))
def ready(count):
- return _command('RDY', str(count))
+ return _command('RDY', None, str(count))
def finish(id):
- return _command('FIN', id)
+ return _command('FIN', None, id)
def requeue(id, time_ms):
- return _command('REQ', id, time_ms)
+ return _command('REQ', None, id, time_ms)
def nop():
- return _command('NOP')
+ return _command('NOP', None)
def valid_topic_name(topic):
if not 0 < len(topic) < 33:
Please sign in to comment.
Something went wrong with that request. Please try again.