Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to execute ZMQ_TCP_KEEPALIVE for my application. #3017

Open
sanshaw1 opened this issue Mar 22, 2018 · 8 comments
Open

Not able to execute ZMQ_TCP_KEEPALIVE for my application. #3017

sanshaw1 opened this issue Mar 22, 2018 · 8 comments

Comments

@sanshaw1
Copy link

I have an application that uses ZMQ Pub-Sub Model. I have an algorithm in my sub side that if the socket remains idle for 40 seconds it should disconnect and reconnect again. Now, I want to remove this algorithm. Instead i want to use TCP KeepAlive Packets so that my socket does not remain idle and thus my sub does not disconnect and connect frequently.
Please find my code below:

zmq_socket_t socket;
socket = zmq_socket(_zmq_context, ZMQ_PUB);

int tcp_keepalive = 1, tcp_keepalive_idle = 25;

if(zmq_setsockopt(socket, ZMQ_TCP_KEEPALIVE, &tcp_keepalive, sizeof(tcp_keepalive))) {
INFO(0, "sanjay keepalive error -err %d", tcp_keepalive);
return NULL;
}
else
{
INFO(0, "keepalive set -noerr %d", tcp_keepalive);
}

if(zmq_setsockopt(socket, ZMQ_TCP_KEEPALIVE_IDLE, &tcp_keepalive_idle, sizeof(tcp_keepalive_idle)))
{
INFO(0, "keepalive idle error -err %d", tcp_keepalive_idle);
return NULL;
}
else
{
INFO(0, "idle set -noerr %d", tcp_keepalive_idle);
}
if (zmq_bind(socket, urlp)) {
ERR(0, "zmq_bind(PUB): %s", ERRNO);
FREE(urlp);
return NULL;
}

Please help me on this ?? I am not able to achieve my target. Am i missing anything?

@bluca
Copy link
Member

bluca commented Mar 22, 2018

You forgot to mention what exactly the problem is. Also you probably need to set ZMQ_TCP_KEEPALIVE_INTVL and ZMQ_TCP_KEEPALIVE_CNT. Those are all OS-dependent flags, so you'll want to consult your OS manual to find out what the default values are and what the suggested configuration is.

@sanshaw1
Copy link
Author

i have added TCP_KeepALIVE code and TCP_keepAlive_IDLE code. My problem is that publisher is not sending TCP packets to the SUB so what is happening is the socket is remaining idle, Once the socket remains idle for around 40s, sub thinks that something is blocking and tries to disconnect and reconnect itself. what i want is to send TCP keepalive packets to the sub from the pub. How can i achieve it. is my code ok or do i need to add something.

@bluca
Copy link
Member

bluca commented Mar 23, 2018

The default on Linux is very long, are you sure setting the options is not making things nworse?
Try to set them on the client as well.
There are also heartbeat options - it's the same as keepalives but at the ZMQ protocol layer so it's platform independent

@bluca
Copy link
Member

bluca commented Apr 17, 2018

Any update?

@louiewh
Copy link

louiewh commented Jul 19, 2019

@bluca I also happen to this issue. And I set all the keepalive option like this:

static void SetSocketOpt(zmq::socket_t* socket){
    int32_t keep_alive = 1;
    int32_t keep_alive_idle = 10;
    int32_t keep_alive_cnt = 5;
    int32_t keep_alive_intvl = 5;

    socket->setsockopt(ZMQ_TCP_KEEPALIVE, &keep_alive, sizeof (keep_alive));
    socket->setsockopt(ZMQ_TCP_KEEPALIVE_IDLE, &keep_alive_idle, sizeof (keep_alive_idle));
    socket->setsockopt(ZMQ_TCP_KEEPALIVE_CNT, &keep_alive_cnt, sizeof (keep_alive_cnt));
    socket->setsockopt(ZMQ_TCP_KEEPALIVE_INTVL, &keep_alive_intvl, sizeof (keep_alive_intvl));
}

    zmq::socket_t socket(context, ZMQ_REP);
    snprintf(buff, sizeof(buff), "tcp://%s:%u", cdc_linux_host.c_str(), cdc_linux_port);
    SetSocketOpt(&socket);
    socket.bind(buff);

The result like this:
~# netstat -npo|grep 5050
tcp 0 0 172.20.1.11:5050 172.20.1.1:41927 ESTABLISHED 5777/ecall_service off (0.00/0/0)
tcp 0 0 172.20.1.11:5050 172.20.1.1:42006 ESTABLISHED 5777/ecall_service off (0.00/0/0)
tcp 0 0 172.20.1.11:5050 172.20.1.1:42761 ESTABLISHED 5777/ecall_service off (0.00/0/0)
tcp 0 0 172.20.1.11:5050 172.20.1.1:42422 ESTABLISHED 5777/ecall_service off (0.00/0/0)

@bluca
Copy link
Member

bluca commented Jul 19, 2019

Try to use the heartbeat options instead, which use the same mechanism but inside the protocol rather than at the TCP level

@louiewh
Copy link

louiewh commented Jul 19, 2019

@bluca I find the reason why heatbeat don't work.
Because there are some macro such as ZMQ_HAVE_SO_KEEPALIVE for compile.
cross-compile no effect.
So How open the macro ?
3Q

@bluca
Copy link
Member

bluca commented Jul 19, 2019

You can search online how to correctly set up cross compilation using autotools, we are not doing anything special

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants