Skip to content

Commit 85902f9

Browse files
committed
feature: implemented the new configure_by_lua phase.
1 parent a698cb0 commit 85902f9

11 files changed

+733
-57
lines changed

config

+2
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ HTTP_LUA_SRCS=" \
344344
$ngx_addon_dir/src/ngx_http_lua_semaphore.c\
345345
$ngx_addon_dir/src/ngx_http_lua_coroutine.c \
346346
$ngx_addon_dir/src/ngx_http_lua_bodyfilterby.c \
347+
$ngx_addon_dir/src/ngx_http_lua_configureby.c \
347348
$ngx_addon_dir/src/ngx_http_lua_initby.c \
348349
$ngx_addon_dir/src/ngx_http_lua_initworkerby.c \
349350
$ngx_addon_dir/src/ngx_http_lua_socket_udp.c \
@@ -405,6 +406,7 @@ HTTP_LUA_DEPS=" \
405406
$ngx_addon_dir/src/ngx_http_lua_semaphore.h\
406407
$ngx_addon_dir/src/ngx_http_lua_coroutine.h \
407408
$ngx_addon_dir/src/ngx_http_lua_bodyfilterby.h \
409+
$ngx_addon_dir/src/ngx_http_lua_configureby.h \
408410
$ngx_addon_dir/src/ngx_http_lua_initby.h \
409411
$ngx_addon_dir/src/ngx_http_lua_initworkerby.h \
410412
$ngx_addon_dir/src/ngx_http_lua_socket_udp.h \

src/ngx_http_lua_common.h

+5
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ typedef struct ngx_http_lua_balancer_peer_data_s
148148
typedef struct ngx_http_lua_sema_mm_s ngx_http_lua_sema_mm_t;
149149

150150

151+
typedef ngx_int_t (*ngx_http_lua_configure_handler_pt)(ngx_conf_t *cf,
152+
ngx_http_lua_main_conf_t *lmcf);
151153
typedef ngx_int_t (*ngx_http_lua_main_conf_handler_pt)(ngx_log_t *log,
152154
ngx_http_lua_main_conf_t *lmcf, lua_State *L);
153155
typedef ngx_int_t (*ngx_http_lua_srv_conf_handler_pt)(ngx_http_request_t *r,
@@ -198,6 +200,9 @@ struct ngx_http_lua_main_conf_s {
198200
ngx_flag_t postponed_to_rewrite_phase_end;
199201
ngx_flag_t postponed_to_access_phase_end;
200202

203+
ngx_http_lua_configure_handler_pt configure_handler;
204+
ngx_str_t configure_src;
205+
201206
ngx_http_lua_main_conf_handler_pt init_handler;
202207
ngx_str_t init_src;
203208

src/ngx_http_lua_configureby.c

+287
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*
5+
* Author: Thibault Charbonnier (thibaultcha)
6+
* I hereby assign copyright in this code to the lua-nginx-module project,
7+
* to be licensed under the same terms as the rest of the code.
8+
*/
9+
10+
11+
#ifndef DDEBUG
12+
#define DDEBUG 0
13+
#endif
14+
15+
16+
#include "ddebug.h"
17+
#include "ngx_http_lua_configureby.h"
18+
#include "ngx_http_lua_directive.h"
19+
#include "ngx_http_lua_util.h"
20+
#include "ngx_http_lua_shdict.h"
21+
#include "ngx_http_lua_api.h"
22+
23+
24+
static ngx_conf_t *cfp;
25+
26+
27+
char *
28+
ngx_http_lua_configure_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
29+
void *conf)
30+
{
31+
char *rv;
32+
ngx_conf_t save;
33+
34+
save = *cf;
35+
cf->handler = ngx_http_lua_configure_by_lua;
36+
cf->handler_conf = conf;
37+
38+
rv = ngx_http_lua_conf_lua_block_parse(cf, cmd);
39+
40+
*cf = save;
41+
42+
return rv;
43+
}
44+
45+
46+
char *
47+
ngx_http_lua_configure_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
48+
void *conf)
49+
{
50+
u_char *name;
51+
ngx_str_t *value;
52+
ngx_http_lua_main_conf_t *lmcf = conf;
53+
54+
if (cmd->post == NULL) {
55+
return NGX_CONF_ERROR;
56+
}
57+
58+
if (lmcf->configure_handler) {
59+
return "is duplicate";
60+
}
61+
62+
value = cf->args->elts;
63+
64+
if (value[1].len == 0) {
65+
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
66+
"invalid location config: no runnable Lua code");
67+
return NGX_CONF_ERROR;
68+
}
69+
70+
lmcf->configure_handler = (ngx_http_lua_configure_handler_pt) cmd->post;
71+
72+
if (cmd->post == ngx_http_lua_configure_handler_file) {
73+
name = ngx_http_lua_rebase_path(cf->pool, value[1].data,
74+
value[1].len);
75+
if (name == NULL) {
76+
return NGX_CONF_ERROR;
77+
}
78+
79+
lmcf->configure_src.data = name;
80+
lmcf->configure_src.len = ngx_strlen(name);
81+
82+
} else {
83+
lmcf->configure_src = value[1];
84+
}
85+
86+
return NGX_CONF_OK;
87+
}
88+
89+
90+
ngx_int_t
91+
ngx_http_lua_configure_handler_inline(ngx_conf_t *cf,
92+
ngx_http_lua_main_conf_t *lmcf)
93+
{
94+
int status;
95+
lua_State *L = lmcf->lua;
96+
97+
cfp = cf;
98+
99+
status = luaL_loadbuffer(L, (char *) lmcf->configure_src.data,
100+
lmcf->configure_src.len, "=configure_by_lua")
101+
|| ngx_http_lua_do_call(cf->log, L);
102+
103+
cfp = NULL;
104+
105+
return ngx_http_lua_report(cf->log, L, status, "configure_by_lua");
106+
}
107+
108+
109+
ngx_int_t
110+
ngx_http_lua_configure_handler_file(ngx_conf_t *cf,
111+
ngx_http_lua_main_conf_t *lmcf)
112+
{
113+
int status;
114+
lua_State *L = lmcf->lua;
115+
116+
cfp = cf;
117+
118+
status = luaL_loadfile(L, (char *) lmcf->configure_src.data)
119+
|| ngx_http_lua_do_call(cf->log, L);
120+
121+
cfp = NULL;
122+
123+
return ngx_http_lua_report(cf->log, L, status, "configure_by_lua_file");
124+
}
125+
126+
127+
ngx_uint_t
128+
ngx_http_lua_is_configure_phase()
129+
{
130+
return cfp != NULL;
131+
}
132+
133+
134+
#ifndef NGX_LUA_NO_FFI_API
135+
unsigned int
136+
ngx_http_lua_ffi_is_configure_phase()
137+
{
138+
return ngx_http_lua_is_configure_phase();
139+
}
140+
141+
142+
int
143+
ngx_http_lua_ffi_configure_shared_dict(ngx_str_t *name, ngx_str_t *size,
144+
u_char *errstr, size_t *err_len)
145+
{
146+
ssize_t ssize;
147+
ngx_shm_zone_t **zp;
148+
ngx_shm_zone_t *zone;
149+
ngx_http_lua_shdict_ctx_t *ctx;
150+
ngx_http_lua_main_conf_t *lmcf;
151+
lua_State *L;
152+
ngx_conf_t *cf = cfp;
153+
154+
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
155+
156+
ssize = ngx_parse_size(size);
157+
if (ssize <= NGX_HTTP_LUA_SHDICT_MINSIZE) {
158+
*err_len = ngx_snprintf(errstr, *err_len,
159+
"invalid lua shared dict size \"%s\"",
160+
size->data)
161+
- errstr;
162+
return NGX_ERROR;
163+
}
164+
165+
ctx = ngx_pcalloc(cf->cycle->pool, sizeof(ngx_http_lua_shdict_ctx_t));
166+
if (ctx == NULL) {
167+
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
168+
- errstr;
169+
return NGX_ERROR;
170+
}
171+
172+
ctx->name = *name;
173+
ctx->main_conf = lmcf;
174+
ctx->log = &cf->cycle->new_log;
175+
176+
zone = ngx_http_lua_shared_memory_add(cf, name, (size_t) ssize,
177+
&ngx_http_lua_module);
178+
if (zone == NULL) {
179+
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
180+
- errstr;
181+
return NGX_ERROR;
182+
}
183+
184+
if (zone->data) {
185+
return NGX_DECLINED;
186+
}
187+
188+
zone->init = ngx_http_lua_shdict_init_zone;
189+
zone->data = ctx;
190+
191+
if (lmcf->shdict_zones == NULL) {
192+
lmcf->shdict_zones = ngx_palloc(cf->pool, sizeof(ngx_array_t));
193+
if (lmcf->shdict_zones == NULL) {
194+
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
195+
- errstr;
196+
return NGX_ERROR;
197+
}
198+
199+
if (ngx_array_init(lmcf->shdict_zones, cf->pool, 2,
200+
sizeof(ngx_shm_zone_t *))
201+
!= NGX_OK)
202+
{
203+
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
204+
- errstr;
205+
return NGX_ERROR;
206+
}
207+
}
208+
209+
zp = ngx_array_push(lmcf->shdict_zones);
210+
if (zp == NULL) {
211+
*err_len = ngx_snprintf(errstr, *err_len, "no memory")
212+
- errstr;
213+
return NGX_ERROR;
214+
}
215+
216+
*zp = zone;
217+
218+
L = lmcf->lua;
219+
220+
lua_getglobal(L, "ngx");
221+
lua_getfield(L, -1, "shared");
222+
if (!lua_getmetatable(L, -1)) {
223+
ngx_http_lua_create_shdict_mt(L); /* ngx.shared shmt */
224+
}
225+
226+
ngx_http_lua_attach_shdict(L, name, zone);
227+
lua_pop(L, 2); /* pop: ngx.shared + shmt */
228+
229+
return NGX_OK;
230+
}
231+
232+
233+
void
234+
ngx_http_lua_ffi_configure_max_pending_timers(int n_timers)
235+
{
236+
237+
ngx_http_lua_main_conf_t *lmcf;
238+
ngx_conf_t *cf = cfp;
239+
240+
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
241+
242+
lmcf->max_pending_timers = (ngx_int_t) n_timers;
243+
}
244+
245+
246+
void
247+
ngx_http_lua_ffi_configure_max_running_timers(int n_timers)
248+
{
249+
250+
ngx_http_lua_main_conf_t *lmcf;
251+
ngx_conf_t *cf = cfp;
252+
253+
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_lua_module);
254+
255+
lmcf->max_running_timers = (ngx_int_t) n_timers;
256+
}
257+
258+
259+
int
260+
ngx_http_lua_ffi_configure_env(u_char *value, size_t name_len, size_t len)
261+
{
262+
ngx_core_conf_t *ccf;
263+
ngx_str_t *var;
264+
ngx_conf_t *cf = cfp;
265+
266+
ccf = (ngx_core_conf_t *) ngx_get_conf(cf->cycle->conf_ctx,
267+
ngx_core_module);
268+
269+
var = ngx_array_push(&ccf->env);
270+
if (var == NULL) {
271+
return NGX_ERROR;
272+
}
273+
274+
var->data = ngx_pnalloc(cf->pool, len);
275+
if (var->data == NULL) {
276+
return NGX_ERROR;
277+
}
278+
279+
(void) ngx_copy(var->data, value, len);
280+
var->len = name_len;
281+
282+
return NGX_OK;
283+
}
284+
#endif
285+
286+
287+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_lua_configureby.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*
5+
* Author: Thibault Charbonnier (thibaultcha)
6+
* I hereby assign copyright in this code to the lua-nginx-module project,
7+
* to be licensed under the same terms as the rest of the code.
8+
*/
9+
10+
11+
#ifndef _NGX_HTTP_LUA_CONFIGUREBY_H_INCLUDED_
12+
#define _NGX_HTTP_LUA_CONFIGUREBY_H_INCLUDED_
13+
14+
15+
#include "ngx_http_lua_common.h"
16+
17+
18+
char *ngx_http_lua_configure_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
19+
void *conf);
20+
21+
char *ngx_http_lua_configure_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
22+
void *conf);
23+
24+
ngx_int_t ngx_http_lua_configure_handler_inline(ngx_conf_t *cf,
25+
ngx_http_lua_main_conf_t *lmcf);
26+
27+
ngx_int_t ngx_http_lua_configure_handler_file(ngx_conf_t *cf,
28+
ngx_http_lua_main_conf_t *lmcf);
29+
30+
ngx_uint_t ngx_http_lua_is_configure_phase();
31+
32+
33+
#endif /* _NGX_HTTP_LUA_CONFIGUREBY_H_INCLUDED_ */
34+
35+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_lua_directive.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ ngx_http_lua_shared_dict(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
109109

110110
size = ngx_parse_size(&value[2]);
111111

112-
if (size <= 8191) {
112+
if (size <= NGX_HTTP_LUA_SHDICT_MINSIZE) {
113113
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
114114
"invalid lua shared dict size \"%V\"", &value[2]);
115115
return NGX_CONF_ERROR;

0 commit comments

Comments
 (0)