1
0
mirror of https://github.com/openresty/openresty synced 2024-11-14 08:45:50 +01:00
openresty/t/002-reuseport-close-unused-fds.t
spacewander cf7516fcbc optimize: added an NGINX core patch to ensure unused listening fds are closed when 'reuseport' is used.
When `reuseport` is enabled in the `listen` directive, Nginx will create
a listening fd for each worker process in the master process.

These fds will be inherited by the worker processes, but most of them
are unused. For example, considering we have 32 listening ip:port
configurations and 64 worker processes, each worker process will inherit
2048 (32 * 64) listening fds, but only 32 fds are used. By closing the
unused fds, this change could save up to 2016 (32 * 63) fds in a worker
process.

It doesn't affect the listening socket, since there is only one used fd
which associates to the socket with or without this change.

Co-authored-by: Thibault Charbonnier <thibaultcha@me.com>
2019-08-05 18:54:51 -07:00

84 lines
1.7 KiB
C

# vim:set ft= ts=4 sw=4 et fdm=marker:
use Test::Nginx::Socket::Lua;
master_on();
workers(2);
log_level('debug');
repeat_each(1);
plan tests => repeat_each() * (blocks() * 3);
no_long_string();
run_tests();
__DATA__
=== TEST 1: closes unused file descriptors when 'reuseport' is enabled
--- http_config
server {
listen 127.0.0.1:12345 reuseport;
}
--- config
location /t {
return 200;
}
--- request
GET /t
--- ignore_response_body
--- no_error_log
[error]
--- grep_error_log eval: qr/\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.1:12345/
--- grep_error_log_out eval
qr/\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.1:12345
\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.1:12345
/
=== TEST 2: closes multiple unused file descriptors when 'reuseport' is enabled
--- http_config
server {
listen 127.0.0.1:12345 reuseport;
}
server {
listen 127.0.0.2:12345 reuseport;
}
--- config
location /t {
return 200;
}
--- request
GET /t
--- ignore_response_body
--- no_error_log
[error]
--- grep_error_log eval: qr/\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.\d+:12345/
--- grep_error_log_out eval
qr/\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.1:12345
\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.2:12345
\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.1:12345
\[debug\] .*? closing unused fd:\d+ listening on 127\.0\.0\.2:12345
/
=== TEST 3: does not close any fd if 'reuseport' is not used
--- http_config
server {
listen 127.0.0.1:12345;
}
--- config
location /t {
return 200;
}
--- request
GET /t
--- ignore_response_body
--- no_error_log
[error]
closing unused fd