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

optimize: cache reversed host string. #21

Merged
merged 1 commit into from
Oct 10, 2019
Merged
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
39 changes: 26 additions & 13 deletions lib/resty/radixtree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,29 @@ local function pre_insert_route(self, path, route)
if type(hosts) == "table" and #hosts > 0 then
route_opts.hosts = {}
for _, h in ipairs(hosts) do
local host_is_wildcard = false
local is_wildcard = false
if h and h:sub(1, 1) == '*' then
host_is_wildcard = true
is_wildcard = true
h = h:sub(2):reverse()
else
h = h:reverse()
end

insert_tab(route_opts.hosts, host_is_wildcard)
insert_tab(route_opts.hosts, is_wildcard)
insert_tab(route_opts.hosts, h)
end

elseif type(hosts) == "string" then
local host_is_wildcard = false
if hosts and hosts:sub(1, 1) == '*' then
host_is_wildcard = true
hosts = hosts:sub(2):reverse()
local is_wildcard = false
local host = hosts
if host:sub(1, 1) == '*' then
is_wildcard = true
host = host:sub(2):reverse()
else
host = host:reverse()
end

route_opts.hosts = {host_is_wildcard, hosts}
route_opts.hosts = {is_wildcard, host}
end

local uris = route.uris
Expand Down Expand Up @@ -350,7 +355,7 @@ local function match_host(route_host_is_wildcard, route_host, request_host)
return route_host == request_host
end

local i = request_host:reverse():find(route_host, 1, true)
local i = request_host:find(route_host, 1, true)
if i ~= 1 then
return false
end
Expand Down Expand Up @@ -432,11 +437,19 @@ local function match_route_opts(route, opts)
-- log_info("route.hosts: ", type(route.hosts))
if route.hosts then
local matched = false

if opts.host and not opts.host_reversed then
opts.host_reversed = opts.host:reverse()
end

local hosts = route.hosts
for i = 1, #hosts, 2 do
if match_host(hosts[i], hosts[i + 1], opts.host) then
matched = true
break
local reverse_host = opts.host_reversed
if reverse_host then
for i = 1, #hosts, 2 do
if match_host(hosts[i], hosts[i + 1], reverse_host) then
matched = true
break
end
end
end

Expand Down