Skip to content

Commit fe748fb

Browse files
committed
feature: new ngx_meta_lua_module and 'lua {}' conf block.
1 parent a797491 commit fe748fb

11 files changed

+3075
-0
lines changed

src/meta/config

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
ngx_module_type=CORE
2+
ngx_module_name=ngx_meta_lua_module
3+
ngx_module_incs="$ngx_addon_dir"
4+
ngx_module_deps="$ngx_addon_dir/ddebug.h \
5+
$ngx_addon_dir/ngx_meta_lua_api.h \
6+
$ngx_addon_dir/ngx_meta_lua_module.h \
7+
$ngx_addon_dir/ngx_meta_lua_shdict.h"
8+
ngx_module_srcs="$ngx_addon_dir/ngx_meta_lua_module.c \
9+
$ngx_addon_dir/ngx_meta_lua_shdict.c"
10+
ngx_module_libs=
11+
ngx_module_link=ADDON
12+
13+
. auto/module

src/meta/ddebug.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef _DDEBUG_H_INCLUDED_
8+
#define _DDEBUG_H_INCLUDED_
9+
10+
11+
#include <ngx_config.h>
12+
#include <nginx.h>
13+
#include <ngx_core.h>
14+
15+
16+
#if defined(DDEBUG) && (DDEBUG)
17+
18+
# if (NGX_HAVE_VARIADIC_MACROS)
19+
20+
# define dd(...) fprintf(stderr, "lua *** %s: ", __func__); \
21+
fprintf(stderr, __VA_ARGS__); \
22+
fprintf(stderr, " at %s line %d.\n", __FILE__, __LINE__)
23+
24+
# else
25+
26+
#include <stdarg.h>
27+
#include <stdio.h>
28+
29+
#include <stdarg.h>
30+
31+
static ngx_inline void
32+
dd(const char *fmt, ...) {
33+
}
34+
35+
# endif
36+
37+
#else
38+
39+
# if (NGX_HAVE_VARIADIC_MACROS)
40+
41+
# define dd(...)
42+
43+
# else
44+
45+
#include <stdarg.h>
46+
47+
static ngx_inline void
48+
dd(const char *fmt, ...) {
49+
}
50+
51+
# endif
52+
53+
#endif
54+
55+
#if defined(DDEBUG) && (DDEBUG)
56+
57+
#define dd_check_read_event_handler(r) \
58+
dd("r->read_event_handler = %s", \
59+
r->read_event_handler == ngx_http_block_reading ? \
60+
"ngx_http_block_reading" : \
61+
r->read_event_handler == ngx_http_test_reading ? \
62+
"ngx_http_test_reading" : \
63+
r->read_event_handler == ngx_http_request_empty_handler ? \
64+
"ngx_http_request_empty_handler" : "UNKNOWN")
65+
66+
#define dd_check_write_event_handler(r) \
67+
dd("r->write_event_handler = %s", \
68+
r->write_event_handler == ngx_http_handler ? \
69+
"ngx_http_handler" : \
70+
r->write_event_handler == ngx_http_core_run_phases ? \
71+
"ngx_http_core_run_phases" : \
72+
r->write_event_handler == ngx_http_request_empty_handler ? \
73+
"ngx_http_request_empty_handler" : "UNKNOWN")
74+
75+
#else
76+
77+
#define dd_check_read_event_handler(r)
78+
#define dd_check_write_event_handler(r)
79+
80+
#endif
81+
82+
83+
#endif /* _DDEBUG_H_INCLUDED_ */
84+
85+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/meta/ngx_meta_lua_api.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef _NGX_META_LUA_API_H_INCLUDED_
8+
#define _NGX_META_LUA_API_H_INCLUDED_
9+
10+
11+
#include <nginx.h>
12+
#include <ngx_core.h>
13+
14+
#include <lua.h>
15+
#include <lualib.h>
16+
#include <lauxlib.h>
17+
18+
19+
#define ngx_meta_lua_version 00001
20+
21+
22+
typedef ngx_int_t (*ngx_meta_lua_main_conf_handler_pt)(ngx_log_t *log,
23+
ngx_str_t init_src, lua_State *L);
24+
25+
26+
ngx_int_t ngx_meta_lua_post_init_handler(ngx_conf_t *cf,
27+
ngx_meta_lua_main_conf_handler_pt init_handler, ngx_str_t init_src,
28+
lua_State *L);
29+
char *ngx_meta_lua_shdict_directive_helper(ngx_conf_t *cf, void *tag);
30+
void ngx_meta_lua_inject_shdict_api(lua_State *L, ngx_cycle_t *cycle,
31+
void *tag);
32+
33+
34+
#endif /* _NGX_META_LUA_API_H_INCLUDED_ */
35+
36+
37+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/meta/ngx_meta_lua_module.c

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Copyright (C) Yichun Zhang (agentzh)
3+
*/
4+
5+
#include <ngx_config.h>
6+
7+
#include "ngx_meta_lua_shdict.h"
8+
9+
10+
static void *ngx_meta_lua_module_create_conf(ngx_cycle_t *cycle);
11+
static char *ngx_meta_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
12+
static ngx_int_t ngx_meta_lua_run_init_handler(ngx_cycle_t *cycle,
13+
ngx_log_t *log, ngx_meta_lua_init_handler_t *inh);
14+
15+
16+
static ngx_command_t ngx_meta_lua_cmds[] = {
17+
18+
{ ngx_string("lua"),
19+
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
20+
ngx_meta_lua_block,
21+
0,
22+
0,
23+
NULL },
24+
25+
{ ngx_string("lua_shared_dict"),
26+
NGX_META_LUA_CONF|NGX_CONF_TAKE2,
27+
ngx_meta_lua_shdict_directive,
28+
0,
29+
0,
30+
NULL },
31+
32+
ngx_null_command
33+
};
34+
35+
36+
static ngx_core_module_t ngx_meta_lua_module_ctx = {
37+
ngx_string("lua"),
38+
ngx_meta_lua_module_create_conf,
39+
NULL
40+
};
41+
42+
43+
ngx_module_t ngx_meta_lua_module = {
44+
NGX_MODULE_V1,
45+
&ngx_meta_lua_module_ctx, /* module context */
46+
ngx_meta_lua_cmds, /* module directives */
47+
NGX_CORE_MODULE, /* module type */
48+
NULL, /* init master */
49+
NULL, /* init module */
50+
NULL, /* init process */
51+
NULL, /* init thread */
52+
NULL, /* exit thread */
53+
NULL, /* exit process */
54+
NULL, /* exit master */
55+
NGX_MODULE_V1_PADDING
56+
};
57+
58+
59+
static void *
60+
ngx_meta_lua_module_create_conf(ngx_cycle_t *cycle)
61+
{
62+
ngx_meta_lua_conf_t *mcf;
63+
64+
mcf = ngx_pcalloc(cycle->pool, sizeof(ngx_meta_lua_conf_t));
65+
if (mcf == NULL) {
66+
return NULL;
67+
}
68+
69+
mcf->shdict_zones = ngx_array_create(cycle->pool, 2,
70+
sizeof(ngx_shm_zone_t *));
71+
if (mcf->shdict_zones == NULL) {
72+
return NULL;
73+
}
74+
75+
mcf->init_handlers = ngx_array_create(cycle->pool, 2,
76+
sizeof(ngx_meta_lua_init_handler_t *));
77+
if (mcf->init_handlers == NULL) {
78+
return NULL;
79+
}
80+
81+
return mcf;
82+
}
83+
84+
85+
static char *
86+
ngx_meta_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
87+
{
88+
char *rv;
89+
ngx_conf_t pcf;
90+
ngx_meta_lua_conf_t *mcf = *(ngx_meta_lua_conf_t **) conf;
91+
92+
if (mcf->parsed_lua_block) {
93+
return "is duplicate";
94+
}
95+
96+
/* parse the lua{} block */
97+
98+
mcf->parsed_lua_block = 1;
99+
100+
pcf = *cf;
101+
102+
cf->ctx = mcf;
103+
cf->module_type = NGX_CORE_MODULE;
104+
cf->cmd_type = NGX_META_LUA_CONF;
105+
106+
rv = ngx_conf_parse(cf, NULL);
107+
108+
*cf = pcf;
109+
110+
if (rv != NGX_CONF_OK) {
111+
return NGX_CONF_ERROR;
112+
}
113+
114+
return NGX_CONF_OK;
115+
}
116+
117+
118+
ngx_int_t
119+
ngx_meta_lua_post_init_handler(ngx_conf_t *cf,
120+
ngx_meta_lua_main_conf_handler_pt init_handler, ngx_str_t init_src,
121+
lua_State *L)
122+
{
123+
ngx_meta_lua_conf_t *mcf;
124+
ngx_meta_lua_init_handler_t **inhp, *inh, iinh;
125+
126+
if (init_handler == NULL) {
127+
return NGX_OK;
128+
}
129+
130+
mcf = ngx_meta_lua_conf_get_main_conf(cf);
131+
132+
if (!mcf->delay_init_handlers) {
133+
iinh.init_handler = init_handler;
134+
iinh.init_src = init_src;
135+
iinh.L = L;
136+
137+
return ngx_meta_lua_run_init_handler(cf->cycle, cf->log, &iinh);
138+
}
139+
140+
inh = ngx_palloc(cf->pool, sizeof(ngx_meta_lua_init_handler_t));
141+
if (inh == NULL) {
142+
return NGX_ERROR;
143+
}
144+
145+
inh->init_handler = init_handler;
146+
inh->init_src = init_src;
147+
inh->L = L;
148+
149+
inhp = ngx_array_push(mcf->init_handlers);
150+
if (inhp == NULL) {
151+
return NGX_ERROR;
152+
}
153+
154+
*inhp = inh;
155+
156+
return NGX_OK;
157+
}
158+
159+
160+
ngx_int_t
161+
ngx_meta_lua_run_delayed_init_handlers(ngx_meta_lua_conf_t *mcf,
162+
ngx_cycle_t *cycle, ngx_log_t *log)
163+
{
164+
ngx_uint_t i;
165+
ngx_meta_lua_init_handler_t **inhp;
166+
167+
inhp = mcf->init_handlers->elts;
168+
169+
/* respect order in which the modules were compiled */
170+
171+
for (i = 0; i < mcf->init_handlers->nelts; i++) {
172+
ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
173+
"lua run delayed init_handler: %p", inhp[i]);
174+
175+
if (ngx_meta_lua_run_init_handler(cycle, log, inhp[i]) != NGX_OK) {
176+
return NGX_ERROR;
177+
}
178+
}
179+
180+
return NGX_OK;
181+
}
182+
183+
184+
static ngx_int_t
185+
ngx_meta_lua_run_init_handler(ngx_cycle_t *cycle, ngx_log_t *log,
186+
ngx_meta_lua_init_handler_t *inh)
187+
{
188+
volatile ngx_cycle_t *saved_cycle;
189+
ngx_int_t rc;
190+
191+
saved_cycle = ngx_cycle;
192+
ngx_cycle = cycle;
193+
194+
rc = inh->init_handler(log, inh->init_src, inh->L);
195+
196+
ngx_cycle = saved_cycle;
197+
198+
if (rc != NGX_OK) {
199+
return NGX_ERROR;
200+
}
201+
202+
return NGX_OK;
203+
}
204+
205+
206+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/meta/ngx_meta_lua_module.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
/*
3+
* Copyright (C) Yichun Zhang (agentzh)
4+
*/
5+
6+
7+
#ifndef _NGX_META_LUA_H_INCLUDED_
8+
#define _NGX_META_LUA_H_INCLUDED_
9+
10+
11+
#include "ngx_meta_lua_api.h"
12+
13+
14+
#define NGX_META_LUA_MODULE 0x41554c4d /* "MLUA" */
15+
#define NGX_META_LUA_CONF 0x02000000
16+
17+
18+
#define ngx_meta_lua_conf_get_main_conf(cf) \
19+
((ngx_meta_lua_conf_t *) ngx_get_conf(cf->cycle->conf_ctx, \
20+
ngx_meta_lua_module))
21+
22+
#define ngx_meta_lua_cycle_get_main_conf(cycle) \
23+
((ngx_meta_lua_conf_t *) ngx_get_conf(cycle->conf_ctx, \
24+
ngx_meta_lua_module))
25+
26+
27+
typedef struct {
28+
ngx_uint_t shm_zones_inited;
29+
ngx_array_t *shdict_zones;
30+
ngx_array_t *init_handlers;
31+
unsigned delay_init_handlers:1;
32+
unsigned parsed_lua_block:1;
33+
} ngx_meta_lua_conf_t;
34+
35+
36+
typedef struct {
37+
ngx_meta_lua_main_conf_handler_pt init_handler;
38+
ngx_str_t init_src;
39+
lua_State *L;
40+
} ngx_meta_lua_init_handler_t;
41+
42+
43+
extern ngx_module_t ngx_meta_lua_module;
44+
45+
46+
ngx_int_t ngx_meta_lua_run_delayed_init_handlers(ngx_meta_lua_conf_t *mcf,
47+
ngx_cycle_t *cycle, ngx_log_t *log);
48+
49+
50+
#endif /* _NGX_META_LUA_H_INCLUDED_ */
51+
52+
53+
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

0 commit comments

Comments
 (0)