nghttpx: Convert shrpx_session_affinity to enum class

This commit is contained in:
Tatsuhiro Tsujikawa 2018-10-16 22:25:42 +09:00
parent 20ea964f2f
commit 5e4f434fd8
6 changed files with 32 additions and 29 deletions

View File

@ -660,7 +660,7 @@ void ClientHandler::pool_downstream_connection(
auto &shared_addr = group->shared_addr; auto &shared_addr = group->shared_addr;
if (shared_addr->affinity.type == AFFINITY_NONE) { if (shared_addr->affinity.type == SessionAffinity::NONE) {
auto &dconn_pool = group->shared_addr->dconn_pool; auto &dconn_pool = group->shared_addr->dconn_pool;
dconn_pool.add_downstream_connection(std::move(dconn)); dconn_pool.add_downstream_connection(std::move(dconn));
@ -1003,17 +1003,17 @@ ClientHandler::get_downstream_connection(int &err, Downstream *downstream,
auto &group = groups[group_idx]; auto &group = groups[group_idx];
auto &shared_addr = group->shared_addr; auto &shared_addr = group->shared_addr;
if (shared_addr->affinity.type != AFFINITY_NONE) { if (shared_addr->affinity.type != SessionAffinity::NONE) {
uint32_t hash; uint32_t hash;
switch (shared_addr->affinity.type) { switch (shared_addr->affinity.type) {
case AFFINITY_IP: case SessionAffinity::IP:
if (!affinity_hash_computed_) { if (!affinity_hash_computed_) {
affinity_hash_ = compute_affinity_from_ip(ipaddr_); affinity_hash_ = compute_affinity_from_ip(ipaddr_);
affinity_hash_computed_ = true; affinity_hash_computed_ = true;
} }
hash = affinity_hash_; hash = affinity_hash_;
break; break;
case AFFINITY_COOKIE: case SessionAffinity::COOKIE:
hash = get_affinity_cookie(downstream, shared_addr->affinity.cookie.name); hash = get_affinity_cookie(downstream, shared_addr->affinity.cookie.name);
break; break;
default: default:

View File

@ -903,11 +903,11 @@ int parse_downstream_params(DownstreamParams &out,
} else if (util::istarts_with_l(param, "affinity=")) { } else if (util::istarts_with_l(param, "affinity=")) {
auto valstr = StringRef{first + str_size("affinity="), end}; auto valstr = StringRef{first + str_size("affinity="), end};
if (util::strieq_l("none", valstr)) { if (util::strieq_l("none", valstr)) {
out.affinity.type = AFFINITY_NONE; out.affinity.type = SessionAffinity::NONE;
} else if (util::strieq_l("ip", valstr)) { } else if (util::strieq_l("ip", valstr)) {
out.affinity.type = AFFINITY_IP; out.affinity.type = SessionAffinity::IP;
} else if (util::strieq_l("cookie", valstr)) { } else if (util::strieq_l("cookie", valstr)) {
out.affinity.type = AFFINITY_COOKIE; out.affinity.type = SessionAffinity::COOKIE;
} else { } else {
LOG(ERROR) LOG(ERROR)
<< "backend: affinity: value must be one of none, ip, and cookie"; << "backend: affinity: value must be one of none, ip, and cookie";
@ -1004,7 +1004,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr,
return -1; return -1;
} }
if (params.affinity.type == AFFINITY_COOKIE && if (params.affinity.type == SessionAffinity::COOKIE &&
params.affinity.cookie.name.empty()) { params.affinity.cookie.name.empty()) {
LOG(ERROR) << "backend: affinity-cookie-name is mandatory if " LOG(ERROR) << "backend: affinity-cookie-name is mandatory if "
"affinity=cookie is specified"; "affinity=cookie is specified";
@ -1056,10 +1056,10 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr,
auto &g = addr_groups[(*it).second]; auto &g = addr_groups[(*it).second];
// Last value wins if we have multiple different affinity // Last value wins if we have multiple different affinity
// value under one group. // value under one group.
if (params.affinity.type != AFFINITY_NONE) { if (params.affinity.type != SessionAffinity::NONE) {
if (g.affinity.type == AFFINITY_NONE) { if (g.affinity.type == SessionAffinity::NONE) {
g.affinity.type = params.affinity.type; g.affinity.type = params.affinity.type;
if (params.affinity.type == AFFINITY_COOKIE) { if (params.affinity.type == SessionAffinity::COOKIE) {
g.affinity.cookie.name = make_string_ref( g.affinity.cookie.name = make_string_ref(
downstreamconf.balloc, params.affinity.cookie.name); downstreamconf.balloc, params.affinity.cookie.name);
if (!params.affinity.cookie.path.empty()) { if (!params.affinity.cookie.path.empty()) {
@ -1129,7 +1129,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr,
auto &g = addr_groups.back(); auto &g = addr_groups.back();
g.addrs.push_back(addr); g.addrs.push_back(addr);
g.affinity.type = params.affinity.type; g.affinity.type = params.affinity.type;
if (params.affinity.type == AFFINITY_COOKIE) { if (params.affinity.type == SessionAffinity::COOKIE) {
g.affinity.cookie.name = g.affinity.cookie.name =
make_string_ref(downstreamconf.balloc, params.affinity.cookie.name); make_string_ref(downstreamconf.balloc, params.affinity.cookie.name);
if (!params.affinity.cookie.path.empty()) { if (!params.affinity.cookie.path.empty()) {
@ -4076,7 +4076,7 @@ int configure_downstream_group(Config *config, bool http2_proxy,
} }
} }
if (g.affinity.type != AFFINITY_NONE) { if (g.affinity.type != SessionAffinity::NONE) {
size_t idx = 0; size_t idx = 0;
for (auto &addr : g.addrs) { for (auto &addr : g.addrs) {
StringRef key; StringRef key;

View File

@ -369,13 +369,13 @@ enum class Proto {
MEMCACHED, MEMCACHED,
}; };
enum shrpx_session_affinity { enum class SessionAffinity {
// No session affinity // No session affinity
AFFINITY_NONE, NONE,
// Client IP affinity // Client IP affinity
AFFINITY_IP, IP,
// Cookie based affinity // Cookie based affinity
AFFINITY_COOKIE, COOKIE,
}; };
enum shrpx_cookie_secure { enum shrpx_cookie_secure {
@ -390,7 +390,7 @@ enum shrpx_cookie_secure {
struct AffinityConfig { struct AffinityConfig {
// Type of session affinity. // Type of session affinity.
shrpx_session_affinity type; SessionAffinity type;
struct { struct {
// Name of a cookie to use. // Name of a cookie to use.
StringRef name; StringRef name;
@ -496,13 +496,15 @@ struct AffinityHash {
struct DownstreamAddrGroupConfig { struct DownstreamAddrGroupConfig {
DownstreamAddrGroupConfig(const StringRef &pattern) DownstreamAddrGroupConfig(const StringRef &pattern)
: pattern(pattern), affinity{AFFINITY_NONE}, redirect_if_not_tls(false) {} : pattern(pattern),
affinity{SessionAffinity::NONE},
redirect_if_not_tls(false) {}
StringRef pattern; StringRef pattern;
StringRef mruby_file; StringRef mruby_file;
std::vector<DownstreamAddrConfig> addrs; std::vector<DownstreamAddrConfig> addrs;
// Bunch of session affinity hash. Only used if affinity == // Bunch of session affinity hash. Only used if affinity ==
// AFFINITY_IP. // SessionAffinity::IP.
std::vector<AffinityHash> affinity_hash; std::vector<AffinityHash> affinity_hash;
// Cookie based session affinity configuration. // Cookie based session affinity configuration.
AffinityConfig affinity; AffinityConfig affinity;

View File

@ -259,7 +259,7 @@ int HttpDownstreamConnection::initiate_connection() {
// initial_addr_idx_. // initial_addr_idx_.
size_t temp_idx = initial_addr_idx_; size_t temp_idx = initial_addr_idx_;
auto &next_downstream = shared_addr->affinity.type == AFFINITY_NONE auto &next_downstream = shared_addr->affinity.type == SessionAffinity::NONE
? shared_addr->next ? shared_addr->next
: temp_idx; : temp_idx;
auto end = next_downstream; auto end = next_downstream;
@ -274,7 +274,7 @@ int HttpDownstreamConnection::initiate_connection() {
assert(addr->dns); assert(addr->dns);
} else { } else {
assert(addr_ == nullptr); assert(addr_ == nullptr);
if (shared_addr->affinity.type == AFFINITY_NONE) { if (shared_addr->affinity.type == SessionAffinity::NONE) {
addr = &addrs[next_downstream]; addr = &addrs[next_downstream];
if (++next_downstream >= addrs.size()) { if (++next_downstream >= addrs.size()) {
next_downstream = 0; next_downstream = 0;
@ -810,7 +810,7 @@ void remove_from_pool(HttpDownstreamConnection *dconn) {
auto &group = dconn->get_downstream_addr_group(); auto &group = dconn->get_downstream_addr_group();
auto &shared_addr = group->shared_addr; auto &shared_addr = group->shared_addr;
if (shared_addr->affinity.type == AFFINITY_NONE) { if (shared_addr->affinity.type == SessionAffinity::NONE) {
auto &dconn_pool = auto &dconn_pool =
dconn->get_downstream_addr_group()->shared_addr->dconn_pool; dconn->get_downstream_addr_group()->shared_addr->dconn_pool;
dconn_pool.remove_downstream_connection(dconn); dconn_pool.remove_downstream_connection(dconn);

View File

@ -77,7 +77,8 @@ DownstreamAddrGroup::~DownstreamAddrGroup() {}
using DownstreamKey = using DownstreamKey =
std::tuple<std::vector<std::tuple<StringRef, StringRef, size_t, size_t, std::tuple<std::vector<std::tuple<StringRef, StringRef, size_t, size_t,
Proto, uint16_t, bool, bool, bool, bool>>, Proto, uint16_t, bool, bool, bool, bool>>,
bool, int, StringRef, StringRef, int, int64_t, int64_t>; bool, SessionAffinity, StringRef, StringRef, int, int64_t,
int64_t>;
namespace { namespace {
DownstreamKey create_downstream_key( DownstreamKey create_downstream_key(
@ -164,7 +165,7 @@ void Worker::replace_downstream_config(
auto &shared_addr = g->shared_addr; auto &shared_addr = g->shared_addr;
if (shared_addr->affinity.type == AFFINITY_NONE) { if (shared_addr->affinity.type == SessionAffinity::NONE) {
shared_addr->dconn_pool.remove_all(); shared_addr->dconn_pool.remove_all();
continue; continue;
} }
@ -213,7 +214,7 @@ void Worker::replace_downstream_config(
shared_addr->addrs.resize(src.addrs.size()); shared_addr->addrs.resize(src.addrs.size());
shared_addr->affinity.type = src.affinity.type; shared_addr->affinity.type = src.affinity.type;
if (src.affinity.type == AFFINITY_COOKIE) { if (src.affinity.type == SessionAffinity::COOKIE) {
shared_addr->affinity.cookie.name = shared_addr->affinity.cookie.name =
make_string_ref(shared_addr->balloc, src.affinity.cookie.name); make_string_ref(shared_addr->balloc, src.affinity.cookie.name);
if (!src.affinity.cookie.path.empty()) { if (!src.affinity.cookie.path.empty()) {
@ -303,7 +304,7 @@ void Worker::replace_downstream_config(
shared_addr->http1_pri.weight = num_http1; shared_addr->http1_pri.weight = num_http1;
shared_addr->http2_pri.weight = num_http2; shared_addr->http2_pri.weight = num_http2;
if (shared_addr->affinity.type != AFFINITY_NONE) { if (shared_addr->affinity.type != SessionAffinity::NONE) {
for (auto &addr : shared_addr->addrs) { for (auto &addr : shared_addr->addrs) {
addr.dconn_pool = std::make_unique<DownstreamConnectionPool>(); addr.dconn_pool = std::make_unique<DownstreamConnectionPool>();
} }

View File

@ -137,7 +137,7 @@ struct WeightedPri {
struct SharedDownstreamAddr { struct SharedDownstreamAddr {
SharedDownstreamAddr() SharedDownstreamAddr()
: balloc(1024, 1024), : balloc(1024, 1024),
affinity{AFFINITY_NONE}, affinity{SessionAffinity::NONE},
next{0}, next{0},
http1_pri{}, http1_pri{},
http2_pri{}, http2_pri{},
@ -151,7 +151,7 @@ struct SharedDownstreamAddr {
BlockAllocator balloc; BlockAllocator balloc;
std::vector<DownstreamAddr> addrs; std::vector<DownstreamAddr> addrs;
// Bunch of session affinity hash. Only used if affinity == // Bunch of session affinity hash. Only used if affinity ==
// AFFINITY_IP. // SessionAffinity::IP.
std::vector<AffinityHash> affinity_hash; std::vector<AffinityHash> affinity_hash;
// List of Http2Session which is not fully utilized (i.e., the // List of Http2Session which is not fully utilized (i.e., the
// server advertised maximum concurrency is not reached). We will // server advertised maximum concurrency is not reached). We will