2022-02-23

ZMQ detect client unavailability with heartbeat

I am experiencing with zmq, but I could not find an answer in the docs and examples that the proper use of the following socket options:

  • ZMQ_HEARTBEAT_IVL
  • ZMQ_HEARTBEAT_TIMEOUT
  • ZMQ_HEARTBEAT_TTL

The documentation states that they can be used with any socket types ("when using connection-oriented transports"). Is there a way to use them with REQ/REP socket pairs?

Test client code:

zmq::context_t context( 1 );
zmq::socket_t client( context, ZMQ_REQ );
client.connect( "tcp://localhost:5555" );

// Send request.
std::string request( "test message" );
zmq::message_t message( string.size() );
memcpy( message.data(), string.data(), string.size() );
socket.send( message, flags );

// Wait for reply.
socket.recv( &message );

// Do some work that takes cca 10 secs.
// ...

delete client;

Test server code:

zmq::context_t context( 1 );
zmq::socket_t server( context, zmq::socket_type::rep );

//server.set( zmq::sockopt::rcvtimeo, 5000 );
server.set( zmq::sockopt::heartbeat_ivl, 1000 );
server.set( zmq::sockopt::heartbeat_timeout, 3000 );
server.set( zmq::sockopt::heartbeat_ttl, 3000 );

server.bind( "tcp://*:5555" );

// Wait for request.
zmq::message_t message;
server.recv( &message );

// Send reply.
zmq::message_t message2("Test reply.", 11);
server.send( message);

// Wait for new request.
server.recv( &message );

// ...

My goal is to end the server side after the client is done. Socket option zmq::sockopt::rcvtimeo works well, it closes the connection on the server side. I thought that the heartbeat socket options would cause something similar, I mean close the server after the client has finished its job and not earlier. I tried them in all possible combinations on both server and client sides, but the server side always gets stuck in the second recv opretation. Checking the localhost network traffic with Wireshark tells that the ping-pong messages are delivered, but that's all.

So my questions are:

  • Can these socket options be used in a REQ/REP connection?
  • If yes, then how? Which side shall set these socket options?
  • If no, then what socket types are recommended?

Thanks in advance!



No comments:

Post a Comment