Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic load balancing #289

Merged
merged 12 commits into from
Dec 7, 2021
Merged

Add basic load balancing #289

merged 12 commits into from
Dec 7, 2021

Conversation

belugum
Copy link
Contributor

@belugum belugum commented Nov 29, 2021

Count the number of requests in queue in each thread and use the thread with the smallest queue for new requests.
Related to issue #258, may fix issue #182.
This will avoid ignoring some requests when a thread is stuck.

@crow-clang-format
Copy link

--- include/crow/http_connection.h	(before formatting)
+++ include/crow/http_connection.h	(after formatting)
@@ -187,7 +187,7 @@
           std::function<std::string()>& get_cached_date_str_f,
           detail::task_timer& task_timer,
           typename Adaptor::context* adaptor_ctx_,
-          int &queue_length_):
+          int& queue_length_):
           adaptor_(io_service, adaptor_ctx_),
           handler_(handler),
           parser_(this),
@@ -705,7 +705,7 @@
 
         size_t res_stream_threshold_;
 
-        int &queue_length;
+        int& queue_length;
     };
 
 } // namespace crow
--- include/crow/http_server.h	(before formatting)
+++ include/crow/http_server.h	(after formatting)
@@ -196,7 +196,7 @@
             asio::io_service& is = *io_service_pool_[service_idx];
             auto p = new Connection<Adaptor, Handler, Middlewares...>(
               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]);
+              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(),

Copy link
Member

@The-EDev The-EDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for your work, I have a few points aside from the formatting. Please take a look at them.

One more thing I noticed is the fact that task_queue_length_pool_[service_idx] could be accessed / edited by multiple threads, meaning it would be wise to make them atomic.

include/crow/http_connection.h Outdated Show resolved Hide resolved
include/crow/http_server.h Outdated Show resolved Hide resolved
include/crow/http_server.h Outdated Show resolved Hide resolved
include/crow/http_server.h Outdated Show resolved Hide resolved
@CrowCpp CrowCpp deleted a comment from crow-clang-format bot Nov 30, 2021
Copy link
Member

@The-EDev The-EDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code seems okay, with the exception of the comment below and the unit tests failing to compile.

Please let me know if you need to restart CI

include/crow/http_server.h Outdated Show resolved Hide resolved
@belugum
Copy link
Contributor Author

belugum commented Nov 30, 2021

Yes, I wasn't familliar with atomics so it didn't compile. It's fixed now :)

@The-EDev
Copy link
Member

Seems alright, I'll run one final test myself to check that everything is good and approve/merge the PR.

Thanks again for the work!

Copy link
Member

@The-EDev The-EDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've checked the code once more and found a couple of interesting things (Along with a significant fault of my own that I need to fix ASAP)

include/crow/http_connection.h Outdated Show resolved Hide resolved
include/crow/http_connection.h Outdated Show resolved Hide resolved
include/crow/http_connection.h Outdated Show resolved Hide resolved
include/crow/http_connection.h Outdated Show resolved Hide resolved
include/crow/http_server.h Outdated Show resolved Hide resolved
Copy link
Member

@The-EDev The-EDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic is sound and works fine as far as I can tell. I would just like to add a couple things to tie up any potential loose ends. What do you think of them @belugum? I promise this is the last review (I wanted to be excessively thorough because this is a vital part of the framework)

include/crow/http_connection.h Outdated Show resolved Hide resolved
include/crow/http_server.h Outdated Show resolved Hide resolved
include/crow/http_server.h Outdated Show resolved Hide resolved
include/crow/http_server.h Show resolved Hide resolved
include/crow/http_server.h Show resolved Hide resolved
Add debug verbosity

Co-authored-by: Farook Al-Sammarraie <[email protected]>
@crow-clang-format
Copy link

--- include/crow/http_server.h	(before formatting)
+++ include/crow/http_server.h	(after formatting)
@@ -195,7 +195,7 @@
             uint16_t service_idx = pick_io_service_idx();
             asio::io_service& is = *io_service_pool_[service_idx];
             task_queue_length_pool_[service_idx]++;
-            CROW_LOG_DEBUG << &is << " {" <<  service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
+            CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
 
             auto p = new Connection<Adaptor, Handler, Middlewares...>(
               is, handler_, server_name_, middlewares_,
@@ -214,7 +214,7 @@
                   else
                   {
                       task_queue_length_pool_[service_idx]--;
-                      CROW_LOG_DEBUG << &is << " {" <<  service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
+                      CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
                       delete p;
                   }
                   do_accept();

@belugum
Copy link
Contributor Author

belugum commented Dec 7, 2021

Looks good. I applied your suggestions, thanks!

@crow-clang-format
Copy link

--- include/crow/http_server.h	(before formatting)
+++ include/crow/http_server.h	(after formatting)
@@ -196,7 +196,7 @@
             uint16_t service_idx = pick_io_service_idx();
             asio::io_service& is = *io_service_pool_[service_idx];
             task_queue_length_pool_[service_idx]++;
-            CROW_LOG_DEBUG << &is << " {" <<  service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
+            CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
 
             auto p = new Connection<Adaptor, Handler, Middlewares...>(
               is, handler_, server_name_, middlewares_,
@@ -215,7 +215,7 @@
                   else
                   {
                       task_queue_length_pool_[service_idx]--;
-                      CROW_LOG_DEBUG << &is << " {" <<  service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
+                      CROW_LOG_DEBUG << &is << " {" << service_idx << "} queue length: " << task_queue_length_pool_[service_idx];
                       delete p;
                   }
                   do_accept();

@CrowCpp CrowCpp deleted a comment from crow-clang-format bot Dec 7, 2021
@CrowCpp CrowCpp deleted a comment from crow-clang-format bot Dec 7, 2021
Copy link
Member

@The-EDev The-EDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good, Thank you very much for your work!

@The-EDev The-EDev merged commit 5037891 into CrowCpp:master Dec 7, 2021
@belugum
Copy link
Contributor Author

belugum commented Dec 8, 2021

No problem, thanks for merging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants