From 20ea964f2fb529bbd1d4487b543709fbb0534a69 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 16 Oct 2018 22:17:37 +0900 Subject: [PATCH] nghttpx: Convert shrpx_proto to enum class --- src/shrpx_client_handler.cc | 34 ++++++++++++------------- src/shrpx_client_handler.h | 2 +- src/shrpx_config.cc | 20 +++++++-------- src/shrpx_config.h | 11 +++++--- src/shrpx_connection.cc | 2 +- src/shrpx_connection.h | 4 +-- src/shrpx_http2_session.cc | 2 +- src/shrpx_http_downstream_connection.cc | 6 ++--- src/shrpx_live_check.cc | 12 ++++----- src/shrpx_memcached_connection.cc | 2 +- src/shrpx_tls.cc | 4 +-- src/shrpx_worker.cc | 20 +++++++-------- src/shrpx_worker.h | 2 +- 13 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/shrpx_client_handler.cc b/src/shrpx_client_handler.cc index f1605302..9b3ff083 100644 --- a/src/shrpx_client_handler.cc +++ b/src/shrpx_client_handler.cc @@ -396,7 +396,7 @@ ClientHandler::ClientHandler(Worker *worker, int fd, SSL *ssl, get_config()->conn.upstream.ratelimit.write, get_config()->conn.upstream.ratelimit.read, writecb, readcb, timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold, - get_config()->tls.dyn_rec.idle_timeout, PROTO_NONE), + get_config()->tls.dyn_rec.idle_timeout, Proto::NONE), ipaddr_(make_string_ref(balloc_, ipaddr)), port_(make_string_ref(balloc_, port)), faddr_(faddr), @@ -769,7 +769,7 @@ Http2Session *ClientHandler::select_http2_session( // First count the working backend addresses. size_t min = 0; for (const auto &addr : shared_addr->addrs) { - if (addr.proto != PROTO_HTTP2 || addr.connect_blocker->blocked()) { + if (addr.proto != Proto::HTTP2 || addr.connect_blocker->blocked()) { continue; } @@ -825,7 +825,7 @@ Http2Session *ClientHandler::select_http2_session( DownstreamAddr *selected_addr = nullptr; for (auto &addr : shared_addr->addrs) { - if (addr.in_avail || addr.proto != PROTO_HTTP2 || + if (addr.in_avail || addr.proto != Proto::HTTP2 || (addr.http2_extra_freelist.size() == 0 && addr.connect_blocker->blocked())) { continue; @@ -939,7 +939,7 @@ uint32_t ClientHandler::get_affinity_cookie(Downstream *downstream, std::unique_ptr ClientHandler::get_downstream_connection(int &err, Downstream *downstream, - shrpx_proto pref_proto) { + Proto pref_proto) { size_t group_idx; auto &downstreamconf = *worker_->get_downstream_config(); auto &routerconf = downstreamconf.router; @@ -1043,7 +1043,7 @@ ClientHandler::get_downstream_connection(int &err, Downstream *downstream, } addr = &shared_addr->addrs[shared_addr->affinity_hash[i].idx]; if (addr->connect_blocker->blocked() || - (pref_proto != PROTO_NONE && pref_proto != addr->proto)) { + (pref_proto != Proto::NONE && pref_proto != addr->proto)) { continue; } break; @@ -1055,7 +1055,7 @@ ClientHandler::get_downstream_connection(int &err, Downstream *downstream, aff_idx = i; } - if (addr->proto == PROTO_HTTP2) { + if (addr->proto == Proto::HTTP2) { auto http2session = select_http2_session_with_affinity(group, addr); auto dconn = std::make_unique(http2session); @@ -1081,33 +1081,33 @@ ClientHandler::get_downstream_connection(int &err, Downstream *downstream, auto http1_weight = shared_addr->http1_pri.weight; auto http2_weight = shared_addr->http2_pri.weight; - auto proto = PROTO_NONE; + auto proto = Proto::NONE; - if (pref_proto == PROTO_HTTP1) { + if (pref_proto == Proto::HTTP1) { if (http1_weight > 0) { - proto = PROTO_HTTP1; + proto = Proto::HTTP1; } - } else if (pref_proto == PROTO_HTTP2) { + } else if (pref_proto == Proto::HTTP2) { if (http2_weight > 0) { - proto = PROTO_HTTP2; + proto = Proto::HTTP2; } } else if (http1_weight > 0 && http2_weight > 0) { // We only advance cycle if both weight has nonzero to keep its // distance under WEIGHT_MAX. if (pri_less(shared_addr->http1_pri, shared_addr->http2_pri)) { - proto = PROTO_HTTP1; + proto = Proto::HTTP1; shared_addr->http1_pri.cycle = next_cycle(shared_addr->http1_pri); } else { - proto = PROTO_HTTP2; + proto = Proto::HTTP2; shared_addr->http2_pri.cycle = next_cycle(shared_addr->http2_pri); } } else if (http1_weight > 0) { - proto = PROTO_HTTP1; + proto = Proto::HTTP1; } else if (http2_weight > 0) { - proto = PROTO_HTTP2; + proto = Proto::HTTP2; } - if (proto == PROTO_NONE) { + if (proto == Proto::NONE) { if (LOG_ENABLED(INFO)) { CLOG(INFO, this) << "No working downstream address found"; } @@ -1116,7 +1116,7 @@ ClientHandler::get_downstream_connection(int &err, Downstream *downstream, return nullptr; } - if (proto == PROTO_HTTP2) { + if (proto == Proto::HTTP2) { if (LOG_ENABLED(INFO)) { CLOG(INFO, this) << "Downstream connection pool is empty." << " Create new one"; diff --git a/src/shrpx_client_handler.h b/src/shrpx_client_handler.h index 9e1153bd..37999f26 100644 --- a/src/shrpx_client_handler.h +++ b/src/shrpx_client_handler.h @@ -106,7 +106,7 @@ public: // backend whose protocol is |pref_proto|. std::unique_ptr get_downstream_connection(int &err, Downstream *downstream, - shrpx_proto pref_proto = PROTO_NONE); + Proto pref_proto = Proto::NONE); MemchunkPool *get_mcpool(); SSL *get_ssl() const; // Call this function when HTTP/2 connection header is received at diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index b9fe0e3a..76ffc55d 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -816,7 +816,7 @@ struct DownstreamParams { ev_tstamp write_timeout; size_t fall; size_t rise; - shrpx_proto proto; + Proto proto; bool tls; bool dns; bool redirect_if_not_tls; @@ -858,10 +858,10 @@ int parse_downstream_params(DownstreamParams &out, } if (util::streq_l("h2", std::begin(protostr), protostr.size())) { - out.proto = PROTO_HTTP2; + out.proto = Proto::HTTP2; } else if (util::streq_l("http/1.1", std::begin(protostr), protostr.size())) { - out.proto = PROTO_HTTP1; + out.proto = Proto::HTTP1; } else { LOG(ERROR) << "backend: proto: unknown protocol " << protostr; return -1; @@ -993,7 +993,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr, auto &addr_groups = downstreamconf.addr_groups; DownstreamParams params{}; - params.proto = PROTO_HTTP1; + params.proto = Proto::HTTP1; if (parse_downstream_params(params, src_params) != 0) { return -1; @@ -3872,15 +3872,15 @@ int int_syslog_facility(const StringRef &strfacility) { return -1; } -StringRef strproto(shrpx_proto proto) { +StringRef strproto(Proto proto) { switch (proto) { - case PROTO_NONE: + case Proto::NONE: return StringRef::from_lit("none"); - case PROTO_HTTP1: + case Proto::HTTP1: return StringRef::from_lit("http/1.1"); - case PROTO_HTTP2: + case Proto::HTTP2: return StringRef::from_lit("h2"); - case PROTO_MEMCACHED: + case Proto::MEMCACHED: return StringRef::from_lit("memcached"); } @@ -3943,7 +3943,7 @@ int configure_downstream_group(Config *config, bool http2_proxy, DownstreamAddrConfig addr{}; addr.host = StringRef::from_lit(DEFAULT_DOWNSTREAM_HOST); addr.port = DEFAULT_DOWNSTREAM_PORT; - addr.proto = PROTO_HTTP1; + addr.proto = Proto::HTTP1; DownstreamAddrGroupConfig g(StringRef::from_lit("/")); g.addrs.push_back(std::move(addr)); diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 3b60e77f..ef2c1d61 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -362,7 +362,12 @@ constexpr size_t SHRPX_OBFUSCATED_NODE_LENGTH = 8; constexpr char DEFAULT_DOWNSTREAM_HOST[] = "127.0.0.1"; constexpr int16_t DEFAULT_DOWNSTREAM_PORT = 80; -enum shrpx_proto { PROTO_NONE, PROTO_HTTP1, PROTO_HTTP2, PROTO_MEMCACHED }; +enum class Proto { + NONE, + HTTP1, + HTTP2, + MEMCACHED, +}; enum shrpx_session_affinity { // No session affinity @@ -466,7 +471,7 @@ struct DownstreamAddrConfig { size_t fall; size_t rise; // Application protocol used in this group - shrpx_proto proto; + Proto proto; // backend port. 0 if |host_unix| is true. uint16_t port; // true if |host| contains UNIX domain socket path. @@ -1232,7 +1237,7 @@ read_tls_ticket_key_file(const std::vector &files, const EVP_CIPHER *cipher, const EVP_MD *hmac); // Returns string representation of |proto|. -StringRef strproto(shrpx_proto proto); +StringRef strproto(Proto proto); int configure_downstream_group(Config *config, bool http2_proxy, bool numeric_addr_only, diff --git a/src/shrpx_connection.cc b/src/shrpx_connection.cc index 335e4e48..f189d282 100644 --- a/src/shrpx_connection.cc +++ b/src/shrpx_connection.cc @@ -59,7 +59,7 @@ Connection::Connection(struct ev_loop *loop, int fd, SSL *ssl, const RateLimitConfig &read_limit, IOCb writecb, IOCb readcb, TimerCb timeoutcb, void *data, size_t tls_dyn_rec_warmup_threshold, - ev_tstamp tls_dyn_rec_idle_timeout, shrpx_proto proto) + ev_tstamp tls_dyn_rec_idle_timeout, Proto proto) : tls{DefaultMemchunks(mcpool), DefaultPeekMemchunks(mcpool), DefaultMemchunks(mcpool)}, wlimit(loop, &wev, write_limit.rate, write_limit.burst), diff --git a/src/shrpx_connection.h b/src/shrpx_connection.h index d9ca5c0f..71f1be7e 100644 --- a/src/shrpx_connection.h +++ b/src/shrpx_connection.h @@ -100,7 +100,7 @@ struct Connection { const RateLimitConfig &write_limit, const RateLimitConfig &read_limit, IOCb writecb, IOCb readcb, TimerCb timeoutcb, void *data, size_t tls_dyn_rec_warmup_threshold, - ev_tstamp tls_dyn_rec_idle_timeout, shrpx_proto proto); + ev_tstamp tls_dyn_rec_idle_timeout, Proto proto); ~Connection(); void disconnect(); @@ -169,7 +169,7 @@ struct Connection { // Application protocol used over the connection. This field is not // used in this object at the moment. The rest of the program may // use this value when it is useful. - shrpx_proto proto; + Proto proto; // The point of time when last read is observed. Note: since we use // |rt| as idle timer, the activity is not limited to read. ev_tstamp last_read; diff --git a/src/shrpx_http2_session.cc b/src/shrpx_http2_session.cc index 23958d06..7e924260 100644 --- a/src/shrpx_http2_session.cc +++ b/src/shrpx_http2_session.cc @@ -189,7 +189,7 @@ Http2Session::Http2Session(struct ev_loop *loop, SSL_CTX *ssl_ctx, group->shared_addr->timeout.write, group->shared_addr->timeout.read, {}, {}, writecb, readcb, timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold, - get_config()->tls.dyn_rec.idle_timeout, PROTO_HTTP2), + get_config()->tls.dyn_rec.idle_timeout, Proto::HTTP2), wb_(worker->get_mcpool()), worker_(worker), ssl_ctx_(ssl_ctx), diff --git a/src/shrpx_http_downstream_connection.cc b/src/shrpx_http_downstream_connection.cc index bae1961a..8d035ee1 100644 --- a/src/shrpx_http_downstream_connection.cc +++ b/src/shrpx_http_downstream_connection.cc @@ -92,7 +92,7 @@ void retry_downstream_connection(Downstream *downstream, // request in request buffer. auto ndconn = handler->get_downstream_connection( rv, downstream, - downstream->get_request_header_sent() ? PROTO_HTTP1 : PROTO_NONE); + downstream->get_request_header_sent() ? Proto::HTTP1 : Proto::NONE); if (ndconn) { if (downstream->attach_downstream_connection(std::move(ndconn)) == 0 && downstream->push_request_headers() == 0) { @@ -193,7 +193,7 @@ HttpDownstreamConnection::HttpDownstreamConnection( group->shared_addr->timeout.write, group->shared_addr->timeout.read, {}, {}, connectcb, readcb, connect_timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold, - get_config()->tls.dyn_rec.idle_timeout, PROTO_HTTP1), + get_config()->tls.dyn_rec.idle_timeout, Proto::HTTP1), on_read_(&HttpDownstreamConnection::noop), on_write_(&HttpDownstreamConnection::noop), signal_write_(&HttpDownstreamConnection::noop), @@ -286,7 +286,7 @@ int HttpDownstreamConnection::initiate_connection() { } } - if (addr->proto != PROTO_HTTP1) { + if (addr->proto != Proto::HTTP1) { if (end == next_downstream) { return SHRPX_ERR_NETWORK; } diff --git a/src/shrpx_live_check.cc b/src/shrpx_live_check.cc index ee0d868d..7c4c8f57 100644 --- a/src/shrpx_live_check.cc +++ b/src/shrpx_live_check.cc @@ -106,7 +106,7 @@ LiveCheck::LiveCheck(struct ev_loop *loop, SSL_CTX *ssl_ctx, Worker *worker, worker->get_downstream_config()->timeout.write, worker->get_downstream_config()->timeout.read, {}, {}, writecb, readcb, timeoutcb, this, get_config()->tls.dyn_rec.warmup_threshold, - get_config()->tls.dyn_rec.idle_timeout, PROTO_NONE), + get_config()->tls.dyn_rec.idle_timeout, Proto::NONE), wb_(worker->get_mcpool()), gen_(gen), read_(&LiveCheck::noop), @@ -211,10 +211,10 @@ int LiveCheck::initiate_connection() { } switch (addr_->proto) { - case PROTO_HTTP1: + case Proto::HTTP1: tls::setup_downstream_http1_alpn(ssl); break; - case PROTO_HTTP2: + case Proto::HTTP2: tls::setup_downstream_http2_alpn(ssl); break; default: @@ -359,7 +359,7 @@ int LiveCheck::connected() { return do_write(); } - if (addr_->proto == PROTO_HTTP2) { + if (addr_->proto == Proto::HTTP2) { // For HTTP/2, we try to read SETTINGS ACK from server to make // sure it is really alive, and serving HTTP/2. read_ = &LiveCheck::read_clear; @@ -418,12 +418,12 @@ int LiveCheck::tls_handshake() { auto proto = StringRef{next_proto, next_proto_len}; switch (addr_->proto) { - case PROTO_HTTP1: + case Proto::HTTP1: if (proto.empty() || proto == StringRef::from_lit("http/1.1")) { break; } return -1; - case PROTO_HTTP2: + case Proto::HTTP2: if (util::check_h2_is_selected(proto)) { // For HTTP/2, we try to read SETTINGS ACK from server to make // sure it is really alive, and serving HTTP/2. diff --git a/src/shrpx_memcached_connection.cc b/src/shrpx_memcached_connection.cc index 99ec1eb9..3a9ef214 100644 --- a/src/shrpx_memcached_connection.cc +++ b/src/shrpx_memcached_connection.cc @@ -102,7 +102,7 @@ MemcachedConnection::MemcachedConnection(const Address *addr, MemchunkPool *mcpool, std::mt19937 &gen) : conn_(loop, -1, nullptr, mcpool, write_timeout, read_timeout, {}, {}, - connectcb, readcb, timeoutcb, this, 0, 0., PROTO_MEMCACHED), + connectcb, readcb, timeoutcb, this, 0, 0., Proto::MEMCACHED), do_read_(&MemcachedConnection::noop), do_write_(&MemcachedConnection::noop), sni_name_(sni_name), diff --git a/src/shrpx_tls.cc b/src/shrpx_tls.cc index 57d41f20..8b089597 100644 --- a/src/shrpx_tls.cc +++ b/src/shrpx_tls.cc @@ -1049,9 +1049,9 @@ int select_next_proto_cb(SSL *ssl, unsigned char **out, unsigned char *outlen, void *arg) { auto conn = static_cast(SSL_get_app_data(ssl)); switch (conn->proto) { - case PROTO_HTTP1: + case Proto::HTTP1: return select_h1_next_proto_cb(ssl, out, outlen, in, inlen, arg); - case PROTO_HTTP2: + case Proto::HTTP2: return select_h2_next_proto_cb(ssl, out, outlen, in, inlen, arg); default: return SSL_TLSEXT_ERR_NOACK; diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index ced284cb..a321afc7 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -74,10 +74,10 @@ DownstreamAddrGroup::~DownstreamAddrGroup() {} // DownstreamKey is used to index SharedDownstreamAddr in order to // find the same configuration. -using DownstreamKey = std::tuple< - std::vector>, - bool, int, StringRef, StringRef, int, int64_t, int64_t>; +using DownstreamKey = + std::tuple>, + bool, int, StringRef, StringRef, int, int64_t, int64_t>; namespace { DownstreamKey create_downstream_key( @@ -254,10 +254,10 @@ void Worker::replace_downstream_config( randgen_, loop_, [shared_addr_ptr, &dst_addr]() { switch (dst_addr.proto) { - case PROTO_HTTP1: + case Proto::HTTP1: --shared_addr_ptr->http1_pri.weight; break; - case PROTO_HTTP2: + case Proto::HTTP2: --shared_addr_ptr->http2_pri.weight; break; default: @@ -266,10 +266,10 @@ void Worker::replace_downstream_config( }, [shared_addr_ptr, &dst_addr]() { switch (dst_addr.proto) { - case PROTO_HTTP1: + case Proto::HTTP1: ++shared_addr_ptr->http1_pri.weight; break; - case PROTO_HTTP2: + case Proto::HTTP2: ++shared_addr_ptr->http2_pri.weight; break; default: @@ -280,10 +280,10 @@ void Worker::replace_downstream_config( dst_addr.live_check = std::make_unique( loop_, cl_ssl_ctx_, this, &dst_addr, randgen_); - if (dst_addr.proto == PROTO_HTTP2) { + if (dst_addr.proto == Proto::HTTP2) { ++num_http2; } else { - assert(dst_addr.proto == PROTO_HTTP1); + assert(dst_addr.proto == Proto::HTTP1); ++num_http1; } } diff --git a/src/shrpx_worker.h b/src/shrpx_worker.h index cc8c2a03..e5361725 100644 --- a/src/shrpx_worker.h +++ b/src/shrpx_worker.h @@ -110,7 +110,7 @@ struct DownstreamAddr { // address. size_t num_dconn; // Application protocol used in this backend - shrpx_proto proto; + Proto proto; // true if TLS is used in this backend bool tls; // true if dynamic DNS is enabled