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

perf(router): reuse atc context in router instance #12258

Merged
merged 8 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .requirements
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LUA_KONG_NGINX_MODULE=4fbc3ddc7dcbc706ed286b95344f3cb6da17e637 # 0.8.0
LUA_RESTY_LMDB=19a6da0616db43baf8197dace59e64244430b3c4 # 1.4.1
LUA_RESTY_EVENTS=8448a92cec36ac04ea522e78f6496ba03c9b1fd8 # 0.2.0
LUA_RESTY_WEBSOCKET=60eafc3d7153bceb16e6327074e0afc3d94b1316 # 0.4.0
ATC_ROUTER=95f1d8fe10b244bba5b354e5ed3726442373325e # 1.4.0
ATC_ROUTER=ac71b24ea5556b38b0f9903850ed666c36ad7843 # 1.4.1

KONG_MANAGER=nightly
NGX_WASM_MODULE=b9037acf7fa2d6f9ff02898bfc05544a1edc1fad
Expand Down
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/atc_reuse_context.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "Reuse match copntext between requests to avoid frequent memory allocation/deallocation"
type: performance
scope: Core
2 changes: 1 addition & 1 deletion changelog/unreleased/kong/bump-atc-router.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
message: Bumped atc-router from 1.2.0 to 1.4.0
message: Bumped atc-router from 1.2.0 to 1.4.1
type: dependency
scope: Core
13 changes: 9 additions & 4 deletions kong/router/atc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local _MT = { __index = _M, }

local buffer = require("string.buffer")
local schema = require("resty.router.schema")
local context = require("resty.router.context")
local router = require("resty.router.router")
local lrucache = require("resty.lrucache")
local tb_new = require("table.new")
Expand Down Expand Up @@ -35,7 +36,7 @@ local check_select_params = utils.check_select_params
local get_service_info = utils.get_service_info
local route_match_stat = utils.route_match_stat
local get_cache_key = fields.get_cache_key
local get_atc_context = fields.get_atc_context
local fill_atc_context = fields.fill_atc_context


local DEFAULT_MATCH_LRUCACHE_SIZE = utils.DEFAULT_MATCH_LRUCACHE_SIZE
Expand Down Expand Up @@ -223,7 +224,7 @@ local function new_from_scratch(routes, get_exp_and_priority)
local fields = inst:get_fields()

return setmetatable({
schema = CACHED_SCHEMA,
context = context.new(CACHED_SCHEMA),
router = inst,
routes = routes_t,
services = services_t,
Expand Down Expand Up @@ -412,7 +413,9 @@ function _M:matching(params)
params.host = host
params.port = port

local c, err = get_atc_context(self.schema, self.fields, params)
self.context:reset()

local c, err = fill_atc_context(self.context, self.fields, params)

if not c then
return nil, err
Expand Down Expand Up @@ -552,7 +555,9 @@ function _M:matching(params)
params.dst_ip, params.dst_port,
sni)

local c, err = get_atc_context(self.schema, self.fields, params)
self.context:reset()

local c, err = fill_atc_context(self.context, self.fields, params)
if not c then
return nil, err
end
Expand Down
7 changes: 3 additions & 4 deletions kong/router/fields.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local buffer = require("string.buffer")
local context = require("resty.router.context")


local type = type
Expand Down Expand Up @@ -288,8 +287,8 @@ local function get_cache_key(fields, params, ctx)
end


local function get_atc_context(schema, fields, params)
local c = context.new(schema)
local function fill_atc_context(context, fields, params)
local c = context

local res, err =
fields_visitor(fields, params, nil, function(field, value)
Expand Down Expand Up @@ -354,7 +353,7 @@ end

return {
get_cache_key = get_cache_key,
get_atc_context = get_atc_context,
fill_atc_context = fill_atc_context,

_set_ngx = _set_ngx,
}
Loading