stream-lua-nginx-module
stream-lua-nginx-module copied to clipboard
cross modules stream/http shared dict
This addresses issue openresty/stream-lua-nginx-module#25 - cross modules shared dict. Tested (including adding/removing dicts and reload) but tests still need to be added.
I'v covered different ways to do this. Most were either over complicated or/and risky.
This one is quite simple - a module references the other module zones only during post-configuration phase. It works as it relies on the fact (to my best knowledge) that across all pushed dict functions, zone->data
is ngx_stream_lua_shdict_ctx_t
or ngx_http_lua_shdict_ctx_t
which are cast-interchangeable (they are aligned in the attributes that they are referenced to).
The single non compatible attribute is ctx->main_conf
but it's only accessed during configuration phase where each module takes care of it's own.
Another big advantage is that it works without any changes to lua_ngx_module
. Until http module adopts the change, a dict should be configured in http context for this to work.
This is of course not ideal but it's by far the simplest way.
If possible, it would be best to make these structs fully aligned or even better including a common header file for this. Another TODO is "selectively sharing dicts".
test configuration attached nginx.txt
@alonbg BTW, the Travis CI testing is broken on this PR, as evidenced below.
@agentzh thanks for taking the time to comment. The other changes are simply a miserable rebase out of place. I'll rewind. I agree with your approach. Ill see what I can do.
2 or more share dict, when reload nginx, some dict lost .
17 http {
18 lua_shared_dict http_dict 1m;
19 lua_shared_dict http_dict1 1m;
20
21 server {
22 listen 8888;
23
24 location /udp {
25 content_by_lua_block {
26 local http_dict = ngx.shared.http_dict
27 http_dict:set("udp", ngx.var.uri)
28 local stream = http_dict:get("stream")
29 if stream == nil then
30 ngx.say("udp OK")
31 else
32 ngx.say("udp ok " .. stream)
33 end
34 }
35 }
36 }
37 }
38
39 stream {
40 lua_shared_dict stream_dict1 1m;
41 lua_shared_dict stream_dict2 1m;
42
43 server {
44 listen 127.0.0.1:9999;
45 content_by_lua_block {
46 local http_dict = ngx.shared["http_dict"]
47 local http_dict1 = ngx.shared["http_dict1"]
48 http_dict1:set("test", "test")
49 -- ngx.log(ngx.ERR, "get http shdict = " .. http_dict:get("udp"))
50 http_dict:set("stream", "xxxx")
51 }
52 }
telnet localhost 9999 ./nginx -s reload telnet localhost 9999 so the secound http_dict1 nil.
We need a separate ngx_meta_lua_module for this. It's wrong to do such things in either ngx_stream_lua_module or ngx_http_lua_module. Either way is wrong and fragile.
@agentzh, I'm convinced btw. I'll start working on ngx_meta_lua_module when I find the time. Thanks
What is the status on the ngx_meta_lua_module?
Here is my alternative proposal to this feature: https://github.com/openresty/meta-lua-nginx-module/pull/76