From a5c9498f48dac62dacf6a41ac0de764f6f94403a Mon Sep 17 00:00:00 2001 From: SamYuan1990 Date: Wed, 5 Mar 2025 20:55:18 +0800 Subject: [PATCH 1/6] nit fix for using const Signed-off-by: SamYuan1990 --- src/brpc/adaptive_max_concurrency.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/brpc/adaptive_max_concurrency.cpp b/src/brpc/adaptive_max_concurrency.cpp index ae11ceffcd..26aab1f89b 100644 --- a/src/brpc/adaptive_max_concurrency.cpp +++ b/src/brpc/adaptive_max_concurrency.cpp @@ -101,13 +101,13 @@ const std::string& AdaptiveMaxConcurrency::type() const { } const std::string& AdaptiveMaxConcurrency::UNLIMITED() { - static std::string* s = new std::string("unlimited"); - return *s; + static const std::string s = "unlimited"; + return s; } const std::string& AdaptiveMaxConcurrency::CONSTANT() { - static std::string* s = new std::string("constant"); - return *s; + static const std::string s = "constant"; + return s; } bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency, From 51469267d1c0e91e20dc0c4394448d3e2864c608 Mon Sep 17 00:00:00 2001 From: SamYuan1990 Date: Thu, 6 Mar 2025 08:14:00 +0800 Subject: [PATCH 2/6] as it just const string refactor Signed-off-by: SamYuan1990 --- src/brpc/adaptive_max_concurrency.cpp | 14 +++++++------- src/brpc/adaptive_max_concurrency.h | 6 ++++-- src/brpc/policy/constant_concurrency_limiter.cpp | 2 +- src/brpc/server.cpp | 4 ++-- src/brpc/server.h | 2 +- test/brpc_adaptive_class_unittest.cpp | 8 ++++---- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/brpc/adaptive_max_concurrency.cpp b/src/brpc/adaptive_max_concurrency.cpp index 26aab1f89b..70dd071994 100644 --- a/src/brpc/adaptive_max_concurrency.cpp +++ b/src/brpc/adaptive_max_concurrency.cpp @@ -25,14 +25,14 @@ namespace brpc { AdaptiveMaxConcurrency::AdaptiveMaxConcurrency() - : _value(UNLIMITED()) + : _value(this.UNLIMITED) , _max_concurrency(0) { } AdaptiveMaxConcurrency::AdaptiveMaxConcurrency(int max_concurrency) : _max_concurrency(0) { if (max_concurrency <= 0) { - _value = UNLIMITED(); + _value = this.UNLIMITED; _max_concurrency = 0; } else { _value = butil::string_printf("%d", max_concurrency); @@ -76,7 +76,7 @@ void AdaptiveMaxConcurrency::operator=(const butil::StringPiece& value) { void AdaptiveMaxConcurrency::operator=(int max_concurrency) { if (max_concurrency <= 0) { - _value = UNLIMITED(); + _value = this.UNLIMITED; _max_concurrency = 0; } else { _value = butil::string_printf("%d", max_concurrency); @@ -92,15 +92,15 @@ void AdaptiveMaxConcurrency::operator=(const TimeoutConcurrencyConf& value) { const std::string& AdaptiveMaxConcurrency::type() const { if (_max_concurrency > 0) { - return CONSTANT(); + return this.CONSTANT; } else if (_max_concurrency == 0) { - return UNLIMITED(); + return this.UNLIMITED; } else { return _value; } } -const std::string& AdaptiveMaxConcurrency::UNLIMITED() { +/*const std::string& AdaptiveMaxConcurrency::UNLIMITED() { static const std::string s = "unlimited"; return s; } @@ -108,7 +108,7 @@ const std::string& AdaptiveMaxConcurrency::UNLIMITED() { const std::string& AdaptiveMaxConcurrency::CONSTANT() { static const std::string s = "constant"; return s; -} +}*/ bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency, const butil::StringPiece& concurrency) { diff --git a/src/brpc/adaptive_max_concurrency.h b/src/brpc/adaptive_max_concurrency.h index 6bdad1ef1a..ea0a7448d1 100644 --- a/src/brpc/adaptive_max_concurrency.h +++ b/src/brpc/adaptive_max_concurrency.h @@ -65,8 +65,10 @@ class AdaptiveMaxConcurrency{ const std::string& type() const; // Get strings filled with "unlimited" and "constant" - static const std::string& UNLIMITED(); - static const std::string& CONSTANT(); + //static const std::string& UNLIMITED(); + //static const std::string& CONSTANT(); + static const std::string UNLIMITED = "unlimited"; + static const std::string CONSTANT = "constant"; private: std::string _value; diff --git a/src/brpc/policy/constant_concurrency_limiter.cpp b/src/brpc/policy/constant_concurrency_limiter.cpp index be5f071c34..48c5746527 100644 --- a/src/brpc/policy/constant_concurrency_limiter.cpp +++ b/src/brpc/policy/constant_concurrency_limiter.cpp @@ -37,7 +37,7 @@ int ConstantConcurrencyLimiter::MaxConcurrency() { ConstantConcurrencyLimiter* ConstantConcurrencyLimiter::New(const AdaptiveMaxConcurrency& amc) const { - CHECK_EQ(amc.type(), AdaptiveMaxConcurrency::CONSTANT()); + CHECK_EQ(amc.type(), AdaptiveMaxConcurrency.CONSTANT); return new ConstantConcurrencyLimiter(static_cast(amc)); } diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp index 2da703ef83..8c6dd258db 100644 --- a/src/brpc/server.cpp +++ b/src/brpc/server.cpp @@ -738,7 +738,7 @@ static int get_port_from_fd(int fd) { bool Server::CreateConcurrencyLimiter(const AdaptiveMaxConcurrency& amc, ConcurrencyLimiter** out) { - if (amc.type() == AdaptiveMaxConcurrency::UNLIMITED()) { + if (amc.type() == AdaptiveMaxConcurrency.UNLIMITED) { *out = NULL; return true; } @@ -1086,7 +1086,7 @@ int Server::StartInternal(const butil::EndPoint& endpoint, it->second.status->SetConcurrencyLimiter(NULL); } else { const AdaptiveMaxConcurrency* amc = &it->second.max_concurrency; - if (amc->type() == AdaptiveMaxConcurrency::UNLIMITED()) { + if (amc->type() == AdaptiveMaxConcurrency.UNLIMITED) { amc = &_options.method_max_concurrency; } ConcurrencyLimiter* cl = NULL; diff --git a/src/brpc/server.h b/src/brpc/server.h index 8d1b093cc7..6f3fd5988b 100644 --- a/src/brpc/server.h +++ b/src/brpc/server.h @@ -716,7 +716,7 @@ friend class Controller; int SetServiceMaxConcurrency(T* service) { if (NULL != service) { const AdaptiveMaxConcurrency* amc = &service->_max_concurrency; - if (amc->type() == AdaptiveMaxConcurrency::UNLIMITED()) { + if (amc->type() == AdaptiveMaxConcurrency.UNLIMITED) { amc = &_options.method_max_concurrency; } ConcurrencyLimiter* cl = NULL; diff --git a/test/brpc_adaptive_class_unittest.cpp b/test/brpc_adaptive_class_unittest.cpp index 18128f2a5e..f2fabc7c79 100644 --- a/test/brpc_adaptive_class_unittest.cpp +++ b/test/brpc_adaptive_class_unittest.cpp @@ -31,13 +31,13 @@ const std::string kPooled = "PoOled"; TEST(AdaptiveMaxConcurrencyTest, ShouldConvertCorrectly) { brpc::AdaptiveMaxConcurrency amc(0); - EXPECT_EQ(brpc::AdaptiveMaxConcurrency::UNLIMITED(), amc.type()); - EXPECT_EQ(brpc::AdaptiveMaxConcurrency::UNLIMITED(), amc.value()); + EXPECT_EQ(brpc::AdaptiveMaxConcurrency.UNLIMITED, amc.type()); + EXPECT_EQ(brpc::AdaptiveMaxConcurrency.UNLIMITED, amc.value()); EXPECT_EQ(0, int(amc)); - EXPECT_TRUE(amc == brpc::AdaptiveMaxConcurrency::UNLIMITED()); + EXPECT_TRUE(amc == brpc::AdaptiveMaxConcurrency.UNLIMITED); amc = 10; - EXPECT_EQ(brpc::AdaptiveMaxConcurrency::CONSTANT(), amc.type()); + EXPECT_EQ(brpc::AdaptiveMaxConcurrency.CONSTANT, amc.type()); EXPECT_EQ("10", amc.value()); EXPECT_EQ(10, int(amc)); EXPECT_EQ(amc, "10"); From 9d80d5d28b66c77e18898fc6616db5796ddc22ba Mon Sep 17 00:00:00 2001 From: SamYuan1990 Date: Thu, 6 Mar 2025 08:18:19 +0800 Subject: [PATCH 3/6] fix up Signed-off-by: SamYuan1990 --- src/brpc/adaptive_max_concurrency.cpp | 3 +++ src/brpc/adaptive_max_concurrency.h | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/brpc/adaptive_max_concurrency.cpp b/src/brpc/adaptive_max_concurrency.cpp index 70dd071994..54910feae6 100644 --- a/src/brpc/adaptive_max_concurrency.cpp +++ b/src/brpc/adaptive_max_concurrency.cpp @@ -24,6 +24,9 @@ namespace brpc { +const std::string AdaptiveMaxConcurrency::UNLIMITED = "unlimited"; +const std::string AdaptiveMaxConcurrency::CONSTANT = "constant"; + AdaptiveMaxConcurrency::AdaptiveMaxConcurrency() : _value(this.UNLIMITED) , _max_concurrency(0) { diff --git a/src/brpc/adaptive_max_concurrency.h b/src/brpc/adaptive_max_concurrency.h index ea0a7448d1..008d33dafd 100644 --- a/src/brpc/adaptive_max_concurrency.h +++ b/src/brpc/adaptive_max_concurrency.h @@ -67,8 +67,8 @@ class AdaptiveMaxConcurrency{ // Get strings filled with "unlimited" and "constant" //static const std::string& UNLIMITED(); //static const std::string& CONSTANT(); - static const std::string UNLIMITED = "unlimited"; - static const std::string CONSTANT = "constant"; + static const std::string UNLIMITED;// = "unlimited"; + static const std::string CONSTANT;// = "constant"; private: std::string _value; From 19d95ff7240a2ecc6034012b684eb939fc5e1e5c Mon Sep 17 00:00:00 2001 From: SamYuan1990 Date: Thu, 6 Mar 2025 08:24:59 +0800 Subject: [PATCH 4/6] fix up Signed-off-by: SamYuan1990 --- src/brpc/adaptive_max_concurrency.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/brpc/adaptive_max_concurrency.cpp b/src/brpc/adaptive_max_concurrency.cpp index 54910feae6..b817bc67c8 100644 --- a/src/brpc/adaptive_max_concurrency.cpp +++ b/src/brpc/adaptive_max_concurrency.cpp @@ -28,14 +28,14 @@ const std::string AdaptiveMaxConcurrency::UNLIMITED = "unlimited"; const std::string AdaptiveMaxConcurrency::CONSTANT = "constant"; AdaptiveMaxConcurrency::AdaptiveMaxConcurrency() - : _value(this.UNLIMITED) + : _value(UNLIMITED) , _max_concurrency(0) { } AdaptiveMaxConcurrency::AdaptiveMaxConcurrency(int max_concurrency) : _max_concurrency(0) { if (max_concurrency <= 0) { - _value = this.UNLIMITED; + _value = UNLIMITED; _max_concurrency = 0; } else { _value = butil::string_printf("%d", max_concurrency); @@ -79,7 +79,7 @@ void AdaptiveMaxConcurrency::operator=(const butil::StringPiece& value) { void AdaptiveMaxConcurrency::operator=(int max_concurrency) { if (max_concurrency <= 0) { - _value = this.UNLIMITED; + _value = UNLIMITED; _max_concurrency = 0; } else { _value = butil::string_printf("%d", max_concurrency); @@ -95,9 +95,9 @@ void AdaptiveMaxConcurrency::operator=(const TimeoutConcurrencyConf& value) { const std::string& AdaptiveMaxConcurrency::type() const { if (_max_concurrency > 0) { - return this.CONSTANT; + return CONSTANT; } else if (_max_concurrency == 0) { - return this.UNLIMITED; + return UNLIMITED; } else { return _value; } From 15630d6d28a064c936ca5b426ab1d9be794fbf54 Mon Sep 17 00:00:00 2001 From: SamYuan1990 Date: Thu, 6 Mar 2025 08:29:00 +0800 Subject: [PATCH 5/6] fix up Signed-off-by: SamYuan1990 --- src/brpc/policy/constant_concurrency_limiter.cpp | 2 +- src/brpc/server.cpp | 4 ++-- src/brpc/server.h | 2 +- test/brpc_adaptive_class_unittest.cpp | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/brpc/policy/constant_concurrency_limiter.cpp b/src/brpc/policy/constant_concurrency_limiter.cpp index 48c5746527..9a6711c2eb 100644 --- a/src/brpc/policy/constant_concurrency_limiter.cpp +++ b/src/brpc/policy/constant_concurrency_limiter.cpp @@ -37,7 +37,7 @@ int ConstantConcurrencyLimiter::MaxConcurrency() { ConstantConcurrencyLimiter* ConstantConcurrencyLimiter::New(const AdaptiveMaxConcurrency& amc) const { - CHECK_EQ(amc.type(), AdaptiveMaxConcurrency.CONSTANT); + CHECK_EQ(amc.type(), AdaptiveMaxConcurrency::CONSTANT); return new ConstantConcurrencyLimiter(static_cast(amc)); } diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp index 8c6dd258db..85ef4ce5d4 100644 --- a/src/brpc/server.cpp +++ b/src/brpc/server.cpp @@ -738,7 +738,7 @@ static int get_port_from_fd(int fd) { bool Server::CreateConcurrencyLimiter(const AdaptiveMaxConcurrency& amc, ConcurrencyLimiter** out) { - if (amc.type() == AdaptiveMaxConcurrency.UNLIMITED) { + if (amc.type() == AdaptiveMaxConcurrency::UNLIMITED) { *out = NULL; return true; } @@ -1086,7 +1086,7 @@ int Server::StartInternal(const butil::EndPoint& endpoint, it->second.status->SetConcurrencyLimiter(NULL); } else { const AdaptiveMaxConcurrency* amc = &it->second.max_concurrency; - if (amc->type() == AdaptiveMaxConcurrency.UNLIMITED) { + if (amc->type() == AdaptiveMaxConcurrency::UNLIMITED) { amc = &_options.method_max_concurrency; } ConcurrencyLimiter* cl = NULL; diff --git a/src/brpc/server.h b/src/brpc/server.h index 6f3fd5988b..2cf34dbd82 100644 --- a/src/brpc/server.h +++ b/src/brpc/server.h @@ -716,7 +716,7 @@ friend class Controller; int SetServiceMaxConcurrency(T* service) { if (NULL != service) { const AdaptiveMaxConcurrency* amc = &service->_max_concurrency; - if (amc->type() == AdaptiveMaxConcurrency.UNLIMITED) { + if (amc->type() == AdaptiveMaxConcurrency::UNLIMITED) { amc = &_options.method_max_concurrency; } ConcurrencyLimiter* cl = NULL; diff --git a/test/brpc_adaptive_class_unittest.cpp b/test/brpc_adaptive_class_unittest.cpp index f2fabc7c79..c0d76c0044 100644 --- a/test/brpc_adaptive_class_unittest.cpp +++ b/test/brpc_adaptive_class_unittest.cpp @@ -31,13 +31,13 @@ const std::string kPooled = "PoOled"; TEST(AdaptiveMaxConcurrencyTest, ShouldConvertCorrectly) { brpc::AdaptiveMaxConcurrency amc(0); - EXPECT_EQ(brpc::AdaptiveMaxConcurrency.UNLIMITED, amc.type()); - EXPECT_EQ(brpc::AdaptiveMaxConcurrency.UNLIMITED, amc.value()); + EXPECT_EQ(brpc::AdaptiveMaxConcurrency::UNLIMITED, amc.type()); + EXPECT_EQ(brpc::AdaptiveMaxConcurrency::UNLIMITED, amc.value()); EXPECT_EQ(0, int(amc)); - EXPECT_TRUE(amc == brpc::AdaptiveMaxConcurrency.UNLIMITED); + EXPECT_TRUE(amc == brpc::AdaptiveMaxConcurrency::UNLIMITED); amc = 10; - EXPECT_EQ(brpc::AdaptiveMaxConcurrency.CONSTANT, amc.type()); + EXPECT_EQ(brpc::AdaptiveMaxConcurrency::CONSTANT, amc.type()); EXPECT_EQ("10", amc.value()); EXPECT_EQ(10, int(amc)); EXPECT_EQ(amc, "10"); From 7e27e94279a8ee0fe73f09aa9de723f9c69922c7 Mon Sep 17 00:00:00 2001 From: SamYuan1990 Date: Thu, 6 Mar 2025 09:03:16 +0800 Subject: [PATCH 6/6] clean up Signed-off-by: SamYuan1990 --- src/brpc/adaptive_max_concurrency.cpp | 10 ---------- src/brpc/adaptive_max_concurrency.h | 2 -- 2 files changed, 12 deletions(-) diff --git a/src/brpc/adaptive_max_concurrency.cpp b/src/brpc/adaptive_max_concurrency.cpp index b817bc67c8..ac8975c7e3 100644 --- a/src/brpc/adaptive_max_concurrency.cpp +++ b/src/brpc/adaptive_max_concurrency.cpp @@ -103,16 +103,6 @@ const std::string& AdaptiveMaxConcurrency::type() const { } } -/*const std::string& AdaptiveMaxConcurrency::UNLIMITED() { - static const std::string s = "unlimited"; - return s; -} - -const std::string& AdaptiveMaxConcurrency::CONSTANT() { - static const std::string s = "constant"; - return s; -}*/ - bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency, const butil::StringPiece& concurrency) { return CompareStringPieceWithoutCase(concurrency, diff --git a/src/brpc/adaptive_max_concurrency.h b/src/brpc/adaptive_max_concurrency.h index 008d33dafd..89a52a362d 100644 --- a/src/brpc/adaptive_max_concurrency.h +++ b/src/brpc/adaptive_max_concurrency.h @@ -65,8 +65,6 @@ class AdaptiveMaxConcurrency{ const std::string& type() const; // Get strings filled with "unlimited" and "constant" - //static const std::string& UNLIMITED(); - //static const std::string& CONSTANT(); static const std::string UNLIMITED;// = "unlimited"; static const std::string CONSTANT;// = "constant";