From d39335829dfc5ca218317ad6639612c9f71742cd Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 6 May 2016 23:10:09 +0900 Subject: [PATCH] nghttpx: Enable kqueue by default We enabled libev kqueue backend in nghttpx by default. Since it might not work on some platforms, we also added --no-kqueue option to disable it. --- gennghttpxfun.py | 1 + src/shrpx.cc | 15 ++++++++++++++- src/shrpx_config.cc | 13 +++++++++++++ src/shrpx_config.h | 3 +++ src/shrpx_connection_handler.cc | 2 +- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/gennghttpxfun.py b/gennghttpxfun.py index 03eec22d..27998498 100755 --- a/gennghttpxfun.py +++ b/gennghttpxfun.py @@ -129,6 +129,7 @@ OPTIONS = [ "backend-tls", "backend-connections-per-host", "error-page", + "no-kqueue", ] LOGVARS = [ diff --git a/src/shrpx.cc b/src/shrpx.cc index 9030e5b6..cd655f8c 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -969,7 +969,7 @@ int event_loop() { return -1; } - auto loop = EV_DEFAULT; + auto loop = ev_default_loop(get_config()->ev_loop_flags); auto pid = fork_worker_process(&ssv); @@ -1042,6 +1042,10 @@ void fill_default_config() { mod_config()->conf_path = "/etc/nghttpx/nghttpx.conf"; mod_config()->pid = getpid(); + if (ev_supported_backends() & ~ev_recommended_backends() & EVBACKEND_KQUEUE) { + mod_config()->ev_loop_flags = ev_recommended_backends() | EVBACKEND_KQUEUE; + } + auto &tlsconf = mod_config()->tls; { auto &ticketconf = tlsconf.ticket; @@ -1439,6 +1443,10 @@ Performance: that have not yet completed the three-way handshake. If value is 0 then fast open is disabled. Default: )" << get_config()->conn.listener.fastopen << R"( + --no-kqueue Don't use kqueue. This option is only applicable for + the platforms which have kqueue. For other platforms, + this option will be simply ignored. + Timeout: --frontend-http2-read-timeout= Specify read timeout for HTTP/2 and SPDY frontend @@ -2560,6 +2568,7 @@ int main(int argc, char **argv) { {SHRPX_OPT_BACKEND_CONNECTIONS_PER_HOST.c_str(), required_argument, &flag, 121}, {SHRPX_OPT_ERROR_PAGE.c_str(), required_argument, &flag, 122}, + {SHRPX_OPT_NO_KQUEUE.c_str(), no_argument, &flag, 123}, {nullptr, 0, nullptr, 0}}; int option_index = 0; @@ -3135,6 +3144,10 @@ int main(int argc, char **argv) { // --error-page cmdcfgs.emplace_back(SHRPX_OPT_ERROR_PAGE, StringRef{optarg}); break; + case 123: + // --no-kqueue + cmdcfgs.emplace_back(SHRPX_OPT_NO_KQUEUE, StringRef::from_lit("yes")); + break; default: break; } diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index 11c88e62..bcb0b50a 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -993,6 +993,7 @@ enum { SHRPX_OPTID_MRUBY_FILE, SHRPX_OPTID_NO_HOST_REWRITE, SHRPX_OPTID_NO_HTTP2_CIPHER_BLACK_LIST, + SHRPX_OPTID_NO_KQUEUE, SHRPX_OPTID_NO_LOCATION_REWRITE, SHRPX_OPTID_NO_OCSP, SHRPX_OPTID_NO_SERVER_PUSH, @@ -1161,6 +1162,9 @@ int option_lookup_token(const char *name, size_t namelen) { case 9: switch (name[8]) { case 'e': + if (util::strieq_l("no-kqueu", name, 8)) { + return SHRPX_OPTID_NO_KQUEUE; + } if (util::strieq_l("read-rat", name, 8)) { return SHRPX_OPTID_READ_RATE; } @@ -2712,6 +2716,15 @@ int parse_config(const StringRef &opt, const StringRef &optarg, opt, optarg); case SHRPX_OPTID_ERROR_PAGE: return parse_error_page(mod_config()->http.error_pages, opt, optarg); + case SHRPX_OPTID_NO_KQUEUE: + if ((ev_supported_backends() & EVBACKEND_KQUEUE) == 0) { + LOG(WARN) << opt << ": kqueue is not supported on this platform"; + return 0; + } + + mod_config()->ev_loop_flags = ev_recommended_backends() & ~EVBACKEND_KQUEUE; + + return 0; case SHRPX_OPTID_CONF: LOG(WARN) << "conf: ignored"; diff --git a/src/shrpx_config.h b/src/shrpx_config.h index f5e4c42d..09471d21 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -275,6 +275,7 @@ constexpr auto SHRPX_OPT_BACKEND_TLS = StringRef::from_lit("backend-tls"); constexpr auto SHRPX_OPT_BACKEND_CONNECTIONS_PER_HOST = StringRef::from_lit("backend-connections-per-host"); constexpr auto SHRPX_OPT_ERROR_PAGE = StringRef::from_lit("error-page"); +constexpr auto SHRPX_OPT_NO_KQUEUE = StringRef::from_lit("no-kqueue"); constexpr size_t SHRPX_OBFUSCATED_NODE_LENGTH = 8; @@ -652,6 +653,8 @@ struct Config { bool verbose; bool daemon; bool http2_proxy; + // flags passed to ev_default_loop() and ev_loop_new() + int ev_loop_flags; }; const Config *get_config(); diff --git a/src/shrpx_connection_handler.cc b/src/shrpx_connection_handler.cc index e6771bfa..dc3223a9 100644 --- a/src/shrpx_connection_handler.cc +++ b/src/shrpx_connection_handler.cc @@ -244,7 +244,7 @@ int ConnectionHandler::create_worker_thread(size_t num) { auto &memcachedconf = get_config()->tls.session_cache.memcached; for (size_t i = 0; i < num; ++i) { - auto loop = ev_loop_new(0); + auto loop = ev_loop_new(get_config()->ev_loop_flags); SSL_CTX *session_cache_ssl_ctx = nullptr; if (memcachedconf.tls) {