Skip to content

Commit 6c7d96f

Browse files
committed
feature: added the ngx.configure module and APIs.
1 parent a0a404f commit 6c7d96f

File tree

5 files changed

+1017
-2
lines changed

5 files changed

+1017
-2
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ install:
6161
- git clone https://github.com/openresty/openresty.git ../openresty
6262
- git clone https://github.com/openresty/openresty-devel-utils.git
6363
- git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module
64-
- git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module
64+
- git clone -b feat/configure-by-lua https://github.com/thibaultcha/lua-nginx-module.git ../lua-nginx-module
6565
- git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx
6666
- git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module
6767
- git clone https://github.com/openresty/lua-resty-lrucache.git

lib/ngx/configure.lua

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
-- Copyright (C) Yichun Zhang (agentzh)
2+
--
3+
-- Author: Thibault Charbonnier (thibaultcha)
4+
5+
6+
local base = require "resty.core.base"
7+
base.allows_subsystem("http", "stream")
8+
9+
10+
local ffi = require "ffi"
11+
local C = ffi.C
12+
--local FFI_OK = base.FFI_OK
13+
local FFI_ERROR = base.FFI_ERROR
14+
local FFI_DECLINED = base.FFI_DECLINED
15+
local ffi_str = ffi.string
16+
local get_string_buf = base.get_string_buf
17+
local get_size_ptr = base.get_size_ptr
18+
local find = string.find
19+
local type = type
20+
21+
22+
local ERR_BUF_SIZE = 128
23+
24+
25+
ffi.cdef [[
26+
unsigned int ngx_http_lua_ffi_is_configure_phase();
27+
28+
int ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name,
29+
ngx_str_t *size, unsigned char *errstr, size_t *errlen);
30+
31+
void ngx_http_lua_ffi_configure_max_pending_timers(int n_timers);
32+
33+
void ngx_http_lua_ffi_configure_max_running_timers(int n_timers);
34+
35+
int ngx_http_lua_ffi_configure_env(const unsigned char *value,
36+
size_t name_len, size_t len);
37+
]]
38+
39+
40+
local _M = { version = base.version }
41+
42+
43+
function _M.is_configure_phase()
44+
return C.ngx_http_lua_ffi_is_configure_phase() == 1
45+
end
46+
47+
48+
do
49+
local name_str_t = ffi.new("ngx_str_t[1]")
50+
local size_str_t = ffi.new("ngx_str_t[1]")
51+
52+
53+
function _M.shared_dict(name, size)
54+
if not _M.is_configure_phase() then
55+
error("API disabled in the current context", 2)
56+
end
57+
58+
if type(name) ~= "string" then
59+
error("name must be a string", 2)
60+
end
61+
62+
if type(size) ~= "string" then
63+
error("size must be a string", 2)
64+
end
65+
66+
local name_len = #name
67+
if name_len == 0 then
68+
error("invalid lua shared dict name", 2)
69+
end
70+
71+
local size_len = #size
72+
if size_len == 0 then
73+
error("invalid lua shared dict size", 2)
74+
end
75+
76+
local name_t = name_str_t[0]
77+
local size_t = size_str_t[0]
78+
79+
name_t.data = name
80+
name_t.len = name_len
81+
82+
size_t.data = size
83+
size_t.len = size_len
84+
85+
local err = get_string_buf(ERR_BUF_SIZE)
86+
local errlen = get_size_ptr()
87+
errlen[0] = ERR_BUF_SIZE
88+
89+
local rc = C.ngx_http_lua_ffi_configure_shared_dict(name_str_t,
90+
size_str_t, err,
91+
errlen)
92+
if rc == FFI_ERROR then
93+
error(ffi_str(err, errlen[0]))
94+
end
95+
96+
if rc == FFI_DECLINED then
97+
return nil, "exists"
98+
end
99+
100+
-- NGINX_OK/FFI_OK
101+
102+
return true
103+
end
104+
end
105+
106+
107+
function _M.max_pending_timers(n_timers)
108+
if not _M.is_configure_phase() then
109+
error("API disabled in the current context", 2)
110+
end
111+
112+
if type(n_timers) ~= "number" then
113+
error("n_timers must be a number", 2)
114+
end
115+
116+
if n_timers < 0 then
117+
error("n_timers must be positive", 2)
118+
end
119+
120+
C.ngx_http_lua_ffi_configure_max_pending_timers(n_timers)
121+
end
122+
123+
124+
function _M.max_running_timers(n_timers)
125+
if not _M.is_configure_phase() then
126+
error("API disabled in the current context", 2)
127+
end
128+
129+
if type(n_timers) ~= "number" then
130+
error("n_timers must be a number", 2)
131+
end
132+
133+
if n_timers < 0 then
134+
error("n_timers must be positive", 2)
135+
end
136+
137+
C.ngx_http_lua_ffi_configure_max_running_timers(n_timers)
138+
end
139+
140+
141+
function _M.env(value)
142+
if not _M.is_configure_phase() then
143+
error("API disabled in the current context", 2)
144+
end
145+
146+
if type(value) ~= "string" then
147+
error("value must be a string", 2)
148+
end
149+
150+
local len = #value
151+
local idx = find(value, "=")
152+
153+
local rc = C.ngx_http_lua_ffi_configure_env(value, idx and idx - 1 or len,
154+
len)
155+
if rc == FFI_ERROR then
156+
error("no memory")
157+
end
158+
end
159+
160+
161+
return _M

lib/resty/core/phase.lua

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local ffi = require 'ffi'
22
local base = require "resty.core.base"
3+
local ngx_configure = require "ngx.configure"
34

45
local C = ffi.C
56
local FFI_ERROR = base.FFI_ERROR
@@ -33,8 +34,13 @@ local context_names = {
3334
function ngx.get_phase()
3435
local r = getfenv(0).__ngx_req
3536

36-
-- if we have no request object, assume we are called from the "init" phase
37+
-- if we have no request object, assume we are called from the "init"
38+
-- or "configure" phase
3739
if not r then
40+
if ngx_configure.is_configure_phase() then
41+
return "configure"
42+
end
43+
3844
return "init"
3945
end
4046

lib/resty/core/shdict.lua

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
local ffi = require 'ffi'
55
local base = require "resty.core.base"
6+
local ngx_configure = require "ngx.configure"
67

78
local ffi_new = ffi.new
89
local ffi_str = ffi.string
@@ -71,6 +72,10 @@ local errmsg = base.get_errmsg_ptr()
7172

7273

7374
local function check_zone(zone)
75+
if ngx_configure.is_configure_phase() then
76+
error("API disabled in the context of configure_by_lua", 3)
77+
end
78+
7479
if not zone or type(zone) ~= "table" then
7580
return error("bad \"zone\" argument")
7681
end

0 commit comments

Comments
 (0)