Skip to content

Commit

Permalink
ruleset: copy configs
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Jan 22, 2025
1 parent f6da67c commit aa37454
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/ruleset.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ struct ruleset *ruleset_new(struct ev_loop *loop)
r->loop = loop;
r->vmstats = (struct ruleset_vmstats){ 0 };
const int memlimit_mb = G.conf->memlimit;
r->memlimit_kb = (memlimit_mb > 0) ? (memlimit_mb << 10u) : 0;
r->config.memlimit_kb = (memlimit_mb > 0) ? (memlimit_mb << 10u) : 0;
r->config.traceback = !!G.conf->traceback;
lua_State *restrict L = lua_newstate(l_alloc, r);
if (L == NULL) {
ruleset_free(r);
Expand Down
7 changes: 5 additions & 2 deletions src/ruleset/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,18 @@ static int api_splithostport(lua_State *restrict L)
static int api_config(lua_State *restrict L)
{
const struct config *restrict conf = G.conf;
lua_createtable(L, 0, 8);
struct ruleset *restrict r = aux_getruleset(L);
lua_createtable(L, 0, 9);

lua_pushinteger(L, (lua_Integer)conf->log_level);
lua_setfield(L, -2, "loglevel");
lua_pushnumber(L, (lua_Number)conf->timeout);
lua_setfield(L, -2, "timeout");
lua_pushboolean(L, conf->auth_required);
lua_setfield(L, -2, "auth_required");
lua_pushboolean(L, conf->traceback);
lua_pushboolean(L, r->config.memlimit_kb);
lua_setfield(L, -2, "memlimit");
lua_pushboolean(L, r->config.traceback);
lua_setfield(L, -2, "traceback");
lua_pushstring(L, conf->listen);
lua_setfield(L, -2, "listen");
Expand Down
12 changes: 8 additions & 4 deletions src/ruleset/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ static int thread_main_k(lua_State *L, int status, lua_KContext ctx);
static int
thread_call_k(lua_State *restrict L, int status, const lua_KContext ctx)
{
const int errfunc = (G.conf->traceback) ? 1 : 0;
struct ruleset *restrict r = aux_getruleset(L);
const int errfunc = (r->config.traceback) ? 1 : 0;
/* lua stack: finish ? ... */
const int base = (int)ctx;
const int n = lua_gettop(L) - base;
Expand All @@ -83,7 +84,8 @@ static int
thread_main_k(lua_State *restrict L, int status, const lua_KContext ctx)
{
ASSERT(status == LUA_YIELD);
const int errfunc = (G.conf->traceback) ? 1 : 0;
struct ruleset *restrict r = aux_getruleset(L);
const int errfunc = (r->config.traceback) ? 1 : 0;
/* lua stack: finish ? func ... */
const int base = (int)ctx;
const int n = lua_gettop(L) - base;
Expand All @@ -95,7 +97,8 @@ thread_main_k(lua_State *restrict L, int status, const lua_KContext ctx)

static int thread_main(lua_State *restrict L)
{
if (G.conf->traceback) {
struct ruleset *restrict r = aux_getruleset(L);
if (r->config.traceback) {
lua_pushcfunction(L, aux_traceback);
}
return lua_yieldk(L, 0, lua_gettop(L), thread_main_k);
Expand Down Expand Up @@ -264,8 +267,9 @@ static bool ruleset_pcallv(
lua_State *restrict L, const lua_CFunction func, const int nargs,
const int nresults, va_list args)
{
struct ruleset *restrict r = aux_getruleset(L);
int errfunc = 0;
if (G.conf->traceback) {
if (r->config.traceback) {
lua_pushcfunction(L, aux_traceback);
errfunc = 1;
}
Expand Down
5 changes: 4 additions & 1 deletion src/ruleset/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
struct ruleset {
struct ev_loop *loop;
struct ruleset_vmstats vmstats;
int memlimit_kb;
struct {
int memlimit_kb;
bool traceback;
} config;
lua_State *L;
struct ev_timer w_ticker;
struct ev_idle w_idle;
Expand Down
2 changes: 1 addition & 1 deletion src/ruleset/cfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ static int ruleset_state_gc(lua_State *restrict L)
static void check_memlimit(lua_State *restrict L)
{
struct ruleset *restrict r = aux_getruleset(L);
const int memlimit_kb = r->memlimit_kb;
const int memlimit_kb = r->config.memlimit_kb;
if (memlimit_kb <= 0) {
return;
}
Expand Down

0 comments on commit aa37454

Please sign in to comment.