Skip to content

Commit

Permalink
proxy: mutator code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
dormando committed Oct 31, 2024
1 parent d44c8f0 commit 49850fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ struct mcp_funcgen_s {
int self_ref; // self-reference if we're attached anywhere
int argument_ref; // reference to an argument to pass to generator
int max_queues; // how many queue slots rctx's have
int extra_queues; // how many extra queue slots for object storage we have
int uobj_queues; // how many extra queue slots for object storage we have
unsigned int refcount; // reference counter
unsigned int total; // total contexts managed
unsigned int free; // free contexts
Expand Down Expand Up @@ -748,7 +748,7 @@ struct mcp_rcontext_s {
int conn_fd; // fd of the originating client, as *c can become invalid
enum mcp_rqueue_e wait_mode;
uint8_t lua_narg; // number of responses to push when yield resuming.
uint8_t obj_count; // number of extra tracked req/res objects.
uint8_t uobj_count; // number of extra tracked req/res objects.
bool first_queue; // HACK
lua_State *Lc; // coroutine thread pointer.
mcp_request_t *request; // ptr to the above reference.
Expand Down
48 changes: 23 additions & 25 deletions proxy_luafgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ static void mcp_rcontext_cleanup(lua_State *L, mcp_funcgen_t *fgen, mcp_rcontext
}

// look for rctx-local objects.
if (rctx->obj_count) {
int lim = fgen->max_queues + rctx->obj_count;
if (rctx->uobj_count) {
int lim = fgen->max_queues + rctx->uobj_count;
for (int x = fgen->max_queues; x < lim; x++) {
struct mcp_rqueue_s *rqu = &rctx->qslots[x];
// Don't need to look at the type:
Expand Down Expand Up @@ -220,7 +220,7 @@ static int _mcplib_funcgen_gencall(lua_State *L) {
mcp_funcgen_t *fgen = luaL_checkudata(L, -2, "mcp.funcgen");
int fgen_idx = lua_absindex(L, -2);
// create the ctx object.
int total_queues = fgen->max_queues + fgen->extra_queues;
int total_queues = fgen->max_queues + fgen->uobj_queues;
size_t rctx_len = sizeof(mcp_rcontext_t) + sizeof(struct mcp_rqueue_s) * total_queues;
mcp_rcontext_t *rc = lua_newuserdatauv(L, rctx_len, 0);
memset(rc, 0, rctx_len);
Expand Down Expand Up @@ -384,8 +384,8 @@ static void _mcp_funcgen_return_rctx(mcp_rcontext_t *rctx) {
}

// look for rctx-local objects.
if (rctx->obj_count) {
int lim = fgen->max_queues + rctx->obj_count;
if (rctx->uobj_count) {
int lim = fgen->max_queues + rctx->uobj_count;
for (int x = fgen->max_queues; x < lim; x++) {
struct mcp_rqueue_s *rqu = &rctx->qslots[x];
if (rqu->obj_type == RQUEUE_TYPE_UOBJ_REQ) {
Expand Down Expand Up @@ -740,12 +740,12 @@ int mcplib_funcgen_ready(lua_State *L) {
}

if (lua_getfield(L, 2, "u") == LUA_TNUMBER) {
int extra_queues = luaL_checkinteger(L, -1);
if (extra_queues < 1 || extra_queues > RQUEUE_UOBJ_MAX) {
int uobj_queues = luaL_checkinteger(L, -1);
if (uobj_queues < 1 || uobj_queues > RQUEUE_UOBJ_MAX) {
proxy_lua_ferror(L, "user obj ('u') in fgen:ready must be between 1 and %d", RQUEUE_UOBJ_MAX);
return 0;
}
fgen->extra_queues = extra_queues;
fgen->uobj_queues = uobj_queues;
}
lua_pop(L, 1);

Expand Down Expand Up @@ -1489,11 +1489,22 @@ int mcplib_rcontext_tls_peer_cn(lua_State *L) {
return 1;
}

// call with uobj on top of stack
static void _mcplib_rcontext_ref_uobj(lua_State *L, mcp_rcontext_t *rctx, void *obj, int otype) {
lua_pushvalue(L, -1); // dupe rq for the rqueue slot
struct mcp_rqueue_s *rqu = &rctx->qslots[rctx->fgen->max_queues + rctx->uobj_count];
rctx->uobj_count++;
// hold the request reference into the rctx for memory management.
rqu->obj_ref = luaL_ref(L, LUA_REGISTRYINDEX);
rqu->obj_type = otype;
rqu->obj = obj;
}

// Creates request object that's tracked by request context so we can call
// cleanup routines post-run.
int mcplib_rcontext_request_new(lua_State *L) {
mcp_rcontext_t *rctx = lua_touserdata(L, 1);
if (rctx->obj_count == rctx->fgen->extra_queues) {
if (rctx->uobj_count == rctx->fgen->uobj_queues) {
proxy_lua_error(L, "rctx request new: object count limit reached");
return 0;
}
Expand All @@ -1502,20 +1513,13 @@ int mcplib_rcontext_request_new(lua_State *L) {
mcp_parser_t pr = {0};
mcp_request_t *rq = mcp_new_request(L, &pr, " ", 1);

lua_pushvalue(L, -1); // dupe rq for the rqueue slot
struct mcp_rqueue_s *rqu = &rctx->qslots[rctx->fgen->max_queues + rctx->obj_count];
rctx->obj_count++;
// hold the request reference into the rctx for memory management.
rqu->obj_ref = luaL_ref(L, LUA_REGISTRYINDEX);
rqu->obj_type = RQUEUE_TYPE_UOBJ_REQ;
rqu->obj = rq;

_mcplib_rcontext_ref_uobj(L, rctx, rq, RQUEUE_TYPE_UOBJ_REQ);
return 1;
}

int mcplib_rcontext_response_new(lua_State *L) {
mcp_rcontext_t *rctx = lua_touserdata(L, 1);
if (rctx->obj_count == rctx->fgen->extra_queues) {
if (rctx->uobj_count == rctx->fgen->uobj_queues) {
proxy_lua_error(L, "rctx request new: object count limit reached");
return 0;
}
Expand All @@ -1525,13 +1529,7 @@ int mcplib_rcontext_response_new(lua_State *L) {
luaL_getmetatable(L, "mcp.response");
lua_setmetatable(L, -2);

lua_pushvalue(L, -1); // dupe rq for the rqueue slot
struct mcp_rqueue_s *rqu = &rctx->qslots[rctx->fgen->max_queues + rctx->obj_count];
rctx->obj_count++;
// hold the request reference into the rctx for memory management.
rqu->obj_ref = luaL_ref(L, LUA_REGISTRYINDEX);
rqu->obj_type = RQUEUE_TYPE_UOBJ_RES;
rqu->obj = r;
_mcplib_rcontext_ref_uobj(L, rctx, r, RQUEUE_TYPE_UOBJ_RES);
return 1;
}

Expand Down

0 comments on commit 49850fb

Please sign in to comment.