Commit 01616ab3 authored by AdamSimpson's avatar AdamSimpson
Browse files

Set TCP keep alive values for builder queue correctly(hopefully)

parent 0d4ea284
Loading
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -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);
+3 −7
Original line number Diff line number Diff line
@@ -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() {
@@ -40,6 +34,8 @@ private:
    beast::flat_buffer buffer;
    boost::optional<BuilderData> builder;

    void enable_keep_alive();

    void read_request_string();

    void request_builder();
+22 −0
Original line number Diff line number Diff line
@@ -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());