From 058ad60b9a5b9dae444279bc5cee6d040b0b08d7 Mon Sep 17 00:00:00 2001 From: George Cockshull Date: Thu, 12 Oct 2023 11:24:57 -0400 Subject: [PATCH] zmq_proxy_steerable: support non-REP socket types Problem: reimplemented zmq_proxy_steerable control socket type is not backwards compatible - replies are always sent. In the past, zmq_proxy_steerable never sent a reply for commands that weren't STATISTICS, so only really worked with PAIR and didn't work at all with REP. Now it only supports REP and PAIR semantics changed. This breaks compatibility with PAIR in a subtle and slightly annoying way if HWMs are hit without reading the replies. Solution: Add a check to send the empty reply only for REP control sockets. This restores backwards compatibility, and supports REP, PAIR, and SUB (for non-reply commands). I had no knowledge of the pre-MPL-2.0 implementation. This fix is based on docs and prior API usage. I contribute this under MPL-2.0. --- doc/zmq_proxy_steerable.txt | 12 +++++++----- src/proxy.cpp | 15 ++++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/doc/zmq_proxy_steerable.txt b/doc/zmq_proxy_steerable.txt index c5c0f661..23081fa9 100644 --- a/doc/zmq_proxy_steerable.txt +++ b/doc/zmq_proxy_steerable.txt @@ -19,21 +19,21 @@ The _zmq_proxy_steerable()_ function is a variant of the _zmq_proxy()_ function. It accepts a fourth _control_ socket. When the _control_ socket is _NULL_ the two functions operate identically. -When a _control_ socket of type _REP_ is provided to the proxy function the +When a _control_ socket of type _REP_ or _PAIR_ is provided to the proxy function the application may send commands to the proxy. The following commands are supported. _PAUSE_:: - The proxy will cease transferring messages between its endpoints. + The proxy will cease transferring messages between its endpoints. _REP_ control socket will reply with an empty message. No response otherwise. _RESUME_:: - The proxy will resume transferring messages between its endpoints. + The proxy will resume transferring messages between its endpoints. _REP_ control socket will reply with an empty message. No response otherwise. _TERMINATE_:: - The proxy function will exit with a return value of 0. + The proxy function will exit with a return value of 0. _REP_ control socket will reply with an empty message. No response otherwise. _STATISTICS_:: - The proxy behavior will remain unchanged and reply with a set of simple summary values of the messages that have been sent through the proxy as described next. + The proxy state will remain unchanged and reply with a set of simple summary values of the messages that have been sent through the proxy as described next. Control socket must support sending. There are eight statistics values, each of size _uint64_t_ in the multi-part message reply to the _STATISTICS_ command. These are: @@ -54,6 +54,8 @@ message reply to the _STATISTICS_ command. These are: - number of bytes sent by the backend socket +Message totals count each part in a multipart message individually. + RETURN VALUE ------------ diff --git a/src/proxy.cpp b/src/proxy.cpp index 2d6845fc..5ba77b37 100644 --- a/src/proxy.cpp +++ b/src/proxy.cpp @@ -197,11 +197,16 @@ static int handle_control (class zmq::socket_base_t *control_, state = terminated; } - // satisfy REP duty and reply no matter what. - cmsg.init_size (0); - rc = control_->send (&cmsg, 0); - if (unlikely (rc < 0)) { - return -1; + int type; + size_t sz = sizeof (type); + zmq_getsockopt (control_, ZMQ_TYPE, &type, &sz); + if (type == ZMQ_REP) { + // satisfy REP duty and reply no matter what. + cmsg.init_size (0); + rc = control_->send (&cmsg, 0); + if (unlikely (rc < 0)) { + return -1; + } } return 0; }