Skip to content

[zmq] pub/sub is not reliable at all #12754

@bitkevin

Description

@bitkevin

The client only subscribe hashblock topic, if there's no block found over than about 30 minutes on the network, the pub/sub will timeout and the client can't detect anything unusual, the client will keep waiting forever.

I fix this by reconnecting, example code:

#define BITCOIND_ZMQ_HASHBLOCK      "hashblock"

const time_t KReconnectInterval = 600;  // seconds

//
// check if need to reconnect ZMQ
//
if (subscriber == nullptr || lastRecvTime + KReconnectInterval < time(nullptr)) {
  // disconnect
  if (subscriber != nullptr) {
    delete subscriber;
    subscriber = nullptr;
  }

  // connect
  subscriber = new zmq::socket_t(zmqContext_, ZMQ_SUB);
  subscriber->connect(zmqBitcoindAddr_);

  // subscribe topic
  subscriber->setsockopt(ZMQ_SUBSCRIBE,
                         BITCOIND_ZMQ_HASHBLOCK, strlen(BITCOIND_ZMQ_HASHBLOCK));
}

//
// recv ZMQ messages
//

// ...

// set last recv time
lastRecvTime = time(nullptr);

// ...

I am not sure if add these options by zmq_setsockopt() could solve this issue.

// ZMQ_HEARTBEAT_xxxx available since zmq-4.2.0
ZMQ_HEARTBEAT_TTL
ZMQ_HEARTBEAT_TIMEOUT
ZMQ_HEARTBEAT_IVL

Maybe could add some heartbeat messages when there's no traffic for a while? I prefer this way.

Some links:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions