diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index 9b3ff083..a32c8e93 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -660,7 +660,7 @@ void ClientHandler::pool_downstream_connection( 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; 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 &shared_addr = group->shared_addr; - if (shared_addr->affinity.type != AFFINITY_NONE) { + if (shared_addr->affinity.type != SessionAffinity::NONE) { uint32_t hash; switch (shared_addr->affinity.type) { - case AFFINITY_IP: + case SessionAffinity::IP: if (!affinity_hash_computed_) { affinity_hash_ = compute_affinity_from_ip(ipaddr_); affinity_hash_computed_ = true; } hash = affinity_hash_; break; - case AFFINITY_COOKIE: + case SessionAffinity::COOKIE: hash = get_affinity_cookie(downstream, shared_addr->affinity.cookie.name); break; default: diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 76ffc55d..0b2ee031 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -903,11 +903,11 @@ int parse_downstream_params(DownstreamParams &out, } else if (util::istarts_with_l(param, "affinity=")) { auto valstr = StringRef{first + str_size("affinity="), end}; if (util::strieq_l("none", valstr)) { - out.affinity.type = AFFINITY_NONE; + out.affinity.type = SessionAffinity::NONE; } else if (util::strieq_l("ip", valstr)) { - out.affinity.type = AFFINITY_IP; + out.affinity.type = SessionAffinity::IP; } else if (util::strieq_l("cookie", valstr)) { - out.affinity.type = AFFINITY_COOKIE; + out.affinity.type = SessionAffinity::COOKIE; } else { LOG(ERROR) << "backend: affinity: value must be one of none, ip, and cookie"; @@ -1004,7 +1004,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr, return -1; } - if (params.affinity.type == AFFINITY_COOKIE && + if (params.affinity.type == SessionAffinity::COOKIE && params.affinity.cookie.name.empty()) { LOG(ERROR) << "backend: affinity-cookie-name is mandatory if " "affinity=cookie is specified"; @@ -1056,10 +1056,10 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr, auto &g = addr_groups[(*it).second]; // Last value wins if we have multiple different affinity // value under one group. - if (params.affinity.type != AFFINITY_NONE) { - if (g.affinity.type == AFFINITY_NONE) { + if (params.affinity.type != SessionAffinity::NONE) { + if (g.affinity.type == SessionAffinity::NONE) { 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( downstreamconf.balloc, params.affinity.cookie.name); if (!params.affinity.cookie.path.empty()) { @@ -1129,7 +1129,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr, auto &g = addr_groups.back(); g.addrs.push_back(addr); 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(downstreamconf.balloc, params.affinity.cookie.name); 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; for (auto &addr : g.addrs) { StringRef key; diff --git a/src/shrpx_config.h b/src/shrpx_config.h index ef2c1d61..d8476479 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -369,13 +369,13 @@ enum class Proto { MEMCACHED, }; -enum shrpx_session_affinity { +enum class SessionAffinity { // No session affinity - AFFINITY_NONE, + NONE, // Client IP affinity - AFFINITY_IP, + IP, // Cookie based affinity - AFFINITY_COOKIE, + COOKIE, }; enum shrpx_cookie_secure { @@ -390,7 +390,7 @@ enum shrpx_cookie_secure { struct AffinityConfig { // Type of session affinity. - shrpx_session_affinity type; + SessionAffinity type; struct { // Name of a cookie to use. StringRef name; @@ -496,13 +496,15 @@ struct AffinityHash { struct DownstreamAddrGroupConfig { 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 mruby_file; std::vector addrs; // Bunch of session affinity hash. Only used if affinity == - // AFFINITY_IP. + // SessionAffinity::IP. std::vector affinity_hash; // Cookie based session affinity configuration. AffinityConfig affinity; diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index 8d035ee1..dac35824 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -259,7 +259,7 @@ int HttpDownstreamConnection::initiate_connection() { // 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 : temp_idx; auto end = next_downstream; @@ -274,7 +274,7 @@ int HttpDownstreamConnection::initiate_connection() { assert(addr->dns); } else { assert(addr_ == nullptr); - if (shared_addr->affinity.type == AFFINITY_NONE) { + if (shared_addr->affinity.type == SessionAffinity::NONE) { addr = &addrs[next_downstream]; if (++next_downstream >= addrs.size()) { next_downstream = 0; @@ -810,7 +810,7 @@ void remove_from_pool(HttpDownstreamConnection *dconn) { auto &group = dconn->get_downstream_addr_group(); auto &shared_addr = group->shared_addr; - if (shared_addr->affinity.type == AFFINITY_NONE) { + if (shared_addr->affinity.type == SessionAffinity::NONE) { auto &dconn_pool = dconn->get_downstream_addr_group()->shared_addr->dconn_pool; dconn_pool.remove_downstream_connection(dconn); diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index a321afc7..9ecdff00 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -77,7 +77,8 @@ DownstreamAddrGroup::~DownstreamAddrGroup() {} using DownstreamKey = std::tuple>, - bool, int, StringRef, StringRef, int, int64_t, int64_t>; + bool, SessionAffinity, StringRef, StringRef, int, int64_t, + int64_t>; namespace { DownstreamKey create_downstream_key( @@ -164,7 +165,7 @@ void Worker::replace_downstream_config( 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(); continue; } @@ -213,7 +214,7 @@ void Worker::replace_downstream_config( shared_addr->addrs.resize(src.addrs.size()); shared_addr->affinity.type = src.affinity.type; - if (src.affinity.type == AFFINITY_COOKIE) { + if (src.affinity.type == SessionAffinity::COOKIE) { shared_addr->affinity.cookie.name = make_string_ref(shared_addr->balloc, src.affinity.cookie.name); if (!src.affinity.cookie.path.empty()) { @@ -303,7 +304,7 @@ void Worker::replace_downstream_config( shared_addr->http1_pri.weight = num_http1; 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) { addr.dconn_pool = std::make_unique(); } diff --git a/src/shrpx_worker.h b/src/shrpx_worker.h index e5361725..f7d025ab 100644 --- a/src/shrpx_worker.h +++ b/src/shrpx_worker.h @@ -137,7 +137,7 @@ struct WeightedPri { struct SharedDownstreamAddr { SharedDownstreamAddr() : balloc(1024, 1024), - affinity{AFFINITY_NONE}, + affinity{SessionAffinity::NONE}, next{0}, http1_pri{}, http2_pri{}, @@ -151,7 +151,7 @@ struct SharedDownstreamAddr { BlockAllocator balloc; std::vector addrs; // Bunch of session affinity hash. Only used if affinity == - // AFFINITY_IP. + // SessionAffinity::IP. std::vector affinity_hash; // List of Http2Session which is not fully utilized (i.e., the // server advertised maximum concurrency is not reached). We will