Commit c9a2a08c authored by Ge Jun's avatar Ge Jun

Make methods in AdaptiveMaxConcurrency more reasonable

parent 81e346a2
...@@ -16,12 +16,29 @@ ...@@ -16,12 +16,29 @@
#include <cstring> #include <cstring>
#include <strings.h> #include <strings.h>
#include "butil/string_printf.h"
#include "butil/logging.h" #include "butil/logging.h"
#include "butil/strings/string_number_conversions.h" #include "butil/strings/string_number_conversions.h"
#include "brpc/adaptive_max_concurrency.h" #include "brpc/adaptive_max_concurrency.h"
namespace brpc { namespace brpc {
AdaptiveMaxConcurrency::AdaptiveMaxConcurrency()
: _value(UNLIMITED())
, _max_concurrency(0) {
}
AdaptiveMaxConcurrency::AdaptiveMaxConcurrency(int max_concurrency)
: _max_concurrency(0) {
if (max_concurrency <= 0) {
_value = UNLIMITED();
_max_concurrency = 0;
} else {
_value = butil::string_printf("%d", max_concurrency);
_max_concurrency = max_concurrency;
}
}
inline bool CompareStringPieceWithoutCase( inline bool CompareStringPieceWithoutCase(
const butil::StringPiece& s1, const char* s2) { const butil::StringPiece& s1, const char* s2) {
DCHECK(s2 != NULL); DCHECK(s2 != NULL);
...@@ -31,41 +48,61 @@ inline bool CompareStringPieceWithoutCase( ...@@ -31,41 +48,61 @@ inline bool CompareStringPieceWithoutCase(
return ::strncasecmp(s1.data(), s2, s1.size()) == 0; return ::strncasecmp(s1.data(), s2, s1.size()) == 0;
} }
AdaptiveMaxConcurrency::AdaptiveMaxConcurrency(const butil::StringPiece& name) { AdaptiveMaxConcurrency::AdaptiveMaxConcurrency(const butil::StringPiece& value)
if (butil::StringToInt(name, &_max_concurrency) && _max_concurrency >= 0) { : _max_concurrency(0) {
_name = "constant"; int max_concurrency = 0;
} else if (_max_concurrency < 0) { if (butil::StringToInt(value, &max_concurrency)) {
LOG(FATAL) << "Invalid max_concurrency: " << name; operator=(max_concurrency);
} else { } else {
_name.assign(name.begin(), name.end()); value.CopyToString(&_value);
_max_concurrency = 0; _max_concurrency = -1;
} }
} }
void AdaptiveMaxConcurrency::operator=(const butil::StringPiece& name) { void AdaptiveMaxConcurrency::operator=(const butil::StringPiece& value) {
int max_concurrency = 0; int max_concurrency = 0;
if (butil::StringToInt(name, &max_concurrency) && max_concurrency >= 0) { if (butil::StringToInt(value, &max_concurrency)) {
_name = "constant"; return operator=(max_concurrency);
_max_concurrency = max_concurrency;
} else if (max_concurrency < 0) {
LOG(ERROR) << "Fail to set max_concurrency, invalid value:" << name;
} else if (CompareStringPieceWithoutCase(name, "constant")) {
LOG(WARNING)
<< "If you want to use a constant maximum concurrency, assign "
<< "an integer value directly to ServerOptions.max_concurrency "
<< "like: `server_options.max_concurrency = 1000`";
_name.assign(name.begin(), name.end());
_max_concurrency = 0;
} else { } else {
_name.assign(name.begin(), name.end()); value.CopyToString(&_value);
_max_concurrency = -1;
}
}
void AdaptiveMaxConcurrency::operator=(int max_concurrency) {
if (max_concurrency <= 0) {
_value = UNLIMITED();
_max_concurrency = 0; _max_concurrency = 0;
} else {
_value = butil::string_printf("%d", max_concurrency);
_max_concurrency = max_concurrency;
}
}
const std::string& AdaptiveMaxConcurrency::type() const {
if (_max_concurrency > 0) {
return CONSTANT();
} else if (_max_concurrency == 0) {
return UNLIMITED();
} else {
return _value;
} }
} }
const std::string& AdaptiveMaxConcurrency::UNLIMITED() {
static std::string* s = new std::string("unlimited");
return *s;
}
const std::string& AdaptiveMaxConcurrency::CONSTANT() {
static std::string* s = new std::string("constant");
return *s;
}
bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency, bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency,
const butil::StringPiece& concurrency) { const butil::StringPiece& concurrency) {
return CompareStringPieceWithoutCase(concurrency, return CompareStringPieceWithoutCase(concurrency,
adaptive_concurrency.name().c_str()); adaptive_concurrency.value().c_str());
} }
} // namespace brpc } // namespace brpc
...@@ -27,37 +27,45 @@ namespace brpc { ...@@ -27,37 +27,45 @@ namespace brpc {
class AdaptiveMaxConcurrency{ class AdaptiveMaxConcurrency{
public: public:
AdaptiveMaxConcurrency() AdaptiveMaxConcurrency();
: _name("constant") AdaptiveMaxConcurrency(int max_concurrency);
, _max_concurrency(0) {} AdaptiveMaxConcurrency(const butil::StringPiece& value);
AdaptiveMaxConcurrency(int max_concurrency)
: _name("constant")
, _max_concurrency(max_concurrency) {}
// Non-trivial destructor to prevent AdaptiveMaxConcurrency from being // Non-trivial destructor to prevent AdaptiveMaxConcurrency from being
// passed to variadic arguments without explicit type conversion. // passed to variadic arguments without explicit type conversion.
// eg: // eg:
// printf("%d", options.max_concurrency) // compile error // printf("%d", options.max_concurrency) // compile error
// printf("%d", static_cast<int>(options.max_concurrency) // ok // printf("%s", options.max_concurrency.value().c_str()) // ok
~AdaptiveMaxConcurrency() {} ~AdaptiveMaxConcurrency() {}
AdaptiveMaxConcurrency(const butil::StringPiece& name); void operator=(int max_concurrency);
void operator=(const butil::StringPiece& value);
void operator=(int max_concurrency) {
_name = "constant";
_max_concurrency = max_concurrency;
}
void operator=(const butil::StringPiece& name);
// 0 for type="unlimited"
// >0 for type="constant"
// <0 for type="user-defined"
operator int() const { return _max_concurrency; } operator int() const { return _max_concurrency; }
const std::string& name() const { return _name; }
// "unlimited" for type="unlimited"
// "10" "20" "30" for type="constant"
// "user-defined" for type="user-defined"
const std::string& value() const { return _value; }
// "unlimited", "constant" or "user-defined"
const std::string& type() const;
// Get strings filled with "unlimited" and "constant"
static const std::string& UNLIMITED();
static const std::string& CONSTANT();
private: private:
std::string _name; std::string _value;
int _max_concurrency; int _max_concurrency;
}; };
inline std::ostream& operator<<(std::ostream& os, const AdaptiveMaxConcurrency& amc) {
return os << amc.value();
}
bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency, bool operator==(const AdaptiveMaxConcurrency& adaptive_concurrency,
const butil::StringPiece& concurrency); const butil::StringPiece& concurrency);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment