From 2607bf57912838ecd698dead3c338133c7ab8582 Mon Sep 17 00:00:00 2001 From: Maxime BELUGUET Date: Tue, 30 Nov 2021 19:22:07 +0100 Subject: [PATCH] Change += to ++, add queue length decrementation on error --- include/crow/http_connection.h | 2 +- include/crow/http_server.h | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index d3811ff66..d08bcdda6 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -407,7 +407,7 @@ namespace crow do_write_general(); } - queue_length -= 1; + queue_length--; } private: diff --git a/include/crow/http_server.h b/include/crow/http_server.h index ac3315bf1..2fd600995 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -182,7 +182,7 @@ namespace crow int min_queue_idx = 0; // TODO improve load balancing - for (uint i = 1; i < task_queue_length_pool_.size(); i += 1) + for (uint16_t i = 1; i < task_queue_length_pool_.size(); i++) { if (task_queue_length_pool_[i] < task_queue_length_pool_[min_queue_idx]) min_queue_idx = i; @@ -194,10 +194,12 @@ namespace crow { int service_idx = pick_io_service_idx(); asio::io_service& is = *io_service_pool_[service_idx]; + task_queue_length_pool_[service_idx]++; + auto p = new Connection( is, handler_, server_name_, middlewares_, get_cached_date_str_pool_[service_idx], *task_timer_pool_[service_idx], adaptor_ctx_, task_queue_length_pool_[service_idx]); - task_queue_length_pool_[service_idx] += 1; + acceptor_.async_accept( p->socket(), [this, p, &is](boost::system::error_code ec) { @@ -210,6 +212,7 @@ namespace crow } else { + task_queue_length_pool_[service_idx]--; delete p; } do_accept(); @@ -221,7 +224,7 @@ namespace crow std::vector> io_service_pool_; std::vector task_timer_pool_; std::vector> get_cached_date_str_pool_; - std::vector task_queue_length_pool_; + std::vector> task_queue_length_pool_; tcp::acceptor acceptor_; boost::asio::signal_set signals_; boost::asio::deadline_timer tick_timer_;