Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify bin/apisix to support the SO_REUSEPORT #1085

Merged
merged 7 commits into from
Feb 9, 2020
75 changes: 75 additions & 0 deletions bin/apisix
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,26 @@ stream {
}

server {
{% if enable_reuseport then %}

{% for _, port in ipairs(stream_proxy.tcp or {}) do %}
listen {*port*} reuseport;
{% end %}
{% for _, port in ipairs(stream_proxy.udp or {}) do %}
listen {*port*} udp reuseport;
{% end %}

{% else %} {% --if enable_reuseport %}

{% for _, port in ipairs(stream_proxy.tcp or {}) do %}
listen {*port*};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listen {*port*} {% if enable_reuseport then %} reuseport {% end %};

This style is simpler.

{% end %}
{% for _, port in ipairs(stream_proxy.udp or {}) do %}
listen {*port*} udp;
{% end %}

{% end %}

preread_by_lua_block {
apisix.stream_preread_phase()
}
Expand Down Expand Up @@ -261,6 +274,22 @@ http {
{% end %}

server {
{% if enable_reuseport then %}

listen {* node_listen *} reuseport;
{% if ssl.enable then %}
listen {* ssl.listen_port *} ssl {% if ssl.enable_http2 then %} http2 {% end %} reuseport;
{% end %}

{% if enable_ipv6 then %}
listen [::]:{* node_listen *} reuseport;
{% if ssl.enable then %}
listen [::]:{* node_ssl_listen *} ssl {% if ssl.enable_http2 then %} http2 {% end %} reuseport;
{% end %}
{% end %} {% -- if enable_ipv6 %}

{% else %} {% -- if enable_reuseport %}

listen {* node_listen *};
{% if ssl.enable then %}
listen {* ssl.listen_port *} ssl {% if ssl.enable_http2 then %} http2 {% end %};
Expand All @@ -273,6 +302,8 @@ http {
{% end %}
{% end %} {% -- if enable_ipv6 %}

{% end %} {% -- end enable_reuseport %}

ssl_certificate cert/apisix.crt;
ssl_certificate_key cert/apisix.key;
ssl_session_cache shared:SSL:1m;
Expand Down Expand Up @@ -437,6 +468,28 @@ local function get_openresty_version()
return nil
end

local function check_macos_kernel()
local str = "Darwin"
local ret = excute_cmd("uname")
local pos = string.find(ret, str)
if pos == nil then
return false
end

return true
end

local function get_linux_kernel_version()
local str = "Linux version "
local ret = excute_cmd("cat /proc/version")
local m = string.match(ret, "[0-9]+\.[0-9]+\.[0-9]+")
if m == nil then
return nil
end

return m
end

local function split(self, sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
Expand Down Expand Up @@ -507,6 +560,7 @@ local function init()
with_module_status = with_module_status,
node_ssl_listen = 9443, -- default value
error_log = {level = "warn"},
enable_reuseport = false, -- default value
}

if not yaml_conf.apisix then
Expand All @@ -530,6 +584,27 @@ local function init()
sys_conf["worker_processes"] = "auto"
end

local check_macos = check_macos_kernel()
if check_macos == true then
enable_reuseport = false
return
end

--linux try to up enable_reuseport
local kernel_ver = get_linux_kernel_version()
if kernel_ver == nil then
io.stderr:write("can not find kernel version\n")
return
end

local reuseport_kernel_ver = "3.9.0"
if check_or_version(kernel_ver, reuseport_kernel_ver) then
io.stderr:write("kernel version >=", reuseport_kernel_ver, " current ", kernel_ver, ", up SO_REUSEPORT.\n")
enable_reuseport = true
else
io.stderr:write("kernel version <", reuseport_kernel_ver, " current ", kernel_ver, ", apisix will not open SO_REUSEPORT\n")
end

local conf_render = template.compile(ngx_tpl)
local ngxconf = conf_render(sys_conf)

Expand Down