aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/grpc/patches/28-limit-thread-pools-via-env.patch
blob: ec4429a63284e5f21ed7c94180102283fd0f36d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--- contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc	(1c9f99d6c38355a4a99b04e59c154a9ae9cb2d58)
+++ contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc	(working tree)
@@ -33,7 +33,8 @@
 #include "src/core/lib/event_engine/thread_local.h"
 #include "src/core/lib/gprpp/thd.h"
 #include "src/core/lib/gprpp/time.h"
-
+#include "src/core/lib/gprpp/env.h"
+#include "src/core/lib/gpr/string.h"
 namespace {
     size_t threads_limit_ = 0;
 }
@@ -41,6 +42,17 @@ namespace {
 namespace grpc_event_engine {
 namespace experimental {
 
+size_t GetMaxPoolThreadFromEnv() {
+  auto value = grpc_core::GetEnv("GRPC_MAX_THREADS_THREAD_POOL_ENV");
+  if (!value.has_value()) return 0;
+  int parse_succeeded = gpr_parse_nonnegative_int(value->c_str());
+
+  if (parse_succeeded <= 0) {
+      return 0;
+  }
+  return static_cast<size_t>(parse_succeeded);
+}
+
 size_t ThreadPool::SetThreadsLimit(size_t count) {
     size_t prev = threads_limit_;
     threads_limit_ = count;
@@ -50,11 +62,19 @@ size_t ThreadPool::SetThreadsLimit(size_t count) {
 unsigned ThreadPool::GetMaxSystemThread() {
     unsigned max_threads = grpc_core::Clamp(gpr_cpu_num_cores(), 2u, 32u);
 
+    auto threads_limit_env = GetMaxPoolThreadFromEnv();
+    if (threads_limit_env) {
+        unsigned new_max_threads = std::min(max_threads, static_cast<unsigned>(threads_limit_env));
+        gpr_log(GPR_INFO, "Threads limit changed via env from %u to %u", max_threads, new_max_threads);
+        max_threads = new_max_threads;
+    }
+
     if (threads_limit_) {
         unsigned new_max_threads = std::min(max_threads, static_cast<unsigned>(threads_limit_));
         gpr_log(GPR_INFO, "Threads limit changed from %u to %u", max_threads, new_max_threads);
         max_threads = new_max_threads;
     }
+    gpr_log(GPR_INFO, "Threads limit:[%u]", max_threads);
     return max_threads;
 }