Skip to content

Commit

Permalink
feat(conf) new upstream keepalive connection pools properties
Browse files Browse the repository at this point in the history
* Introduce new properties for upstream keepalive pooling.
* Ensure older properties are properly deprecated and that their
  settings cascades to the new ones.
* Use different examples for NGINX directives injection.
  • Loading branch information
thibaultcha authored and hishamhm committed Jun 9, 2020
1 parent f37fa1b commit 1385d52
Show file tree
Hide file tree
Showing 6 changed files with 543 additions and 148 deletions.
122 changes: 70 additions & 52 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -586,43 +586,45 @@
# See http://nginx.org/en/docs/http/ngx_http_realip_module.html#real_ip_recursive
# for a description of this directive.

#client_max_body_size = 0 # Defines the maximum request body size allowed
# by requests proxied by Kong, specified in
# the Content-Length request header. If a
# request exceeds this limit, Kong will
# respond with a 413 (Request Entity Too
# Large). Setting this value to 0 disables
# checking the request body size.

# See http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
# for further description of this parameter. Numeric values may be suffixed
# with `k` or `m` to denote limits in terms of kilobytes or megabytes.

#client_body_buffer_size = 8k # Defines the buffer size for reading the
# request body. If the client request body is
# larger than this value, the body will be
# buffered to disk. Note that when the body is
# buffered to disk Kong plugins that access or
# manipulate the request body may not work, so
# it is advisable to set this value as high as
# possible (e.g., set it as high as
# `client_max_body_size` to force request
# bodies to be kept in memory). Do note that
# high-concurrency environments will require
# significant memory allocations to process
# many concurrent large request bodies.

# See http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size
# for further description of this parameter. Numeric values may be suffixed
# with `k` or `m` to denote limits in terms of kilobytes or megabytes.

#error_default_type = text/plain # Default MIME type to use when the request
# `Accept` header is missing and Nginx
# is returning an error for the request.
# Accepted values are `text/plain`,
# `text/html`, `application/json`, and
# `application/xml`.

#upstream_keepalive_pool_size = 60 # Sets the default size of the upstream
# keepalive connection pools.
# Upstream keepalive connection pools
# are segmented by the `dst ip/dst
# port/SNI` attributes of a connection.
# A value of `0` will disable upstream
# keepalive connections by default, forcing
# each upstream request to open a new
# connection.

#upstream_keepalive_max_requests = 100 # Sets the default maximum number of
# requests than can be proxied upstream
# through one keepalive connection.
# After the maximum number of requests
# is reached, the connection will be
# closed.
# A value of `0` will disable this
# behavior, and a keepalive connection
# can be used to proxy an indefinite
# number of requests.

#upstream_keepalive_idle_timeout = 60 # Sets the default timeout (in seconds)
# for which an upstream keepalive
# connection should be kept open. When
# the timeout is reached while the
# connection has not been reused, it
# will be closed.
# A value of `0` will disable this
# behavior, and an idle keepalive
# connection may be kept open
# indefinitely.

#------------------------------------------------------------------------------
# NGINX injected directives
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -696,28 +698,44 @@
#
# See http://nginx.org/en/docs/ngx_core_module.html#worker_connections

#nginx_upstream_keepalive = 60 # Sets the maximum number of idle keepalive
# connections to upstream servers that are
# preserved in the cache of each worker
# process. When this number is exceeded, the
# least recently used connections are closed.
# A value of `NONE` will disable this behavior
# altogether, forcing each upstream request
# to open a new connection.
# See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

#nginx_upstream_keepalive_requests = 100
# Sets the maximum number of requests that can
# be served through one keepalive connection.
# After the maximum number of requests is
# made, the connection is closed.
# See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive_requests

#nginx_upstream_keepalive_timeout = 60s
# Sets a timeout during which an idle
# keepalive connection to an upstream server
# will stay open.
# See http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive_timeout
#nginx_http_client_header_buffer_size = 1k # Sets buffer size for reading the
# client request headers.
# See http://nginx.org/en/docs/http/ngx_http_core_module.html#client_header_buffer_size

#nginx_http_large_client_header_buffers = 4 8k # Sets the maximum number and
# size of buffers used for
# reading large clients
# requests headers.
# See http://nginx.org/en/docs/http/ngx_http_core_module.html#large_client_header_buffers

#nginx_http_client_max_body_size = 0 # Defines the maximum request body size
# allowed by requests proxied by Kong,
# specified in the Content-Length request
# header. If a request exceeds this
# limit, Kong will respond with a 413
# (Request Entity Too Large). Setting
# this value to 0 disables checking the
# request body size.
# See http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

#nginx_http_client_body_buffer_size = 8k # Defines the buffer size for reading
# the request body. If the client
# request body is larger than this
# value, the body will be buffered to
# disk. Note that when the body is
# buffered to disk Kong plugins that
# access or manipulate the request
# body may not work, so it is
# advisable to set this value as high
# as possible (e.g., set it as high
# as `client_max_body_size` to force
# request bodies to be kept in
# memory). Do note that
# high-concurrency environments will
# require significant memory
# allocations to process many
# concurrent large request bodies.
# See http://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size

#------------------------------------------------------------------------------
# DATASTORE
Expand Down
160 changes: 128 additions & 32 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,67 @@ local PREFIX_PATHS = {
}


local function upstream_keepalive_deprecated_properties(conf)
-- nginx_http_upstream_keepalive -> nginx_upstream_keepalive
if conf.nginx_upstream_keepalive == nil then
if conf.nginx_http_upstream_keepalive ~= nil then
conf.nginx_upstream_keepalive = conf.nginx_http_upstream_keepalive
end
end

-- upstream_keepalive -> nginx_upstream_keepalive + nginx_http_upstream_keepalive
if conf.nginx_upstream_keepalive == nil then
if conf.upstream_keepalive ~= nil then
if conf.upstream_keepalive == 0 then
conf.nginx_upstream_keepalive = "NONE"
conf.nginx_http_upstream_keepalive = "NONE"

else
conf.nginx_upstream_keepalive = tostring(conf.upstream_keepalive)
conf.nginx_http_upstream_keepalive = tostring(conf.upstream_keepalive)
end
end
end

-- nginx_upstream_keepalive -> upstream_keepalive_pool_size
if conf.upstream_keepalive_pool_size == nil then
if conf.nginx_upstream_keepalive ~= nil then
if conf.nginx_upstream_keepalive == "NONE" then
conf.upstream_keepalive_pool_size = 0

else
conf.upstream_keepalive_pool_size = tonumber(conf.nginx_upstream_keepalive)
end
end
end

-- nginx_http_upstream_keepalive_requests -> nginx_upstream_keepalive_requests
if conf.nginx_upstream_keepalive_requests == nil then
conf.nginx_upstream_keepalive_requests = conf.nginx_http_upstream_keepalive_requests
end

-- nginx_upstream_keepalive_requests -> upstream_keepalive_max_requests
if conf.upstream_keepalive_max_requests == nil
and conf.nginx_upstream_keepalive_requests ~= nil
then
conf.upstream_keepalive_max_requests = tonumber(conf.nginx_upstream_keepalive_requests)
end

-- nginx_http_upstream_keepalive_timeout -> nginx_upstream_keepalive_timeout
if conf.nginx_upstream_keepalive_timeout == nil then
conf.nginx_upstream_keepalive_timeout = conf.nginx_http_upstream_keepalive_timeout
end
--
-- nginx_upstream_keepalive_timeout -> upstream_keepalive_idle_timeout
if conf.upstream_keepalive_idle_timeout == nil
and conf.nginx_upstream_keepalive_timeout ~= nil
then
conf.upstream_keepalive_idle_timeout =
utils.nginx_conf_time_to_seconds(conf.nginx_upstream_keepalive_timeout)
end
end


-- By default, all properties in the configuration are considered to
-- be strings/numbers, but if we want to forcefully infer their type, specify it
-- in this table.
Expand Down Expand Up @@ -233,53 +294,66 @@ local CONF_INFERENCES = {
replacement = "nginx_main_worker_processes",
},
},
upstream_keepalive = { -- TODO: remove since deprecated in 1.3

-- TODO: remove since deprecated in 1.3
upstream_keepalive = {
typ = "number",
deprecated = {
replacement = "nginx_upstream_keepalive",
alias = function(conf)
if tonumber(conf.upstream_keepalive) == 0 then
conf.nginx_upstream_keepalive = "NONE"
replacement = "upstream_keepalive_pool_size",
alias = upstream_keepalive_deprecated_properties,
}
},

elseif conf.nginx_upstream_keepalive == nil then
conf.nginx_upstream_keepalive = tostring(conf.upstream_keepalive)
end
end,
-- TODO: remove since deprecated in 2.0
nginx_http_upstream_keepalive = {
typ = "string",
deprecated = {
replacement = "upstream_keepalive_pool_size",
alias = upstream_keepalive_deprecated_properties,
}
},
nginx_http_upstream_keepalive = { -- TODO: remove since deprecated in 2.0
nginx_http_upstream_keepalive_requests = {
typ = "string",
deprecated = {
replacement = "nginx_upstream_keepalive",
alias = function(conf)
if conf.nginx_upstream_keepalive == nil then
conf.nginx_upstream_keepalive = tostring(conf.nginx_http_upstream_keepalive)
end
end,
replacement = "upstream_keepalive_max_requests",
alias = upstream_keepalive_deprecated_properties,
}
},
nginx_http_upstream_keepalive_timeout = { -- TODO: remove since deprecated in 2.0
nginx_http_upstream_keepalive_timeout = {
typ = "string",
deprecated = {
replacement = "nginx_upstream_keepalive_timeout",
alias = function(conf)
if conf.nginx_upstream_keepalive_timeout == nil then
conf.nginx_upstream_keepalive_timeout = tostring(conf.nginx_http_upstream_keepalive_timeout)
end
end,
replacement = "upstream_keepalive_idle_timeout",
alias = upstream_keepalive_deprecated_properties,
}
},
nginx_http_upstream_keepalive_requests = { -- TODO: remove since deprecated in 2.0

-- TODO: remove since deprecated in 2.1
nginx_upstream_keepalive = {
typ = "string",
deprecated = {
replacement = "nginx_upstream_keepalive_requests",
alias = function(conf)
if conf.nginx_upstream_keepalive_requests == nil then
conf.nginx_upstream_keepalive_requests = tostring(conf.nginx_http_upstream_keepalive_requests)
end
end,
replacement = "upstream_keepalive_pool_size",
alias = upstream_keepalive_deprecated_properties,
}
},
nginx_upstream_keepalive_requests = {
typ = "string",
deprecated = {
replacement = "upstream_keepalive_max_requests",
alias = upstream_keepalive_deprecated_properties,
}
},
nginx_upstream_keepalive_timeout = {
typ = "string",
deprecated = {
replacement = "upstream_keepalive_idle_timeout",
alias = upstream_keepalive_deprecated_properties,
}
},

upstream_keepalive_pool_size = { typ = "number" },
upstream_keepalive_max_requests = { typ = "number" },
upstream_keepalive_idle_timeout = { typ = "number" },

headers = { typ = "array" },
trusted_ips = { typ = "array" },
real_ip_header = {
Expand All @@ -296,14 +370,24 @@ local CONF_INFERENCES = {
},
client_max_body_size = {
typ = "string",
alias = {
deprecated = {
replacement = "nginx_http_client_max_body_size",
alias = function(conf)
if conf.nginx_http_client_max_body_size == nil then
conf.nginx_http_client_max_body_size = conf.client_max_body_size
end
end,
}
},
client_body_buffer_size = {
typ = "string",
alias = {
deprecated = {
replacement = "nginx_http_client_body_buffer_size",
alias = function(conf)
if conf.nginx_http_client_body_buffer_size == nil then
conf.nginx_http_client_body_buffer_size = conf.client_body_buffer_size
end
end,
}
},
error_default_type = { enum = {
Expand Down Expand Up @@ -873,6 +957,18 @@ local function check_and_infer(conf, opts)
end
end

if conf.upstream_keepalive_pool_size < 0 then
errors[#errors + 1] = "upstream_keepalive_pool_size must be 0 or greater"
end

if conf.upstream_keepalive_max_requests < 0 then
errors[#errors + 1] = "upstream_keepalive_max_requests must be 0 or greater"
end

if conf.upstream_keepalive_idle_timeout < 0 then
errors[#errors + 1] = "upstream_keepalive_idle_timeout must be 0 or greater"
end

return #errors == 0, errors[1], errors
end

Expand Down
3 changes: 3 additions & 0 deletions kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ status_ssl_cert_key = NONE
headers = server_tokens, latency_tokens
trusted_ips = NONE
error_default_type = text/plain
upstream_keepalive_pool_size = 60
upstream_keepalive_max_requests = 100
upstream_keepalive_idle_timeout = 60
nginx_main_daemon = on
nginx_main_user = nobody nobody
Expand Down
Loading

0 comments on commit 1385d52

Please sign in to comment.