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: I90b9ece162babc4ccc23498eec6082d20fa6c461
Partial-Bug: #1461761
  • Loading branch information
Raj Reddy committed Jul 2, 2015
1 parent 357ed7b commit 36746a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/io/tcp_session.cc
Expand Up @@ -248,13 +248,15 @@ void TcpSession::Accepted() {

bool TcpSession::Connected(Endpoint remote) {
assert(refcount_);

{
tbb::mutex::scoped_lock lock(mutex_);
if (closed_) {
return false;
}
SessionEstablished(remote, TcpSession::ACTIVE);
}
SetSocketOptions();

TCP_SESSION_LOG_DEBUG(this, TCP_DIR_IN,
"Active session connection complete");
Expand Down Expand Up @@ -675,7 +677,7 @@ bool TcpSession::IsSocketErrorHard(const boost::system::error_code &ec) {
}

boost::system::error_code TcpSession::SetSocketKeepaliveOptions(int keepalive_time,
int keepalive_intvl, int keepalive_probes) {
int keepalive_intvl, int keepalive_probes, int tcp_user_timeout_val) {
boost::system::error_code ec;
socket_base::keep_alive keep_alive_option(true);
socket()->set_option(keep_alive_option, ec);
Expand Down Expand Up @@ -724,6 +726,17 @@ boost::system::error_code TcpSession::SetSocketKeepaliveOptions(int keepalive_ti
return ec;
}
#endif
#ifdef TCP_USER_TIMEOUT
typedef boost::asio::detail::socket_option::integer< IPPROTO_TCP, TCP_USER_TIMEOUT > tcp_user_timeout;
tcp_user_timeout tcp_user_timeout_option(tcp_user_timeout_val);
socket()->set_option(tcp_user_timeout_option, ec);
if (ec) {
TCP_SESSION_LOG_ERROR(this, TCP_DIR_OUT,
"tcp_user_timeout: " << tcp_user_timeout_val << " set error: " << ec);
return ec;
}
#endif

return ec;
}

Expand Down
2 changes: 1 addition & 1 deletion src/io/tcp_session.h
Expand Up @@ -186,7 +186,7 @@ class TcpSession {

EventObserver observer() { return observer_; }
boost::system::error_code SetSocketKeepaliveOptions(int keepalive_time,
int keepalive_intvl, int keepalive_probes);
int keepalive_intvl, int keepalive_probes, int tcp_user_timeout_val = 0);

void CloseInternal(bool call_observer, bool notify_server = true);

Expand Down

0 comments on commit 36746a1

Please sign in to comment.