[zeromq-dev] Concurrent send/receive with a ROUTER socket - efficient and idiomatic solution

Eyal Arubas eyalarubas at gmail.com
Sat Aug 30 17:35:09 CEST 2014

I am implementing a server in Go with a ROUTER socket.

In one goroutine, messages are received from the socket, and distributed to
workers through work queues (buffered channels).
Workers produce some result and queue it to another buffered channel.
In another goroutine, results are dequeued and sent back to clients.

The same ROUTER socket needs to be accessed from those two goroutines (one
to receive and one to send), which creates concurrency problems; as sockets
are not threads-safe.

As a demonstration, the following Go code panics randomly with:

fatal error: unexpected signal during runtime execution

Link to demo:

My current solution is to use a poller with a short timeout, and a mutex to
lock access to the socket. But this seems inefficient and not idiomatic.
Why should I poll and lock the socket, with some arbitrary timeout, if
nothing is there?

Link to my current solution:

Is there a better way?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20140830/dedc7493/attachment.html>

More information about the zeromq-dev mailing list