You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by "bevis.peters@baesystems.com" <be...@baesystems.com> on 2017/03/29 09:05:20 UTC

thrift C++ server timeout client connections

I am using apache thrift 0.9.3 in C++ with a TThreadPoolServer with a 15 thread limit.

I am running into a problem whereby a remote client may connect to my server, and so use up a thread, but then never disconnect. That connection thread on the server then remains "in use" forever. If this happens 15 times, my server blocks up.

Is there any way to tell the server to disconnect a client after a set time? This seems to be such an obvious problem, I feel there must be a solution which I've missed.



Bevis

Please consider the environment before printing this email. This message should be regarded as confidential. If you have received this email in error please notify the sender and destroy it immediately. Statements of intent shall only become binding when confirmed in hard copy by an authorised signatory. The contents of this email may relate to dealings with other companies under the control of BAE Systems Applied Intelligence Limited, details of which can be found at http://www.baesystems.com/Businesses/index.htm.

RE: thrift C++ server timeout client connections

Posted by "bevis.peters@baesystems.com" <be...@baesystems.com>.
Yes, I did eventually discover the setKeepAlive() call, thank you!



Bevis


-----Original Message-----
From: Gajanan H [mailto:grhegde09@gmail.com]
Sent: 13 April 2017 13:23
To: user@thrift.apache.org
Subject: Re: thrift C++ server timeout client connections


Hi Bevis,

You can enable keep-alive on the server. Here is some sample C++ code.

auto tServerSocket = new apache::thrift::transport::TServerSocket (port);
tServerSocket->setKeepAlive (true);

This enables tcp keep-alive
<http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html > on the server with default timeouts. If you want to set your own timeout values, you can patch thrift and set them. Here is some sample code.
In file lib/cpp/src/thrift/transport/TSocket.cpp

#ifdef __linux__
  if(keepAlive_){
    /**
    * Set TCP_KEEPIDLE
    */
    value = 1;
    ret = setsockopt(socket_, SOL_TCP, TCP_KEEPIDLE, const_cast_sockopt(&value), sizeof(value));

    if (ret == -1) {
    int errno_copy = THRIFT_GET_SOCKET_ERROR;  // Copy THRIFT_GET_SOCKET_ERROR because we're allocating memory.
    GlobalOutput.perror("TSocket::setKeepAlive() setsockopt(TCP_KEEPIDLE) "
+ getSocketInfo(), errno_copy);
    }

    /**
    * Set TCP_KEEPINTVL
    */
    value = 1;
    ret = setsockopt(socket_, SOL_TCP, TCP_KEEPINTVL, const_cast_sockopt(&value), sizeof(value));

    if (ret == -1) {
    int errno_copy = THRIFT_GET_SOCKET_ERROR;  // Copy THRIFT_GET_SOCKET_ERROR because we're allocating memory.
    GlobalOutput.perror("TSocket::setKeepAlive() setsockopt(TCP_KEEPINTVL) " + getSocketInfo(), errno_copy);
    }
    /**
    * Set TCP_KEEPCNT
    */
    value = 3;
    ret = setsockopt(socket_, SOL_TCP, TCP_KEEPCNT, const_cast_sockopt(&value), sizeof(value));

    if (ret == -1) {
    int errno_copy = THRIFT_GET_SOCKET_ERROR;  // Copy THRIFT_GET_SOCKET_ERROR because we're allocating memory.
    GlobalOutput.perror("TSocket::setKeepAlive() setsockopt(TCP_KEEPCNT) "
+ getSocketInfo(), errno_copy);
    }
  }
#endif

Hope this helps!

Regards,
Gajanan

On Wed, Mar 29, 2017 at 2:35 PM, bevis.peters@baesystems.com < bevis.peters@baesystems.com> wrote:

> I am using apache thrift 0.9.3 in C++ with a TThreadPoolServer with a
> 15 thread limit.
>
> I am running into a problem whereby a remote client may connect to my
> server, and so use up a thread, but then never disconnect. That
> connection thread on the server then remains "in use" forever. If this
> happens 15 times, my server blocks up.
>
> Is there any way to tell the server to disconnect a client after a set
> time? This seems to be such an obvious problem, I feel there must be a
> solution which I've missed.
>
>
>
> Bevis
>
> Please consider the environment before printing this email. This
> message should be regarded as confidential. If you have received this
> email in error please notify the sender and destroy it immediately.
> Statements of intent shall only become binding when confirmed in hard
> copy by an authorised signatory. The contents of this email may relate
> to dealings with other companies under the control of BAE Systems
> Applied Intelligence Limited, details of which can be found at
> http://www.baesystems.com/Busi nesses/index.htm.
>
Please consider the environment before printing this email. This message should be regarded as confidential. If you have received this email in error please notify the sender and destroy it immediately. Statements of intent shall only become binding when confirmed in hard copy by an authorised signatory. The contents of this email may relate to dealings with other companies under the control of BAE Systems Applied Intelligence Limited, details of which can be found at http://www.baesystems.com/Businesses/index.htm.

Re: thrift C++ server timeout client connections

Posted by Gajanan H <gr...@gmail.com>.
Hi Bevis,

You can enable keep-alive on the server. Here is some sample C++ code.

auto tServerSocket = new apache::thrift::transport::TServerSocket (port);
tServerSocket->setKeepAlive (true);

This enables tcp keep-alive
<http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/usingkeepalive.html> on the
server with default timeouts. If you want to set your own timeout values,
you can patch thrift and set them. Here is some sample code.
In file lib/cpp/src/thrift/transport/TSocket.cpp

#ifdef __linux__
  if(keepAlive_){
    /**
    * Set TCP_KEEPIDLE
    */
    value = 1;
    ret = setsockopt(socket_, SOL_TCP, TCP_KEEPIDLE,
const_cast_sockopt(&value), sizeof(value));

    if (ret == -1) {
    int errno_copy = THRIFT_GET_SOCKET_ERROR;  // Copy
THRIFT_GET_SOCKET_ERROR because we're allocating memory.
    GlobalOutput.perror("TSocket::setKeepAlive() setsockopt(TCP_KEEPIDLE) "
+ getSocketInfo(), errno_copy);
    }

    /**
    * Set TCP_KEEPINTVL
    */
    value = 1;
    ret = setsockopt(socket_, SOL_TCP, TCP_KEEPINTVL,
const_cast_sockopt(&value), sizeof(value));

    if (ret == -1) {
    int errno_copy = THRIFT_GET_SOCKET_ERROR;  // Copy
THRIFT_GET_SOCKET_ERROR because we're allocating memory.
    GlobalOutput.perror("TSocket::setKeepAlive() setsockopt(TCP_KEEPINTVL)
" + getSocketInfo(), errno_copy);
    }
    /**
    * Set TCP_KEEPCNT
    */
    value = 3;
    ret = setsockopt(socket_, SOL_TCP, TCP_KEEPCNT,
const_cast_sockopt(&value), sizeof(value));

    if (ret == -1) {
    int errno_copy = THRIFT_GET_SOCKET_ERROR;  // Copy
THRIFT_GET_SOCKET_ERROR because we're allocating memory.
    GlobalOutput.perror("TSocket::setKeepAlive() setsockopt(TCP_KEEPCNT) "
+ getSocketInfo(), errno_copy);
    }
  }
#endif

Hope this helps!

Regards,
Gajanan

On Wed, Mar 29, 2017 at 2:35 PM, bevis.peters@baesystems.com <
bevis.peters@baesystems.com> wrote:

> I am using apache thrift 0.9.3 in C++ with a TThreadPoolServer with a 15
> thread limit.
>
> I am running into a problem whereby a remote client may connect to my
> server, and so use up a thread, but then never disconnect. That connection
> thread on the server then remains "in use" forever. If this happens 15
> times, my server blocks up.
>
> Is there any way to tell the server to disconnect a client after a set
> time? This seems to be such an obvious problem, I feel there must be a
> solution which I've missed.
>
>
>
> Bevis
>
> Please consider the environment before printing this email. This message
> should be regarded as confidential. If you have received this email in
> error please notify the sender and destroy it immediately. Statements of
> intent shall only become binding when confirmed in hard copy by an
> authorised signatory. The contents of this email may relate to dealings
> with other companies under the control of BAE Systems Applied Intelligence
> Limited, details of which can be found at http://www.baesystems.com/Busi
> nesses/index.htm.
>