add keepalive_timeout flag (#6674)

* add keepalive_timeout flag
This commit is contained in:
fenwuyaoji
2023-08-31 02:06:39 +08:00
committed by GitHub
parent d234c7246c
commit 14dcf91812
6 changed files with 48 additions and 15 deletions
+3 -1
View File
@@ -35,7 +35,9 @@ class RequestHandler;
class Connection : public std::enable_shared_from_this<Connection>
{
public:
explicit Connection(boost::asio::io_context &io_context, RequestHandler &handler);
explicit Connection(boost::asio::io_context &io_context,
RequestHandler &handler,
short keepalive_timeout);
Connection(const Connection &) = delete;
Connection &operator=(const Connection &) = delete;
+15 -7
View File
@@ -31,18 +31,24 @@ class Server
{
public:
// Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else
static std::shared_ptr<Server>
CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads)
static std::shared_ptr<Server> CreateServer(std::string &ip_address,
int ip_port,
unsigned requested_num_threads,
short keepalive_timeout)
{
util::Log() << "http 1.1 compression handled by zlib version " << zlibVersion();
const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency());
const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads);
return std::make_shared<Server>(ip_address, ip_port, real_num_threads);
return std::make_shared<Server>(ip_address, ip_port, real_num_threads, keepalive_timeout);
}
explicit Server(const std::string &address, const int port, const unsigned thread_pool_size)
: thread_pool_size(thread_pool_size), acceptor(io_context),
new_connection(std::make_shared<Connection>(io_context, request_handler))
explicit Server(const std::string &address,
const int port,
const unsigned thread_pool_size,
const short keepalive_timeout)
: thread_pool_size(thread_pool_size), keepalive_timeout(keepalive_timeout),
acceptor(io_context), new_connection(std::make_shared<Connection>(
io_context, request_handler, keepalive_timeout))
{
const auto port_string = std::to_string(port);
@@ -94,7 +100,8 @@ class Server
if (!e)
{
new_connection->start();
new_connection = std::make_shared<Connection>(io_context, request_handler);
new_connection =
std::make_shared<Connection>(io_context, request_handler, keepalive_timeout);
acceptor.async_accept(
new_connection->socket(),
boost::bind(&Server::HandleAccept, this, boost::asio::placeholders::error));
@@ -107,6 +114,7 @@ class Server
RequestHandler request_handler;
unsigned thread_pool_size;
short keepalive_timeout;
boost::asio::io_context io_context;
boost::asio::ip::tcp::acceptor acceptor;
std::shared_ptr<Connection> new_connection;