From 402ebb277f127b56f20e00e15439c6a18d6d4039 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 27 Jan 2015 23:47:56 +0900 Subject: [PATCH] nghttpx: Add --num-accept and --accept-delay options --- src/shrpx.cc | 21 +++++++++++++++++++++ src/shrpx_accept_handler.cc | 8 +++++++- src/shrpx_config.cc | 24 ++++++++++++++++++++++++ src/shrpx_config.h | 4 ++++ 4 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/shrpx.cc b/src/shrpx.cc index 71adfc4b..6134fc26 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -757,6 +757,8 @@ void fill_default_config() { mod_config()->tls_ctx_per_worker = false; mod_config()->downstream_request_buffer_size = 16 * 1024; mod_config()->downstream_response_buffer_size = 16 * 1024; + mod_config()->num_accept = 16; + mod_config()->accept_delay = 0.5; } } // namespace @@ -897,6 +899,15 @@ Performance: Default: )" << util::utos_with_unit(get_config()->downstream_response_buffer_size) << R"( + --num-accept= + The number of connections acceptor can accept at once. + Default: )" << get_config()->num_accept << R"( + --accept-delay= + Acceptors get idle in milliseconds after they + accepted at most N connections, where N is defined in + --num-accept option. + Default: )" << static_cast(get_config()->accept_delay * 1000) + << R"( Timeout: --frontend-http2-read-timeout= @@ -1301,6 +1312,8 @@ int main(int argc, char **argv) { {"tls-ctx-per-worker", no_argument, &flag, 70}, {"backend-response-buffer", required_argument, &flag, 71}, {"backend-request-buffer", required_argument, &flag, 72}, + {"num-accept", required_argument, &flag, 73}, + {"accept-delay", required_argument, &flag, 74}, {nullptr, 0, nullptr, 0}}; int option_index = 0; @@ -1630,6 +1643,14 @@ int main(int argc, char **argv) { // --backend-request-buffer cmdcfgs.emplace_back(SHRPX_OPT_BACKEND_REQUEST_BUFFER, optarg); break; + case 73: + // --num-accept + cmdcfgs.emplace_back(SHRPX_OPT_NUM_ACCEPT, optarg); + break; + case 74: + // --accept-delay + cmdcfgs.emplace_back(SHRPX_OPT_ACCEPT_DELAY, optarg); + break; default: break; } diff --git a/src/shrpx_accept_handler.cc b/src/shrpx_accept_handler.cc index 364cc9f8..dc1746f9 100644 --- a/src/shrpx_accept_handler.cc +++ b/src/shrpx_accept_handler.cc @@ -56,6 +56,7 @@ AcceptHandler::~AcceptHandler() { } void AcceptHandler::accept_connection() { + size_t n = 0; for (;;) { sockaddr_union sockaddr; socklen_t addrlen = sizeof(sockaddr); @@ -83,7 +84,7 @@ void AcceptHandler::accept_connection() { continue; } - return; + break; } #ifndef HAVE_ACCEPT4 @@ -94,7 +95,12 @@ void AcceptHandler::accept_connection() { util::make_socket_nodelay(cfd); conn_hnr_->handle_connection(cfd, &sockaddr.sa, addrlen); + ++n; + if (n == get_config()->num_accept) { + break; + } } + conn_hnr_->disable_acceptor_temporary(get_config()->accept_delay); } void AcceptHandler::enable() { ev_io_start(conn_hnr_->get_loop(), &wev_); } diff --git a/src/shrpx_config.cc b/src/shrpx_config.cc index ac32ae2d..13fc354d 100644 --- a/src/shrpx_config.cc +++ b/src/shrpx_config.cc @@ -143,6 +143,8 @@ const char SHRPX_OPT_RLIMIT_NOFILE[] = "rlimit-nofile"; const char SHRPX_OPT_TLS_CTX_PER_WORKER[] = "tls-ctx-per-worker"; const char SHRPX_OPT_BACKEND_REQUEST_BUFFER[] = "backend-request-buffer"; const char SHRPX_OPT_BACKEND_RESPONSE_BUFFER[] = "backend-response-buffer"; +const char SHRPX_OPT_NUM_ACCEPT[] = "num-accept"; +const char SHRPX_OPT_ACCEPT_DELAY[] = "accept-delay"; namespace { Config *config = nullptr; @@ -1156,6 +1158,28 @@ int parse_config(const char *opt, const char *optarg) { return 0; } + if (util::strieq(opt, SHRPX_OPT_NUM_ACCEPT)) { + size_t n; + if (parse_uint(&n, opt, optarg) != 0) { + return -1; + } + + mod_config()->num_accept = n; + + return 0; + } + + if (util::strieq(opt, SHRPX_OPT_ACCEPT_DELAY)) { + size_t n; + if (parse_uint(&n, opt, optarg) != 0) { + return -1; + } + + mod_config()->accept_delay = n / 1000.; + + return 0; + } + if (util::strieq(opt, "conf")) { LOG(WARN) << "conf: ignored"; diff --git a/src/shrpx_config.h b/src/shrpx_config.h index 41c4889a..c64a2c69 100644 --- a/src/shrpx_config.h +++ b/src/shrpx_config.h @@ -131,6 +131,8 @@ extern const char SHRPX_OPT_RLIMIT_NOFILE[]; extern const char SHRPX_OPT_TLS_CTX_PER_WORKER[]; extern const char SHRPX_OPT_BACKEND_REQUEST_BUFFER[]; extern const char SHRPX_OPT_BACKEND_RESPONSE_BUFFER[]; +extern const char SHRPX_OPT_NUM_ACCEPT[]; +extern const char SHRPX_OPT_ACCEPT_DELAY[]; union sockaddr_union { sockaddr_storage storage; @@ -197,6 +199,7 @@ struct Config { ev_tstamp stream_write_timeout; ev_tstamp downstream_idle_read_timeout; ev_tstamp listener_disable_timeout; + ev_tstamp accept_delay; std::unique_ptr host; std::unique_ptr private_key_file; std::unique_ptr private_key_passwd; @@ -260,6 +263,7 @@ struct Config { size_t rlimit_nofile; size_t downstream_request_buffer_size; size_t downstream_response_buffer_size; + size_t num_accept; // Bit mask to disable SSL/TLS protocol versions. This will be // passed to SSL_CTX_set_options(). long int tls_proto_mask;