iresty / lua-resty-libr3 Goto Github PK
View Code? Open in Web Editor NEWHigh-performance path dispatching library base on libr3 for OpenResty
License: Apache License 2.0
High-performance path dispatching library base on libr3 for OpenResty
License: Apache License 2.0
I changed first line to:
INST_PREFIX ?= /usr/local/openresty/luajit
and still get:
root@win10:~/lua-resty-libr3# make install
install -d /usr/local/openresty/luajit/share/lua/5.1/resty/
install lib/resty/*.lua /usr/local/openresty/luajit/share/lua/5.1/resty/
install libr3.so /usr/local/openresty/luajit/lib/lua/5.1/
install: cannot stat 'libr3.so': No such file or directory
Makefile:71: recipe for target 'install' failed
make: *** [install] Error 1
seems 'libr3.so
. so I tried:
root@win10:~/lua-resty-libr3# gcc r3_resty.c
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccaph0UW.o: In function `r3_mem_alloc':
r3_resty.c:(.text+0x29): undefined reference to `r3_fatal'
/tmp/ccaph0UW.o: In function `r3_create':
r3_resty.c:(.text+0x44): undefined reference to `r3_tree_create'
/tmp/ccaph0UW.o: In function `r3_free':
r3_resty.c:(.text+0x6d): undefined reference to `r3_tree_free'
/tmp/ccaph0UW.o: In function `r3_insert':
r3_resty.c:(.text+0xbc): undefined reference to `r3_tree_insert_routel_ex'
/tmp/ccaph0UW.o: In function `r3_compile':
r3_resty.c:(.text+0x20f): undefined reference to `r3_tree_compile'
/tmp/ccaph0UW.o: In function `r3_match_entry_create':
r3_resty.c:(.text+0x244): undefined reference to `match_entry_createl'
/tmp/ccaph0UW.o: In function `r3_match_route':
r3_resty.c:(.text+0x2c1): undefined reference to `r3_tree_match_route'
/tmp/ccaph0UW.o: In function `r3_match_entry_free':
r3_resty.c:(.text+0x47c): undefined reference to `match_entry_free'
collect2: error: ld returned 1 exit status
When run this Lua script, it will cause the cpu usage to reach 100%.
https://github.com/membphis/lua-resty-r3/blob/master/benchmark/test_3_routes.lua
here is the flame graph:
-- add route
r3:get(host, uri, foo)
r3:insert_route(host, methods, uri, foo)
code
local r3 = require("resty.r3_resty").new()
local encode_json = require("cjson.safe").encode
function foo(params) -- foo handler
ngx.say("foo: ", encode_json(params))
end
-- routing
r3:get("/foo/{id}/{name}", foo)
-- don't forget!!!
r3:compile()
-- dispatch
local ok = r3:dispatch("/foo/a/b", "GET")
print(ok)
and get error:
undefined symbol: r3_tree_create
It there a concept of a middleware for this library? What if I want certain routes to be authenticated, and would like to have an authentication handler that runs for those endpoints before the main handler is invoked.
Hey there! Thank you for this awesome library.
I'm interested in using it in nginx as a router, but for what I'm looking to do, I don't want to invoke a route handler when a route is matched. All I want is to match a request to a route, and lookup that route's metadata.
An example in pseudocode:
local r3route = require "resty.r3"
local r3 = r3route.new({
{
path = [[/foo/{:\w+}/{:\w+}"]],
method = {"GET"},
metadata = { server: foo, datacenter: us-east }
},
{
path = [[/bar/{:\w+}/{:\w+}]],
host = "*.bar.com",
metadata = { server: bar, datacenter: us-east }
},
{
path = [[/alice/{:\w+}/{:\w+}]],
metadata = { server: alice, datacenter: us-west }
},
})
-- match_route is imaginary name, similar API to r3:dispatch
local metadata = r3:match_route("GET", "/alice/one/two")
metadata.server => alice
metadata.datacenter => us-west
I think this is very similar to existing API, with one difference in handler vs. metadata.
I'd be happy to contribute a patch once other contributors are onboard 👍
cc @membphis
When I access the path in r3-tree with illegal methods or some new methods except methods below, it response with http code 500.
If the path accessed is not in r3-tree, it works well.
-- r3.lua
local _METHODS = {
GET = _METHOD_GET,
POST = _METHOD_POST,
PUT = _METHOD_PUT,
DELETE = _METHOD_DELETE,
PATCH = _METHOD_PATCH,
HEAD = _METHOD_HEAD,
OPTIONS = _METHOD_OPTIONS,
}
demo:
worker_processes 1;
error_log logs/error.log debug;
pid logs/nginx.pid;
events {
use epoll;
}
http {
include mime.types;
default_type application/json;
access_log off;
lua_package_path '${prefix}/src/?.lua;${prefix}/src/lib/?.lua;;';
lua_package_cpath '${prefix}/src/lib/?.so;;';
server {
listen 8000 reuseport;
server_name localhost;
lua_code_cache on;
location / {
content_by_lua '
local r3 = require "resty.r3"
local function foo()
ngx.say("foo")
end
local router = r3.new({
{
path = [[/foo]],
method = {"GET"},
handler = foo
}
})
router:compile()
local ok = router:dispatch(ngx.var.uri, ngx.req.get_method())
if not ok then
ngx.log(ngx.ERR, "uri cannot be matched: ", ngx.var.uri)
ngx.exit(404)
end
';
}
}
}
shell
curl -X MOVE localhost:8000/foo
it comes to 500.
error.log
2019/08/23 07:30:58 [error] 6#6: *4 lua entry thread aborted: runtime error: /usr/local/openresty/nginx//src/lib/resty/r3.lua:468: bad argument #2 to 'band' (number expected, got nil)
stack traceback:
coroutine 0:
[C]: in function 'band'
/usr/local/openresty/nginx//src/lib/resty/r3.lua:468: in function 'match_by_path_cache'
/usr/local/openresty/nginx//src/lib/resty/r3.lua:505: in function 'dispatch2'
/usr/local/openresty/nginx//src/lib/resty/r3.lua:547: in function 'dispatch'
content_by_lua(nginx.conf:45):15: in main chunk, client: 127.0.0.1, server: localhost, request: "MOVE /foo HTTP/1.1", host: "localhost:8000"
for example ubuntu, centos, mac, fedora, bsd...
It happened several times but I cannot recover this exactly scence.
warning: core file may not match specified executable file.
[New LWP 6]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `nginx: worker process is shutting'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 r3_route_attribute_free (router=0x7fc1dbc21170) at r3_resty.c:70
70 r3_resty.c: No such file or directory.
(gdb) bt
#0 r3_route_attribute_free (router=0x7fc1dbc21170) at r3_resty.c:70
#1 0x00007fc1da104046 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#2 0x00007fc1da14800c in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#3 0x00007fc1da15d137 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#4 0x00007fc1da101a75 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#5 0x00007fc1da1041c3 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#6 0x00007fc1da104297 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#7 0x00007fc1da105348 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#8 0x00007fc1da10c0c1 in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#9 0x00007fc1da101e6b in ?? () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#10 0x00007fc1da10c3e2 in lua_close () from /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
#11 0x00007fc1dad0e14c in ngx_http_lua_cleanup_vm ()
#12 0x00007fc1dac35536 in ngx_destroy_pool ()
#13 0x00007fc1dac5b1a3 in ?? ()
#14 0x00007fc1dac5b327 in ?? ()
#15 0x00007fc1dac59ac8 in ngx_spawn_process ()
#16 0x00007fc1dac5b770 in ?? ()
---Type <return> to continue, or q <return> to quit---
#17 0x00007fc1dac5c1bd in ngx_master_process_cycle ()
#18 0x00007fc1dac32dfd in main ()
Hey, thanks for this library!
I want to ask a question, I want to routing of the URL is indefinite the length,such as: ip:7500/v1/foo/dir/test/......../test . There may be more levels. So I used regular matching, but.
code:
local r3route = require "resty.r3"
local router_uri =
{
{
path = "/v1/foo/{get_filename}",
method = {"GET"},
handler = main_controllers_get
},
{
path = [[/v1/bar/{bucketname}/{get_filename:([\w-./])}]],
method = {"GET"},
handler = main_controllers_get
},
{
path = "/v1/foo",
method = {"POST"},
handler = main_controllers_post
},
{
path = [[/v1/bar/{bucketname}/{post_filename:([\w-./])}]],
method = {"POST"},
handler = main_controllers_post
}
}
local r3 = r3route.new(router_uri)
r3:compile()
local ok = r3:dispatch(ngx.var.uri , ngx.req.get_method())
if not ok then
ngx.exit(403)
end
How do you solve this problem?
cc -O3 -g -Wall -fpic -DBUILDING_SO -c r3_resty.c
cc -shared -fvisibility=hidden r3_resty.o r3/.libs/libr3.a -o libr3.so
Undefined symbols for architecture x86_64:
"_pcre_compile", referenced from:
_r3_tree_compile_patterns in libr3.a(node.o)
"_pcre_exec", referenced from:
_r3_tree_matchl in libr3.a(node.o)
"_pcre_free", referenced from:
_r3_tree_free in libr3.a(node.o)
_r3_tree_compile_patterns in libr3.a(node.o)
"_pcre_free_study", referenced from:
_r3_tree_free in libr3.a(node.o)
_r3_tree_compile_patterns in libr3.a(node.o)
"_pcre_study", referenced from:
_r3_tree_compile_patterns in libr3.a(node.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [libr3.so] Error 1
Hello. I do as README say but:
root@win10:~/lua-resty-libr3# make install
install -d /usr/share/lua/5.1/resty/
install lib/resty/*.lua /usr/share/lua/5.1/resty/
install libr3.so /usr/lib/lua/5.1/
install: target '/usr/lib/lua/5.1/' is not a directory: No such file or directory
Makefile:71: recipe for target 'install' failed
make: *** [install] Error 1
My openresty install path is /usr/local/openresty
https://github.com/iresty/lua-resty-radixtree#new
paths
, hosts
, remote_addrs
and vars
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.