zeromq / libzmq

ZeroMQ core engine in C++, implements ZMTP/3.1
https://www.zeromq.org
Mozilla Public License 2.0
9.62k stars 2.35k forks source link

Should zmq_setsockopt be called before zmq_connect? #4532

Open zhangyaothu opened 1 year ago

zhangyaothu commented 1 year ago

Please use this template for reporting suspected bugs or requests for help.

Issue description

I use publisher with zmq_bind and subscriber with zmq_connect. I want subscriber could discover publisher is lost for some errors and reconnect again, so I set ZMQ_HEARTBEAT_IVL, ZMQ_HEARTBEAT_TIMEOUT and ZMQ_HEARTBEAT_TTL for the subscriber socket.

I find the order of calling zmq_setsockopt and zmq_connect will influnce who send ZMTP PING. But I expect the subscriber always sends SMTP PING.

Environment

Minimal test code / Steps to reproduce the issue

  1. for subscriber

publisher send PING

  zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0);
  int heartbeat_ms = 100;
  int heartbeat_timeout = 200;
  zmq_setsockopt(socket, ZMQ_HEARTBEAT_IVL, &heartbeat_ms, sizeof(int));
  zmq_setsockopt(socket, ZMQ_HEARTBEAT_TIMEOUT, &heartbeat_timeout, sizeof(int));
  zmq_setsockopt(socket, ZMQ_HEARTBEAT_TTL, &heartbeat_timeout, sizeof(int));
  zmq_connect(socket, "tcp://localhost:8080");

subscriber send PING

  zmq_connect(socket, "tcp://localhost:8080");
  zmq_setsockopt(socket, ZMQ_SUBSCRIBE, "", 0);
  int heartbeat_ms = 100;
  int heartbeat_timeout = 200;
  zmq_setsockopt(socket, ZMQ_HEARTBEAT_IVL, &heartbeat_ms, sizeof(int));
  zmq_setsockopt(socket, ZMQ_HEARTBEAT_TIMEOUT, &heartbeat_timeout, sizeof(int));
  zmq_setsockopt(socket, ZMQ_HEARTBEAT_TTL, &heartbeat_timeout, sizeof(int));
mrx23dot commented 1 year ago

My view, setsockopt should give back error if called as not intended (before/after bind/connect)