Skip to content

Commit

Permalink
Set TCP keep alive values for builder queue correctly(hopefully)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSimpson committed Feb 18, 2018
1 parent 0d4ea28 commit 01616ab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 0 additions & 4 deletions Builder/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ int main(int argc, char *argv[]) {
acceptor.accept(client_stream.next_layer());
client_stream.accept();

// Use keep alive, which hopefully detect badly disconnected clients
boost::asio::socket_base::keep_alive keep_alive(true);
socket.set_option(keep_alive);

Logger::info("Setting the websocket stream to handle binary data and have an unlimited(uint64_t) message size");
client_stream.binary(true);
client_stream.read_message_max(0);
Expand Down
10 changes: 3 additions & 7 deletions BuilderQueue/include/Connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
public:
explicit Connection(tcp::socket socket, BuilderQueue &queue) : stream(std::move(socket)),
queue(queue) {
// Use keep alive, which hopefully detect badly disconnected clients
boost::asio::socket_base::keep_alive keep_alive(true);
boost::system::error_code ec;
socket.set_option(keep_alive, ec);
if(ec) {
Logger::error("Error setting keep alive on socket");
}
enable_keep_alive();
};

~Connection() {
Expand All @@ -40,6 +34,8 @@ class Connection : public std::enable_shared_from_this<Connection> {
beast::flat_buffer buffer;
boost::optional<BuilderData> builder;

void enable_keep_alive();

void read_request_string();

void request_builder();
Expand Down
22 changes: 22 additions & 0 deletions BuilderQueue/src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@

using namespace std::placeholders;

void Connection::enable_keep_alive() {
auto socket = stream.next_layer().native_handle();

// Enable keep alive
int enable = 1;
int rc = setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &enable, sizeof(enable));

// Set keep alive values
unsigned interval_milliseconds = 30000;
struct timeval tv;
tv.tv_sec = interval_milliseconds / 1000;
tv.tv_usec = interval_milliseconds % 1000;
int max_retry = 5;
rc |= setsockopt(socket, SOL_TCP, TCP_KEEPIDLE, &tv, sizeof(tv));
rc |= setsockopt(socket, SOL_TCP, TCP_KEEPINTVL, &tv, sizeof(tv));
rc |= setsockopt(socket, SOL_TCP, TCP_KEEPCNT, &max_retry, sizeof(max_retry));

if(rc != 0) {
Logger::error(std::string("Error setting keepalive: ") + strerror(errno));
}
}

void Connection::wait_for_close() {
// Persist this connection
auto self(shared_from_this());
Expand Down

0 comments on commit 01616ab

Please sign in to comment.