Skip to content

Commit 0ee4d2f

Browse files
committed
fix: sort_route uses vars count as tiebreaker to match max vars
1 parent 8166eea commit 0ee4d2f

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

lib/resty/radixtree.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ local mt = { __index = _M, __gc = gc_free }
205205

206206
local function sort_route(route_a, route_b)
207207
if route_a.priority == route_b.priority then
208+
if #route_a.path_org == #route_b.path_org then
209+
return (route_a.vars_len or 0) > (route_b.vars_len or 0)
210+
end
208211
return #route_a.path_org > #route_b.path_org
209212
end
210213
return (route_a.priority or 0) > (route_b.priority or 0)
@@ -438,6 +441,7 @@ local function common_route_data(path, route, route_opts, global_opts)
438441
error("failed to handle expression: " .. err, 2)
439442
end
440443
route_opts.vars = route_expr
444+
route_opts.vars_len = #route.vars
441445
end
442446

443447
local hosts = route.hosts

t/vars.t

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,38 @@ metadata /aa
566566
metadata /aa
567567
nil
568568
nil
569+
570+
571+
572+
=== TEST 20: match max vars condition
573+
--- config
574+
location /t {
575+
content_by_lua_block {
576+
local radix = require("resty.radixtree")
577+
local rx = radix.new({
578+
{
579+
paths = {"/aa"},
580+
metadata = "metadata /aa",
581+
vars = {
582+
{"arg_k", "==", "v"},
583+
},
584+
},
585+
{
586+
paths = {"/aa"},
587+
metadata = "metadata /aa2",
588+
vars = {
589+
{"arg_k", "==", "v"},
590+
{"arg_k2", "==", "v2"},
591+
},
592+
},
593+
})
594+
595+
ngx.say(rx:match("/aa", {vars = ngx.var}))
596+
}
597+
}
598+
--- request
599+
GET /t?k=v&k2=v2
600+
--- no_error_log
601+
[error]
602+
--- response_body
603+
metadata /aa2

0 commit comments

Comments
 (0)