Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no connections for producer when using nsqlookupd #55

Open
jetspeed opened this issue Oct 11, 2019 · 6 comments
Open

no connections for producer when using nsqlookupd #55

jetspeed opened this issue Oct 11, 2019 · 6 comments

Comments

@jetspeed
Copy link

gem version 2.3.1

require 'nsq'

producer = Nsq::Producer.new(nsqlookupd: ['127.0.0.1:4161'], topic: 'qlc')
puts producer.connected?
producer.write('hello world')
producer.terminate

this cause:

gems/nsq-ruby-2.3.1/lib/nsq/producer.rb:92:in connection_for_write': No connections available (RuntimeError) from /home/was/.rvm/gems/ruby-2.2.2@425on222/gems/nsq-ruby-2.3.1/lib/nsq/producer.rb:65:in write_to_topic'
from /home/was/.rvm/gems/ruby-2.2.2@425on222/gems/nsq-ruby-2.3.1/lib/nsq/producer.rb:42:in write' from test.rb:6:in

'

@jetspeed
Copy link
Author

this will occuer when using
ruby test.rb

but in irb, it gets connections.

why? It seems the @connections not updated by the discovery Thread.

@bschwartz
Copy link
Member

I'm guessing it's a race condition. If you're running it line by line in irb, it probably has a short time to do the discovery and make the connection. I'd try polling producer.connected? before writing. Maybe something like:

require 'nsq'
producer = Nsq::Producer.new(nsqlookupd: ['127.0.0.1:4161'], topic: 'qlc')
sleep(0.1) until producer.connected?
producer.write('hello world')
producer.terminate

@jetspeed
Copy link
Author

you're right. could this be improved?

@bschwartz
Copy link
Member

Yes, I see how this could be confusing :)

I suppose the most straightforward thing to do would be to block until a connection is made when creating the Producer, though you can still run into a case where all your connections drop if your nsqds go down.

I think this PR may be the better solution: #52. I haven't had time to fully review and incorporate it unfortunately.

@jetspeed
Copy link
Author

Another problem, when pub to remote nsqds, some messages may loss,
If I add sleep(0.01) before write_to_topic in write method, nothing lost.

So I think the network latency will affect the message pub, and will cause message loss.

@Soulou
Copy link
Contributor

Soulou commented Mar 17, 2020

Hi @jetspeed we had the same issue and it wasn't acceptable.

We've been using #52 in production since the PR has been done and fixed our message loss issue, our master branch is integrating the PR: https://github.com/Scalingo/nsq-ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants