Skip to content

Commit

Permalink
Avoid throwing exceptions when setting socket option (apache#11329)
Browse files Browse the repository at this point in the history
  • Loading branch information
BewareMyPower authored Jul 15, 2021
1 parent 0584628 commit ac8194f
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions pulsar-client-cpp/lib/ClientConnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,37 @@ void ClientConnection::handleTcpConnected(const boost::system::error_code& err,
}
state_ = TcpConnected;
connectTimeoutTask_->stop();
socket_->set_option(tcp::no_delay(true));

socket_->set_option(tcp::socket::keep_alive(true));
boost::system::error_code error;
socket_->set_option(tcp::no_delay(true), error);
if (error) {
LOG_WARN(cnxString_ << "Socket failed to set tcp::no_delay: " << error.message());
}

socket_->set_option(tcp::socket::keep_alive(true), error);
if (error) {
LOG_WARN(cnxString_ << "Socket failed to set tcp::socket::keep_alive: " << error.message());
}

// Start TCP keep-alive probes after connection has been idle after 1 minute. Ideally this
// should never happen, given that we're sending our own keep-alive probes (within the TCP
// connection) every 30 seconds
socket_->set_option(tcp_keep_alive_idle(1 * 60));
socket_->set_option(tcp_keep_alive_idle(1 * 60), error);
if (error) {
LOG_DEBUG(cnxString_ << "Socket failed to set tcp_keep_alive_idle: " << error.message());
}

// Send up to 10 probes before declaring the connection broken
socket_->set_option(tcp_keep_alive_count(10));
socket_->set_option(tcp_keep_alive_count(10), error);
if (error) {
LOG_DEBUG(cnxString_ << "Socket failed to set tcp_keep_alive_count: " << error.message());
}

// Interval between probes: 6 seconds
socket_->set_option(tcp_keep_alive_interval(6));
socket_->set_option(tcp_keep_alive_interval(6), error);
if (error) {
LOG_DEBUG(cnxString_ << "Socket failed to set tcp_keep_alive_interval: " << error.message());
}

if (tlsSocket_) {
if (!isTlsAllowInsecureConnection_) {
Expand Down

0 comments on commit ac8194f

Please sign in to comment.