wistia / nsq-ruby

NSQ Ruby client
MIT License
68 stars 27 forks source link

Unpack attempts as an unsigned 16-bit integer (rather than a signed int16) #46

Closed leklund closed 6 years ago

leklund commented 6 years ago

After implementing max_attempts I noticed some rather old messages that had negative attempt counts. After investigating I noticed that the nsqd message format defines attempts as an unsigned 16-bit integer. The current unpack implementation is unpacking it as a signed integer which means you end up with negative attempts in ruby-land.

NSQ message format: https://github.com/nsqio/nsq/blob/cc29d7202e35b6d94d361fa9890077048a1d0763/nsqd/message.go#L68-L77

Ruby unpack reference: https://ruby-doc.org/core-2.4.0/String.html#method-i-unpack

example:

[6] pry(main)> msg.data.unpack("Q>S>a16a*")[1]
=> 41677
[7] pry(main)> msg.data.unpack("Q>s>a16a*")[1]
=> -23859
[8] pry(main)> msg.attempts
=> -23859
bschwartz commented 6 years ago

Oh, great find! Thanks @leklund & @lanej.

bschwartz commented 6 years ago

Just pushed a patch update. Thanks! https://rubygems.org/gems/nsq-ruby/versions/2.3.1

leklund commented 6 years ago

Thanks @bschwartz!