Added timeout handling for keep-alive operations.

This commit is contained in:
Denis Chaplygin 2019-08-19 16:15:56 +03:00
parent a0582a3e68
commit 22550d078f
2 changed files with 22 additions and 4 deletions

View File

@ -52,11 +52,15 @@ class Connection : public std::enable_shared_from_this<Connection>
/// Handle completion of a write operation. /// Handle completion of a write operation.
void handle_write(const boost::system::error_code &e); void handle_write(const boost::system::error_code &e);
/// Handle read timeout
void handle_timeout();
std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data, std::vector<char> compress_buffers(const std::vector<char> &uncompressed_data,
const http::compression_type compression_type); const http::compression_type compression_type);
boost::asio::io_service::strand strand; boost::asio::io_service::strand strand;
boost::asio::ip::tcp::socket TCP_socket; boost::asio::ip::tcp::socket TCP_socket;
boost::asio::deadline_timer timer;
RequestHandler &request_handler; RequestHandler &request_handler;
RequestParser request_parser; RequestParser request_parser;
boost::array<char, 8192> incoming_data_buffer; boost::array<char, 8192> incoming_data_buffer;
@ -68,6 +72,7 @@ class Connection : public std::enable_shared_from_this<Connection>
//Keep alive support //Keep alive support
bool keep_alive = false; bool keep_alive = false;
short processed_requests = 512; short processed_requests = 512;
short keepalive_timeout = 5; // In seconds
}; };
} }
} }

View File

@ -19,7 +19,7 @@ namespace server
{ {
Connection::Connection(boost::asio::io_service &io_service, RequestHandler &handler) Connection::Connection(boost::asio::io_service &io_service, RequestHandler &handler)
: strand(io_service), TCP_socket(io_service), request_handler(handler) : strand(io_service), TCP_socket(io_service), timer(io_service), request_handler(handler)
{ {
} }
@ -28,12 +28,19 @@ boost::asio::ip::tcp::socket &Connection::socket() { return TCP_socket; }
/// Start the first asynchronous operation for the connection. /// Start the first asynchronous operation for the connection.
void Connection::start() void Connection::start()
{ {
TCP_socket.async_read_some( TCP_socket.async_read_some(
boost::asio::buffer(incoming_data_buffer), boost::asio::buffer(incoming_data_buffer),
strand.wrap(boost::bind(&Connection::handle_read, strand.wrap(boost::bind(&Connection::handle_read,
this->shared_from_this(), this->shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred))); boost::asio::placeholders::bytes_transferred)));
//init async timer
timer.cancel();
timer.expires_from_now(boost::posix_time::seconds(keepalive_timeout));
timer.async_wait(boost::bind(&Connection::handle_timeout,
this->shared_from_this()));
} }
void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred) void Connection::handle_read(const boost::system::error_code &error, std::size_t bytes_transferred)
@ -133,12 +140,18 @@ void Connection::handle_write(const boost::system::error_code &error)
this->start(); this->start();
} else { } else {
// Initiate graceful connection closure. // Initiate graceful connection closure.
boost::system::error_code ignore_error; handle_timeout();
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
} }
} }
} }
/// Handle completion of a write operation.
void Connection::handle_timeout()
{
boost::system::error_code ignore_error;
TCP_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ignore_error);
}
std::vector<char> Connection::compress_buffers(const std::vector<char> &uncompressed_data, std::vector<char> Connection::compress_buffers(const std::vector<char> &uncompressed_data,
const http::compression_type compression_type) const http::compression_type compression_type)
{ {