nsqio / go-nsq

The official Go package for NSQ
MIT License
2.59k stars 444 forks source link

Does single producer support multi thread ? #278

Closed mill888 closed 4 years ago

mill888 commented 4 years ago

when we want to produce message to nsq, we connect to nsq by NewProducer(addr,nsq.NewConfig() ), then we can send message to nsq by this producer;

But in real world, the server run in multi threads, and if we want to send message, we had to create a new producer by NewProducer ? Because in behind of the "NewProducer " , it had to connect to nsq server, and it woud reduce the performance of my server;

So I want to create only one producer when my server start, and in the multi threads of my server, using the producer to send messages again and again.

So, does nsq support this ?

ploxiln commented 4 years ago

This is a question about the go-nsq client library, so I'll move this there.

ploxiln commented 4 years ago

Yes, the Producer can be used from multiple goroutines running in parallel on different OS threads and cpu cores.

Is the performance "good enough"? Usually, but maybe not always. The limitation would be the channel for transferring commands to the socket writing goroutine, or the socket sending itself. Users with extreme needs can create multiple Producer in a single process, as you have done.