Skip to content

Commit 728bfe9

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

File tree

5 files changed

+1009
-2
lines changed

5 files changed

+1009
-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

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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")
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_DECLINED then
93+
error(ffi_str(err, errlen[0]), 2)
94+
end
95+
96+
if rc == FFI_ERROR then
97+
error("no memory")
98+
end
99+
100+
-- NGINX_OK/FFI_OK
101+
end
102+
end
103+
104+
105+
function _M.max_pending_timers(n_timers)
106+
if not _M.is_configure_phase() then
107+
error("API disabled in the current context", 2)
108+
end
109+
110+
if type(n_timers) ~= "number" then
111+
error("n_timers must be a number", 2)
112+
end
113+
114+
if n_timers < 0 then
115+
error("n_timers must be positive", 2)
116+
end
117+
118+
C.ngx_http_lua_ffi_configure_max_pending_timers(n_timers)
119+
end
120+
121+
122+
function _M.max_running_timers(n_timers)
123+
if not _M.is_configure_phase() then
124+
error("API disabled in the current context", 2)
125+
end
126+
127+
if type(n_timers) ~= "number" then
128+
error("n_timers must be a number", 2)
129+
end
130+
131+
if n_timers < 0 then
132+
error("n_timers must be positive", 2)
133+
end
134+
135+
C.ngx_http_lua_ffi_configure_max_running_timers(n_timers)
136+
end
137+
138+
139+
function _M.env(value)
140+
if not _M.is_configure_phase() then
141+
error("API disabled in the current context", 2)
142+
end
143+
144+
if type(value) ~= "string" then
145+
error("value must be a string", 2)
146+
end
147+
148+
local len = #value
149+
local idx = find(value, "=")
150+
151+
local rc = C.ngx_http_lua_ffi_configure_env(value, idx and idx - 1 or len,
152+
len)
153+
if rc == FFI_ERROR then
154+
error("no memory")
155+
end
156+
end
157+
158+
159+
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)