Skip to content

Commit

Permalink
When the socket has data to send, then the TCP_KEEPALIVE timer doesn'…
Browse files Browse the repository at this point in the history
…t get

activated and it takes much longer time before the socket is closed.
TCP_USER_TIMEOUT also needs to be set for the keepalive to function
when there's traffic on the tcp socket. This will cause the socket
to close when the remote end is not reachable.

Change-Id: I14c45d05992c604691f2a4c1e7ddb4c741f8fc99
Closes-Bug: #1461761
  • Loading branch information
Raj Reddy committed Jul 4, 2015
1 parent a826c88 commit 40fdaf3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion library/cpp/sandesh_session.cc
Expand Up @@ -287,6 +287,7 @@ SandeshSession::SandeshSession(TcpServer *client, Socket *socket,
keepalive_idle_time_(kSessionKeepaliveIdleTime),
keepalive_interval_(kSessionKeepaliveInterval),
keepalive_probes_(kSessionKeepaliveProbes),
tcp_user_timeout_(kSessionTcpUserTimeout),
reader_task_id_(reader_task_id) {
if (Sandesh::role() == Sandesh::SandeshRole::Collector) {
send_buffer_queue_.reset(new Sandesh::SandeshBufferQueue(writer_task_id,
Expand Down Expand Up @@ -340,7 +341,7 @@ boost::system::error_code SandeshSession::SetSocketOptions() {
return ec;
}
return SetSocketKeepaliveOptions(keepalive_idle_time_, keepalive_interval_,
keepalive_probes_);
keepalive_probes_, tcp_user_timeout_);
}

void SandeshSession::OnRead(Buffer buffer) {
Expand Down
4 changes: 3 additions & 1 deletion library/cpp/sandesh_session.h
Expand Up @@ -219,9 +219,10 @@ class SandeshSession : public TcpSession {
friend class SandeshSessionTest;

// 60 seconds - 45s + (3*5)s
static const int kSessionKeepaliveIdleTime = 45; // in seconds
static const int kSessionKeepaliveIdleTime = 15; // in seconds
static const int kSessionKeepaliveInterval = 3; // in seconds
static const int kSessionKeepaliveProbes = 5; // count
static const int kSessionTcpUserTimeout = 30000; // ms

bool SendMsg(Sandesh *sandesh);
bool SendBuffer(boost::shared_ptr<TMemoryBuffer> sbuffer);
Expand All @@ -238,6 +239,7 @@ class SandeshSession : public TcpSession {
int keepalive_idle_time_;
int keepalive_interval_;
int keepalive_probes_;
int tcp_user_timeout_;
int reader_task_id_;

// Session statistics
Expand Down
10 changes: 9 additions & 1 deletion library/python/pysandesh/sandesh_session.py
Expand Up @@ -228,9 +228,11 @@ def _send(self, send_buf):


class SandeshSession(TcpSession):
_KEEPALIVE_IDLE_TIME = 45 # in secs
_KEEPALIVE_IDLE_TIME = 15 # in secs
_KEEPALIVE_INTERVAL = 3 # in secs
_KEEPALIVE_PROBES = 5
_TCP_USER_TIMEOUT_OPT = 18
_TCP_USER_TIMEOUT_VAL = 30000 # ms

def __init__(self, sandesh_instance, server, event_handler,
sandesh_msg_handler):
Expand Down Expand Up @@ -299,6 +301,12 @@ def _set_socket_options(self):
if hasattr(socket, 'TCP_KEEPCNT'):
self._socket.setsockopt(
socket.IPPROTO_TCP, socket.TCP_KEEPCNT, self._KEEPALIVE_PROBES)
try:
self._socket.setsockopt(socket.IPPROTO_TCP,
self._TCP_USER_TIMEOUT_OPT, self._TCP_USER_TIMEOUT_VAL)
except:
self._logger.error('setsockopt failed: option %d, value %d' %
(self._TCP_USER_TIMEOUT_OPT, self._TCP_USER_TIMEOUT_VAL))
# end _set_socket_options

# Private functions
Expand Down

0 comments on commit 40fdaf3

Please sign in to comment.