Skip to content

Commit

Permalink
proxy: -o proxy_arg to pass cmdline to lua
Browse files Browse the repository at this point in the history
Assigned to `mcp.start_arg` and available in all threads.

If argument is a normal string, it is assigned as a string.

If the argument contains ':' then it is split into a table. If sections
of the string contain '_' then they are treated as key/value.

`proxy_arg=foo` -> foo.

`proxy_arg=foo_bar:baz' -> { foo = "bar", baz = true }
  • Loading branch information
dormando committed Mar 27, 2024
1 parent e9b2550 commit 77709d0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
8 changes: 7 additions & 1 deletion memcached.c
Expand Up @@ -4202,7 +4202,8 @@ static void usage(void) {
verify_default("ext_item_age", settings.ext_item_age == UINT_MAX);
#endif
#ifdef PROXY
printf(" - proxy_config: path to lua config file.\n");
printf(" - proxy_config: path to lua library file. separate with ':' for multiple files\n");
printf(" - proxy_arg: string to pass to lua library\n");
#endif
#ifdef TLS
printf(" - ssl_chain_cert: certificate chain file in PEM format\n"
Expand Down Expand Up @@ -4850,6 +4851,7 @@ int main (int argc, char **argv) {
#endif
#ifdef PROXY
PROXY_CONFIG,
PROXY_ARG,
PROXY_URING,
PROXY_MEMPROFILE,
#endif
Expand Down Expand Up @@ -4913,6 +4915,7 @@ int main (int argc, char **argv) {
#endif
#ifdef PROXY
[PROXY_CONFIG] = "proxy_config",
[PROXY_ARG] = "proxy_arg",
[PROXY_URING] = "proxy_uring",
[PROXY_MEMPROFILE] = "proxy_memprofile",
#endif
Expand Down Expand Up @@ -5685,6 +5688,9 @@ int main (int argc, char **argv) {
settings.binding_protocol = proxy_prot;
protocol_specified = true;
break;
case PROXY_ARG:
settings.proxy_startarg = strdup(subopts_value);
break;
case PROXY_URING:
settings.proxy_uring = true;
break;
Expand Down
1 change: 1 addition & 0 deletions memcached.h
Expand Up @@ -548,6 +548,7 @@ struct settings {
bool proxy_uring; /* if the proxy should use io_uring */
bool proxy_memprofile; /* output detail of lua allocations */
char *proxy_startfile; /* lua file to run when workers start */
char *proxy_startarg; /* string argument to pass to proxy */
void *proxy_ctx; /* proxy's state context */
#endif
#ifdef SOCK_COOKIE_ID
Expand Down
2 changes: 1 addition & 1 deletion proto_proxy.c
Expand Up @@ -263,7 +263,7 @@ void process_proxy_funcstats(void *arg, ADD_STAT add_stats, conn *c) {
void *proxy_init(bool use_uring, bool proxy_memprofile) {
proxy_ctx_t *ctx = calloc(1, sizeof(proxy_ctx_t));
ctx->use_uring = use_uring;
ctx->memprofile = proxy_memprofile; // FIXME: take from argument.
ctx->memprofile = proxy_memprofile;

pthread_mutex_init(&ctx->config_lock, NULL);
pthread_cond_init(&ctx->config_cond, NULL);
Expand Down
41 changes: 41 additions & 0 deletions proxy_lua.c
Expand Up @@ -1377,6 +1377,44 @@ static void proxy_register_defines(lua_State *L) {
lua_setfield(L, -2, "WAIT_RESUME");
}

// TODO: low priority malloc error handling.
static void proxy_register_startarg(lua_State *L) {
int idx = lua_absindex(L, -1); // remember 'mcp' table.
if (settings.proxy_startarg == NULL) {
// no argument given.
lua_pushboolean(L, 0);
lua_setfield(L, idx, "start_arg");
return;
}

char *sarg = strdup(settings.proxy_startarg);
if (strchr(sarg, ':') == NULL) {
// just upload the string
lua_pushstring(L, sarg);
} else {
// split into a table and set that instead.
lua_newtable(L);
int nidx = lua_absindex(L, -1);
char *b = NULL;
for (char *p = strtok_r(sarg, ":", &b);
p != NULL;
p = strtok_r(NULL, ":", &b)) {
char *e = NULL;
char *name = strtok_r(p, "_", &e);
lua_pushstring(L, name); // table -> key
char *value = strtok_r(NULL, "_", &e);
if (value == NULL) {
lua_pushboolean(L, 1); // table -> key -> True
} else {
lua_pushstring(L, value); // table -> key -> value
}
lua_settable(L, nidx);
}
}
free(sarg);
lua_setfield(L, idx, "start_arg");
}

// Creates and returns the top level "mcp" module
int proxy_register_libs(void *ctx, LIBEVENT_THREAD *t, void *state) {
lua_State *L = state;
Expand Down Expand Up @@ -1627,6 +1665,9 @@ int proxy_register_libs(void *ctx, LIBEVENT_THREAD *t, void *state) {
luaL_setfuncs(L, mcplib_f_config, 1); // store upvalues.
}

// every VM gets a copy of the start arguments to work with.
proxy_register_startarg(L);

lua_setglobal(L, "mcp"); // set the lib table to mcp global.
return 1;
}

0 comments on commit 77709d0

Please sign in to comment.