Skip to content

Commit c276df7

Browse files
committed
zmq: enable tcp keepalive
1 parent e74649e commit c276df7

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

doc/zmq.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ ZMQ_SUBSCRIBE option set to one or either of these prefixes (for
9898
instance, just `hash`); without doing so will result in no messages
9999
arriving. Please see `contrib/zmq/zmq_sub.py` for a working example.
100100

101+
The ZMQ_PUB socket's ZMQ_TCP_KEEPALIVE option is enabled. This means that
102+
the underlying SO_KEEPALIVE option is enabled when using a TCP transport.
103+
The effective TCP keepalive values are managed through the underlying
104+
operating system configuration and must be configured prior to connection establishment.
105+
106+
For example, when running on GNU/Linux, one might use the following
107+
to lower the keepalive setting to 10 minutes:
108+
109+
sudo sysctl -w net.ipv4.tcp_keepalive_time=600
110+
111+
Setting the keepalive values appropriately for your operating environment may
112+
improve connectivity in situations where long-lived connections are silently
113+
dropped by network middle boxes.
114+
101115
## Remarks
102116

103117
From the perspective of bitcoind, the ZeroMQ socket is write-only; PUB

src/zmq/zmqpublishnotifier.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
8686
return false;
8787
}
8888

89+
const int so_keepalive_option {1};
90+
rc = zmq_setsockopt(psocket, ZMQ_TCP_KEEPALIVE, &so_keepalive_option, sizeof(so_keepalive_option));
91+
if (rc != 0) {
92+
zmqError("Failed to set SO_KEEPALIVE");
93+
zmq_close(psocket);
94+
return false;
95+
}
96+
8997
rc = zmq_bind(psocket, address.c_str());
9098
if (rc != 0)
9199
{

0 commit comments

Comments
 (0)