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: optimizing performance of router match with lrucache #8102

Merged
merged 11 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
106 changes: 106 additions & 0 deletions apisix/core/improve.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
--
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
-- Licensed to the Apache Software Foundation (ASF) under one or more
-- contributor license agreements. See the NOTICE file distributed with
-- this work for additional information regarding copyright ownership.
-- The ASF licenses this file to You under the Apache License, Version 2.0
-- (the "License"); you may not use this file except in compliance with
-- the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
local require = require
local core = require("apisix.core")
local ipairs = ipairs
local type = type

local route_lrucache = core.lrucache.new({
count = 512
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
})

local enable_route_cache

local _M = {}

local orig_router_match
local router

local function match_route(ctx)
orig_router_match(ctx)
-- replace the router match
return ctx.matched_route or false
end


local function ai_match(ctx)
local route_ckey = ctx.var.uri .. "-" .. ctx.var.method .. "-" ..
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
ctx.var.host .. "-" .. ctx.var.remote_addr
local ver = router.user_routes.conf_version
local route_cache = route_lrucache(route_ckey, ver,
match_route, ctx)
-- if the version has not changed, use the cached route
if route_cache then
ctx.matched_route = route_cache
end
end


function _M.routes_analyze(routes)
local vars_flag = false
local filter_flag = false
local prefix_match_flag = false

for _, route in ipairs(routes) do
if type(route) == "table" then
if route.vars then
vars_flag = true
break
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
end

if route.filter_fun then
filter_flag = true
break
end

if route.uri and core.string.has_suffix(route.uri, "*") then
prefix_match_flag = true
break
end

if route.uris then
for _, uri in ipairs(route.uris) do
if core.string.has_suffix(uri, "*") then
prefix_match_flag = true
break
end
end
end
end
end

if vars_flag or filter_flag or prefix_match_flag then
enable_route_cache = false
router.match = orig_router_match
else
enable_route_cache = true
router.match = ai_match
end
end


function _M.enable_route_cache()
tzssangglass marked this conversation as resolved.
Show resolved Hide resolved
return enable_route_cache
end


function _M.init_worker(router_http)
router = router_http
orig_router_match = router.match
end

return _M
2 changes: 2 additions & 0 deletions apisix/http/route.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local service_fetch = require("apisix.http.service").get
local core = require("apisix.core")
local expr = require("resty.expr.v1")
local plugin_checker = require("apisix.plugin").plugin_checker
local routes_analyze = require("apisix.core.improve").routes_analyze
local ipairs = ipairs
local type = type
local error = error
Expand Down Expand Up @@ -91,6 +92,7 @@ function _M.create_radixtree_uri_router(routes, uri_routes, with_parameter)
end
end

routes_analyze(uri_routes)
core.log.info("route items: ", core.json.delay_encode(uri_routes, true))

if with_parameter then
Expand Down
2 changes: 2 additions & 0 deletions apisix/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local http_route = require("apisix.http.route")
local apisix_upstream = require("apisix.upstream")
local core = require("apisix.core")
local plugin_checker = require("apisix.plugin").plugin_checker
local improve_init = require("apisix.core.improve").init_worker
local str_lower = string.lower
local error = error
local ipairs = ipairs
Expand Down Expand Up @@ -83,6 +84,7 @@ function _M.http_init_worker()

local router_http = require("apisix.http.router." .. router_http_name)
attach_http_router_common_methods(router_http)
improve_init(router_http)
router_http.init_worker(filter)
_M.router_http = router_http

Expand Down
Loading