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:
0#define BITCOIND_ZMQ_HASHBLOCK "hashblock"
1
2const time_t KReconnectInterval = 600; // seconds
3
4//
5// check if need to reconnect ZMQ
6//
7if (subscriber == nullptr || lastRecvTime + KReconnectInterval < time(nullptr)) {
8 // disconnect
9 if (subscriber != nullptr) {
10 delete subscriber;
11 subscriber = nullptr;
12 }
13
14 // connect
15 subscriber = new zmq::socket_t(zmqContext_, ZMQ_SUB);
16 subscriber->connect(zmqBitcoindAddr_);
17
18 // subscribe topic
19 subscriber->setsockopt(ZMQ_SUBSCRIBE,
20 BITCOIND_ZMQ_HASHBLOCK, strlen(BITCOIND_ZMQ_HASHBLOCK));
21}
22
23//
24// recv ZMQ messages
25//
26
27// ...
28
29// set last recv time
30lastRecvTime = time(nullptr);
31
32// ...
I am not sure if add these options by zmq_setsockopt()
could solve this issue.
0// ZMQ_HEARTBEAT_xxxx available since zmq-4.2.0
1ZMQ_HEARTBEAT_TTL
2ZMQ_HEARTBEAT_TIMEOUT
3ZMQ_HEARTBEAT_IVL
Maybe could add some heartbeat messages when there’s no traffic for a while? I prefer this way.
Some links: