Skip to content

Commit aef1471

Browse files
ino-joshgittiver
authored andcommitted
Fix "load balancing" not working
Before this commit, the load balancing does not work. The foundation is there, but in practice, the next connection is always picked. For example, if there are 3 contexts available, it will always go: 0, 1, 2, 0, 1, 2, 0, 1, 2, etc. If 0 is a long running connection, and 1 and 2 are short, it will pick 0 for the next connection and then block waiting for that long running connection to end even though 1 and 2 are free. With this fix, the above example would result in this sequence: 0, 1, 2, 1, 2, 1, 2, 1, 2 (until 0 is done with its long running connection).
1 parent bcca011 commit aef1471

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

include/crow/http_connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ namespace crow
6969
res_stream_threshold_(handler->stream_threshold()),
7070
queue_length_(queue_length)
7171
{
72+
queue_length_++;
7273
#ifdef CROW_ENABLE_DEBUG
7374
connectionCount++;
7475
CROW_LOG_DEBUG << "Connection (" << this << ") allocated, total: " << connectionCount;
@@ -77,6 +78,7 @@ namespace crow
7778

7879
~Connection()
7980
{
81+
queue_length_--;
8082
#ifdef CROW_ENABLE_DEBUG
8183
connectionCount--;
8284
CROW_LOG_DEBUG << "Connection (" << this << ") freed, total: " << connectionCount;

include/crow/http_server.h

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ namespace crow // NOTE: Already documented in "crow/app.h"
4949
uint16_t concurrency = 1,
5050
uint8_t timeout = 5,
5151
typename Adaptor::context* adaptor_ctx = nullptr):
52+
concurrency_(concurrency),
53+
task_queue_length_pool_(concurrency_ - 1),
5254
acceptor_(io_context_),
5355
signals_(io_context_),
5456
tick_timer_(io_context_),
5557
handler_(handler),
56-
concurrency_(concurrency),
5758
timeout_(timeout),
5859
server_name_(server_name),
59-
task_queue_length_pool_(concurrency_ - 1),
6060
middlewares_(middlewares),
6161
adaptor_ctx_(adaptor_ctx)
6262
{
@@ -300,12 +300,11 @@ namespace crow // NOTE: Already documented in "crow/app.h"
300300
{
301301
uint16_t context_idx = pick_io_context_idx();
302302
asio::io_context& ic = *io_context_pool_[context_idx];
303-
task_queue_length_pool_[context_idx]++;
304-
CROW_LOG_DEBUG << &ic << " {" << context_idx << "} queue length: " << task_queue_length_pool_[context_idx];
305-
306303
auto p = std::make_shared<Connection<Adaptor, Handler, Middlewares...>>(
307-
ic, handler_, server_name_, middlewares_,
308-
get_cached_date_str_pool_[context_idx], *task_timer_pool_[context_idx], adaptor_ctx_, task_queue_length_pool_[context_idx]);
304+
ic, handler_, server_name_, middlewares_,
305+
get_cached_date_str_pool_[context_idx], *task_timer_pool_[context_idx], adaptor_ctx_, task_queue_length_pool_[context_idx]);
306+
307+
CROW_LOG_DEBUG << &ic << " {" << context_idx << "} queue length: " << task_queue_length_pool_[context_idx];
309308

310309
acceptor_.async_accept(
311310
p->socket(),
@@ -317,11 +316,6 @@ namespace crow // NOTE: Already documented in "crow/app.h"
317316
p->start();
318317
});
319318
}
320-
else
321-
{
322-
task_queue_length_pool_[context_idx]--;
323-
CROW_LOG_DEBUG << &ic << " {" << context_idx << "} queue length: " << task_queue_length_pool_[context_idx];
324-
}
325319
do_accept();
326320
});
327321
}
@@ -336,6 +330,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
336330
}
337331

338332
private:
333+
uint16_t concurrency_{2};
334+
std::vector<std::atomic<unsigned int>> task_queue_length_pool_;
339335
std::vector<std::unique_ptr<asio::io_context>> io_context_pool_;
340336
asio::io_context io_context_;
341337
std::vector<detail::task_timer*> task_timer_pool_;
@@ -351,10 +347,8 @@ namespace crow // NOTE: Already documented in "crow/app.h"
351347
asio::basic_waitable_timer<std::chrono::high_resolution_clock> tick_timer_;
352348

353349
Handler* handler_;
354-
uint16_t concurrency_{2};
355350
std::uint8_t timeout_;
356351
std::string server_name_;
357-
std::vector<std::atomic<unsigned int>> task_queue_length_pool_;
358352

359353
std::chrono::milliseconds tick_interval_;
360354
std::function<void()> tick_function_;

0 commit comments

Comments
 (0)