Problems with Inverted PUB / SUB with ZeroMQ

I am trying to set up an inverted PUB / SUB using ZeroMQ.

This means that subscription (SUB) sockets belong to several long-lived servers, making zmq_bind() ; and Publishing (PUB) is a short-lived client and zmq_connect() .

I use a single ipc:// socket.

I expect a message from the publisher to reach each of the subscribers.

Problem: Only one of the subscriber processes ever receives messages. If this process dies, the publisher is stuck in zmq_term() .

Is this operation mode supported by zmq? If so, what am I doing wrong? If not, how do I implement what I need?

A minimal example with some additional details (in Lua, but that doesn't matter): https://gist.github.com/938429

+6
source share
3 answers

You cannot bind multiple sockets to the same ipc:// address (we are talking about the Unix Domain Socket here ipc: ///tmp/test.ipc == file / tmp / test.ipc).

What you can do is bind each SUB socket to a different ipc: // address and connect one of the publishers to each of these addresses. ZeroMQ allows a single socket to communicate / connect to multiple addresses.

A lock on zmq_term () will most likely be associated with a lingering closure problem (i.e. there is a message that is trying to send a PUB socket). Take a look at the ZMQ_LINGER option.

+6
source

There is a β€œfunction” for ipc: // transport, which is that if two processes are bound to the same IPC endpoint, the second will silently steal the binding from the first. This β€œfeature” was designed to allow processes to recover easily from a failure.

That is why only one subscriber receives messages.

Since you have only one publisher, why not connect the publisher and connect subscribers to it? Even if the publisher comes and goes, subscribers will automatically connect.

+4
source

You cannot bind multiple sockets to the same address on the same computer, be it ipc or tcp, SUB / PUB or REQ / REP. This is similar to network socket binding.

The way to send messages to all subscribers from many publishers is to implement a simple broker that communicates with the SUB address and PUB address. Publishers connect to the SUB socket to send messages, and subscribers connect to the PUB socket of the same broker, and the broker simply redirects all messages received from the SUB socket to the PUB socket. This requires some performance, but is fairly easy to program.

In ZeroMQ 2.0, you can use the executable zmq_forwarder for this purpose, in 2.1 see the zmq_device (3) function.

+2
source

Source: https://habr.com/ru/post/886522/


All Articles