diff options
author | leonidlazarev <leonidlazarev@yandex-team.com> | 2023-08-02 11:23:31 +0300 |
---|---|---|
committer | leonidlazarev <leonidlazarev@yandex-team.com> | 2023-08-02 11:23:31 +0300 |
commit | 616e600cdea06a9aed80467aadaf10a7c680a074 (patch) | |
tree | 5bef9611a091249f6fa1683547db82c9805dd3be /contrib/libs | |
parent | f488cdc92db6276892f75bc57cd95bcbcce5aeb7 (diff) | |
download | ydb-616e600cdea06a9aed80467aadaf10a7c680a074.tar.gz |
grpc: allow limitation for number of threads via environment
Allow to set limitation for number of threads via environment GRPC_MAX_THREADS_THREAD_POOL_ENV
Diffstat (limited to 'contrib/libs')
-rw-r--r-- | contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc b/contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc index a0df463a71..4567086245 100644 --- a/contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc +++ b/contrib/libs/grpc/src/core/lib/event_engine/thread_pool.cc @@ -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; } |