Skip to content

feature: new ngx_meta_lua_module (with multi-subsystems lua_shared_dict support) #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
build
out/
work/
t/servroot*
build*
test*
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
SUBSYS?=http
DESTDIR?=build/src
DESTDIR?=out/src

TEMPLATE_SOURCES=$(wildcard src/subsys/*.tt2)
TEMPLATE_TARGETS=$(subst _subsys_,_$(SUBSYS)_, $(patsubst src/subsys/%.tt2, $(DESTDIR)/%, $(TEMPLATE_SOURCES)))
API_TEMPLATE_SOURCES=$(wildcard src/subsys/api/*.tt2)
API_TEMPLATE_TARGETS=$(subst _subsys_,_$(SUBSYS)_, $(patsubst src/subsys/%.tt2, $(DESTDIR)/%, $(API_TEMPLATE_SOURCES)))
MINI_TT2=util/mini-tt2.pl

.PHONY: all
.PHONY: all clean

all: $(DESTDIR)/api $(TEMPLATE_TARGETS) $(API_TEMPLATE_TARGETS)
find src/$(SUBSYS) -type f -name '*.tt2' -exec $(MINI_TT2) -d $(DESTDIR) -s $(SUBSYS) '{}' ';'
$(shell cp src/$(SUBSYS)/*.{h,c} $(DESTDIR))
Expand All @@ -30,6 +31,5 @@ $(DESTDIR)/%: src/subsys/%.tt2
$(DESTDIR)/api:
mkdir -p $(DESTDIR)/api

.PHONY: clean
clean:
rm -rf build
rm -rf out buildroot work t/servroot*
1 change: 0 additions & 1 deletion src/http/ngx_http_lua_headerfilterby.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
#include "ngx_http_lua_shdict.h"


static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
Expand Down
1 change: 0 additions & 1 deletion src/http/ngx_http_lua_setby.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "ngx_http_lua_string.h"
#include "ngx_http_lua_misc.h"
#include "ngx_http_lua_consts.h"
#include "ngx_http_lua_shdict.h"
#include "ngx_http_lua_util.h"


Expand Down
13 changes: 13 additions & 0 deletions src/meta/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ngx_module_type=CORE
ngx_module_name=ngx_meta_lua_module
ngx_module_incs="$ngx_addon_dir"
ngx_module_deps="$ngx_addon_dir/ddebug.h \
$ngx_addon_dir/ngx_meta_lua_api.h \
$ngx_addon_dir/ngx_meta_lua_module.h \
$ngx_addon_dir/ngx_meta_lua_shdict.h"
ngx_module_srcs="$ngx_addon_dir/ngx_meta_lua_module.c \
$ngx_addon_dir/ngx_meta_lua_shdict.c"
ngx_module_libs=
ngx_module_link=ADDON

. auto/module
24 changes: 12 additions & 12 deletions src/subsys/ddebug.h.tt2 → src/meta/ddebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ dd(const char *fmt, ...) {

#define dd_check_read_event_handler(r) \
dd("r->read_event_handler = %s", \
r->read_event_handler == ngx_http_block_reading ? \
"ngx_http_block_reading" : \
r->read_event_handler == ngx_http_test_reading ? \
"ngx_http_test_reading" : \
r->read_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")
r->read_event_handler == ngx_http_block_reading ? \
"ngx_http_block_reading" : \
r->read_event_handler == ngx_http_test_reading ? \
"ngx_http_test_reading" : \
r->read_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")

#define dd_check_write_event_handler(r) \
dd("r->write_event_handler = %s", \
r->write_event_handler == ngx_http_handler ? \
"ngx_http_handler" : \
r->write_event_handler == ngx_http_core_run_phases ? \
"ngx_http_core_run_phases" : \
r->write_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")
r->write_event_handler == ngx_http_handler ? \
"ngx_http_handler" : \
r->write_event_handler == ngx_http_core_run_phases ? \
"ngx_http_core_run_phases" : \
r->write_event_handler == ngx_http_request_empty_handler ? \
"ngx_http_request_empty_handler" : "UNKNOWN")

#else

Expand Down
37 changes: 37 additions & 0 deletions src/meta/ngx_meta_lua_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

/*
* Copyright (C) Yichun Zhang (agentzh)
*/


#ifndef _NGX_META_LUA_API_H_INCLUDED_
#define _NGX_META_LUA_API_H_INCLUDED_


#include <nginx.h>
#include <ngx_core.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>


#define ngx_meta_lua_version 00001


typedef ngx_int_t (*ngx_meta_lua_main_conf_handler_pt)(ngx_log_t *log,
ngx_str_t init_src, lua_State *L);


ngx_int_t ngx_meta_lua_post_init_handler(ngx_conf_t *cf,
ngx_meta_lua_main_conf_handler_pt init_handler, ngx_str_t init_src,
lua_State *L);
char *ngx_meta_lua_shdict_directive_helper(ngx_conf_t *cf, void *tag);
void ngx_meta_lua_inject_shdict_api(lua_State *L, ngx_cycle_t *cycle,
void *tag);


#endif /* _NGX_META_LUA_API_H_INCLUDED_ */


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
206 changes: 206 additions & 0 deletions src/meta/ngx_meta_lua_module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright (C) Yichun Zhang (agentzh)
*/

#include <ngx_config.h>

#include "ngx_meta_lua_shdict.h"


static void *ngx_meta_lua_module_create_conf(ngx_cycle_t *cycle);
static char *ngx_meta_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_meta_lua_run_init_handler(ngx_cycle_t *cycle,
ngx_log_t *log, ngx_meta_lua_init_handler_t *inh);


static ngx_command_t ngx_meta_lua_cmds[] = {

{ ngx_string("lua"),
NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_meta_lua_block,
0,
0,
NULL },

{ ngx_string("lua_shared_dict"),
NGX_META_LUA_CONF|NGX_CONF_TAKE2,
ngx_meta_lua_shdict_directive,
0,
0,
NULL },

ngx_null_command
};


static ngx_core_module_t ngx_meta_lua_module_ctx = {
ngx_string("lua"),
ngx_meta_lua_module_create_conf,
NULL
};


ngx_module_t ngx_meta_lua_module = {
NGX_MODULE_V1,
&ngx_meta_lua_module_ctx, /* module context */
ngx_meta_lua_cmds, /* module directives */
NGX_CORE_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};


static void *
ngx_meta_lua_module_create_conf(ngx_cycle_t *cycle)
{
ngx_meta_lua_conf_t *mcf;

mcf = ngx_pcalloc(cycle->pool, sizeof(ngx_meta_lua_conf_t));
if (mcf == NULL) {
return NULL;
}

mcf->shdict_zones = ngx_array_create(cycle->pool, 2,
sizeof(ngx_shm_zone_t *));
if (mcf->shdict_zones == NULL) {
return NULL;
}

mcf->init_handlers = ngx_array_create(cycle->pool, 2,
sizeof(ngx_meta_lua_init_handler_t *));
if (mcf->init_handlers == NULL) {
return NULL;
}

return mcf;
}


static char *
ngx_meta_lua_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
char *rv;
ngx_conf_t pcf;
ngx_meta_lua_conf_t *mcf = *(ngx_meta_lua_conf_t **) conf;

if (mcf->parsed_lua_block) {
return "is duplicate";
}

/* parse the lua{} block */

mcf->parsed_lua_block = 1;

pcf = *cf;

cf->ctx = mcf;
cf->module_type = NGX_CORE_MODULE;
cf->cmd_type = NGX_META_LUA_CONF;

rv = ngx_conf_parse(cf, NULL);

*cf = pcf;

if (rv != NGX_CONF_OK) {
return NGX_CONF_ERROR;
}

return NGX_CONF_OK;
}


ngx_int_t
ngx_meta_lua_post_init_handler(ngx_conf_t *cf,
ngx_meta_lua_main_conf_handler_pt init_handler, ngx_str_t init_src,
lua_State *L)
{
ngx_meta_lua_conf_t *mcf;
ngx_meta_lua_init_handler_t **inhp, *inh, iinh;

if (init_handler == NULL) {
return NGX_OK;
}

mcf = ngx_meta_lua_conf_get_main_conf(cf);

if (!mcf->delay_init_handlers) {
iinh.init_handler = init_handler;
iinh.init_src = init_src;
iinh.L = L;

return ngx_meta_lua_run_init_handler(cf->cycle, cf->log, &iinh);
}

inh = ngx_palloc(cf->pool, sizeof(ngx_meta_lua_init_handler_t));
if (inh == NULL) {
return NGX_ERROR;
}

inh->init_handler = init_handler;
inh->init_src = init_src;
inh->L = L;

inhp = ngx_array_push(mcf->init_handlers);
if (inhp == NULL) {
return NGX_ERROR;
}

*inhp = inh;

return NGX_OK;
}


ngx_int_t
ngx_meta_lua_run_delayed_init_handlers(ngx_meta_lua_conf_t *mcf,
ngx_cycle_t *cycle, ngx_log_t *log)
{
ngx_uint_t i;
ngx_meta_lua_init_handler_t **inhp;

inhp = mcf->init_handlers->elts;

/* respect order in which the modules were compiled */

for (i = 0; i < mcf->init_handlers->nelts; i++) {
ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
"lua run delayed init_handler: %p", inhp[i]);

if (ngx_meta_lua_run_init_handler(cycle, log, inhp[i]) != NGX_OK) {
return NGX_ERROR;
}
}

return NGX_OK;
}


static ngx_int_t
ngx_meta_lua_run_init_handler(ngx_cycle_t *cycle, ngx_log_t *log,
ngx_meta_lua_init_handler_t *inh)
{
volatile ngx_cycle_t *saved_cycle;
ngx_int_t rc;

saved_cycle = ngx_cycle;
ngx_cycle = cycle;

rc = inh->init_handler(log, inh->init_src, inh->L);

ngx_cycle = saved_cycle;

if (rc != NGX_OK) {
return NGX_ERROR;
}

return NGX_OK;
}


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
53 changes: 53 additions & 0 deletions src/meta/ngx_meta_lua_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

/*
* Copyright (C) Yichun Zhang (agentzh)
*/


#ifndef _NGX_META_LUA_H_INCLUDED_
#define _NGX_META_LUA_H_INCLUDED_


#include "ngx_meta_lua_api.h"


#define NGX_META_LUA_MODULE 0x41554c4d /* "MLUA" */
#define NGX_META_LUA_CONF 0x02000000


#define ngx_meta_lua_conf_get_main_conf(cf) \
((ngx_meta_lua_conf_t *) ngx_get_conf(cf->cycle->conf_ctx, \
ngx_meta_lua_module))

#define ngx_meta_lua_cycle_get_main_conf(cycle) \
((ngx_meta_lua_conf_t *) ngx_get_conf(cycle->conf_ctx, \
ngx_meta_lua_module))


typedef struct {
ngx_uint_t shm_zones_inited;
ngx_array_t *shdict_zones;
ngx_array_t *init_handlers;
unsigned delay_init_handlers:1;
unsigned parsed_lua_block:1;
} ngx_meta_lua_conf_t;


typedef struct {
ngx_meta_lua_main_conf_handler_pt init_handler;
ngx_str_t init_src;
lua_State *L;
} ngx_meta_lua_init_handler_t;


extern ngx_module_t ngx_meta_lua_module;


ngx_int_t ngx_meta_lua_run_delayed_init_handlers(ngx_meta_lua_conf_t *mcf,
ngx_cycle_t *cycle, ngx_log_t *log);


#endif /* _NGX_META_LUA_H_INCLUDED_ */


/* vi:set ft=c ts=4 sw=4 et fdm=marker: */
Loading