Skip to content

Commit

Permalink
feat(router/traditional): increase lua_regex_cache_max_entries and …
Browse files Browse the repository at this point in the history
…show warning if it is set too small

Co-authored-by: Datong Sun <datong.sun@konghq.com>
  • Loading branch information
mayocream and dndx authored Nov 1, 2022
1 parent 541d08e commit 7b384d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@

- Data plane's connection to control plane is moved to a privileged worker process
[#9432](https://github.com/Kong/kong/pull/9432)
- Increase the default value of `lua_regex_cache_max_entries`, a warning will be thrown
when there are too many regex routes and `router_flavor` is `traditional`.
[#9624](https://github.com/Kong/kong/pull/9624)

### Fixes

Expand Down
5 changes: 5 additions & 0 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,11 @@
# at worst any regex Kong executes could finish within
# roughly 2 seconds.

#nginx_http_lua_regex_cache_max_entries = 8192 # Specifies the maximum number of entries allowed
# in the worker process level compiled regex cache.
# It is recommended to set it to at least (number of regex paths * 2)
# to avoid high CPU usages.

#------------------------------------------------------------------------------
# DATASTORE
#------------------------------------------------------------------------------
Expand Down
16 changes: 16 additions & 0 deletions kong/router/traditional.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ local re_find = ngx.re.find
local header = ngx.header
local var = ngx.var
local ngx_log = ngx.log
local worker_id = ngx.worker.id
local concat = table.concat
local sort = table.sort
local byte = string.byte
Expand Down Expand Up @@ -208,6 +209,7 @@ local MAX_REQ_HEADERS = 100

local match_route
local reduce
local lua_regex_cache_max_entries


local function _set_ngx(mock_ngx)
Expand Down Expand Up @@ -1448,6 +1450,20 @@ function _M.new(routes, cache, cache_neg)
local match_sources = not isempty(plain_indexes.sources)
local match_destinations = not isempty(plain_indexes.destinations)

-- warning about the regex cache size being too small
if not lua_regex_cache_max_entries then
lua_regex_cache_max_entries = tonumber(kong.configuration.nginx_http_lua_regex_cache_max_entries) or 1024
end

if worker_id() == 0 and regex_uris[0] * 2 > lua_regex_cache_max_entries then
ngx_log(WARN, "the 'nginx_http_lua_regex_cache_max_entries' setting is set to ",
lua_regex_cache_max_entries,
" but there are ", regex_uris[0], " regex paths configured. ",
"This may lead to performance issue due to regex cache trashing. ",
"Consider increasing the 'nginx_http_lua_regex_cache_max_entries' ",
"to at least ", regex_uris[0] * 2)
end

local function find_route(req_method, req_uri, req_host, req_scheme,
src_ip, src_port,
dst_ip, dst_port,
Expand Down
1 change: 1 addition & 0 deletions kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ nginx_proxy_real_ip_recursive = off
nginx_admin_client_max_body_size = 10m
nginx_admin_client_body_buffer_size = 10m
nginx_http_lua_regex_match_limit = 100000
nginx_http_lua_regex_cache_max_entries = 8192
client_body_buffer_size = 8k
real_ip_header = X-Real-IP
Expand Down

0 comments on commit 7b384d6

Please sign in to comment.