在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):api7/lua-resty-radixtree开源软件地址(OpenSource Url):https://github.com/api7/lua-resty-radixtree开源编程语言(OpenSource Language):C 43.2%开源软件介绍(OpenSource Introduction):Table of ContentsNameThis is Lua implementation library base on FFI for rax. This project depends on lua-resty-ipmatcher and lua-resty-expr. This project has been working in microservices API gateway Apache APISIX. The project is open sourced by Shenzhen ZhiLiu Technology Co., Ltd. In addition to this open source version, our company also provides a more powerful and performing commercial version, and provides technical support. If you are interested in our commercial version, please contact us. Synopsis location / {
content_by_lua_block {
local radix = require("resty.radixtree")
local rx = radix.new({
{
paths = {"/aa", "/bb*", "/name/:name/*other"},
hosts = {"*.bar.com", "foo.com"},
methods = {"GET", "POST", "PUT"},
remote_addrs = {"127.0.0.1","192.168.0.0/16",
"::1", "fe80::/32"},
vars = {
{"arg_name", "==", "json"},
{"arg_weight", ">", 10},
},
filter_fun = function(vars, opts)
return vars["arg_name"] == "json"
end,
metadata = "metadata /bb",
}
})
-- try to match
local opts = {
host = "foo.com",
method = "GET",
remote_addr = "127.0.0.1",
vars = ngx.var,
}
ngx.say(rx:match("/aa", opts))
-- try to match and store the cached value
local opts = {
host = "foo.com",
method = "GET",
remote_addr = "127.0.0.1",
vars = ngx.var,
matched = {}
}
ngx.say(rx:match("/name/json/foo/bar/gloo", opts))
ngx.say("name: ", opts.matched.name, " other: ", opts.matched.other)
}
} Methodsnew
The routes is an array table, like The attributes of each element may contain these:
The
PathFull path matchlocal rx = radix.new({
{
paths = {"/aa", "/bb/cc", "/dd/ee/index.html"},
metadata = "metadata /aa",
},
{
paths = {"/gg"},
metadata = "metadata /gg",
},
{
paths = {"/index.html"},
metadata = "metadata /index.html",
},
}) Full path matching, allowing multiple paths to be specified at the same time. Prefix matchlocal rx = radix.new({
{
paths = {"/aa/*", "/bb/cc/*"},
metadata = "metadata /aa",
},
{
paths = {"/gg/*"},
metadata = "metadata /gg",
},
}) Path prefix matching, allowing multiple paths to be specified at the same time. Parameters in pathlocal rx = radix.new({
{
-- This handler will match /user/john but will not match /user/ or /user
paths = {"/user/:user"},
metadata = "metadata /user",
},
{
-- However, this one will match /user/john/ and also /user/john/send/data
paths = {"/user/:user/*action"},
metadata = "metadata action",
},
}) match
Matches the route by local metadata = rx:match(ngx.var.uri, {...}) dispatch
Matches the route by local ok = rx:dispatch(ngx.var.uri, {...}) InstallCompile and installmake install DEV ENVInstall Dependenciesmake deps BenchmarkWe wrote some simple benchmark scripts. Machine environment: MacBook Pro (16-inch, 2019), CPU 2.3 GHz Intel Core i9. $ make
cc -O2 -g -Wall -fpic -std=c99 -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -DBUILDING_SO -c src/rax.c -o src/rax.o
cc -O2 -g -Wall -fpic -std=c99 -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -DBUILDING_SO -c src/easy_rax.c -o src/easy_rax.o
cc -shared -fvisibility=hidden src/rax.o src/easy_rax.o -o librestyradixtree.so
$ make bench
resty -I=./lib -I=./deps/share/lua/5.1 benchmark/match-parameter.lua
matched res: 1
route count: 100000
match times: 10000000
time used : 3.1400001049042 sec
QPS : 3184713
each time : 0.31400001049042 ns
resty -I=./lib -I=./deps/share/lua/5.1 benchmark/match-prefix.lua
matched res: 500
route count: 100000
match times: 1000000
time used : 0.42700004577637 sec
QPS : 2341920
resty -I=./lib -I=./deps/share/lua/5.1 benchmark/match-static.lua
matched res: 500
route count: 100000
match times: 10000000
time used : 0.95000004768372 sec
QPS : 10526315
resty -I=./lib -I=./deps/share/lua/5.1 benchmark/match-hosts.lua
matched res: 500
route count: 1000
match times: 100000
time used : 0.60199999809265 sec
QPS : 166112
resty -I=./lib -I=./deps/share/lua/5.1 benchmark/match-wildcard-hosts.lua
matched res: 500
route count: 1000
match times: 50000
time used : 0.47900009155273 sec
QPS : 104384 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论