Skip to content

Commit b8b872a

Browse files
committed
proxy: fix proxy_req_active accounting
In V1 active reqs and await reqs were accounted for differently. In V2 there's no reason to do this. This is also less code and cleaner. So now proxy_req_active will be the total of all active slots, allowing the active req limit setting to properly limit global request memory.
1 parent 707594d commit b8b872a

File tree

2 files changed

+3
-12
lines changed

2 files changed

+3
-12
lines changed

proto_proxy.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,12 @@ int try_read_command_proxy(conn *c) {
691691
// Must only be called with an active coroutine.
692692
void proxy_cleanup_conn(conn *c) {
693693
assert(c->proxy_rctx);
694-
LIBEVENT_THREAD *thr = c->thread;
695694
mcp_rcontext_t *rctx = c->proxy_rctx;
696695
assert(rctx->pending_reqs == 1);
697696
rctx->pending_reqs = 0;
698697

699698
mcp_funcgen_return_rctx(rctx);
700699
c->proxy_rctx = NULL;
701-
WSTAT_DECR(thr, proxy_req_active, 1);
702700
}
703701

704702
// we buffered a SET of some kind.
@@ -899,13 +897,11 @@ int proxy_run_rcontext(mcp_rcontext_t *rctx) {
899897
int cores = lua_resume(Lc, NULL, rctx->lua_narg, &nresults);
900898
rctx->lua_narg = 1; // reset to default since not-default is uncommon.
901899
size_t rlen = 0;
902-
conn *c = rctx->c;
903900
mc_resp *resp = rctx->resp;
904901

905902
if (cores == LUA_OK) {
906903
// don't touch the result object if we were a sub-context.
907904
if (!rctx->parent) {
908-
WSTAT_DECR(c->thread, proxy_req_active, 1);
909905
int type = lua_type(Lc, 1);
910906
mcp_resp_t *r = NULL;
911907
P_DEBUG("%s: coroutine completed. return type: %d\n", __func__, type);
@@ -915,7 +911,6 @@ int proxy_run_rcontext(mcp_rcontext_t *rctx) {
915911
proxy_out_errstring(resp, PROXY_SERVER_ERROR, "backend failure");
916912
} else if (r->cresp) {
917913
mc_resp *tresp = r->cresp;
918-
assert(c != NULL);
919914

920915
_proxy_run_tresp_to_resp(tresp, resp);
921916
// we let the mcp_resp gc handler free up tresp and any
@@ -994,7 +989,6 @@ int proxy_run_rcontext(mcp_rcontext_t *rctx) {
994989
P_DEBUG("%s: Failed to run coroutine: %s\n", __func__, lua_tostring(Lc, -1));
995990
LOGGER_LOG(NULL, LOG_PROXYEVENTS, LOGGER_PROXY_ERROR, NULL, lua_tostring(Lc, -1));
996991
if (!rctx->parent) {
997-
WSTAT_DECR(c->thread, proxy_req_active, 1);
998992
proxy_out_errstring(resp, PROXY_SERVER_ERROR, "lua failure");
999993
}
1000994
rctx->pending_reqs--;
@@ -1160,13 +1154,11 @@ static void proxy_process_command(conn *c, char *command, size_t cmdlen, bool mu
11601154
WSTAT_L(c->thread);
11611155
istats->counters[pr.command]++;
11621156
c->thread->stats.proxy_conn_requests++;
1163-
c->thread->stats.proxy_req_active++;
11641157
active_reqs = c->thread->stats.proxy_req_active;
11651158
WSTAT_UL(c->thread);
11661159

1167-
if (active_reqs > ctx->active_req_limit) {
1160+
if (active_reqs >= ctx->active_req_limit) {
11681161
proxy_out_errstring(c->resp, PROXY_SERVER_ERROR, "active request limit reached");
1169-
WSTAT_DECR(c->thread, proxy_req_active, 1);
11701162
if (pr.vlen != 0) {
11711163
c->sbytes = pr.vlen;
11721164
conn_set_state(c, conn_swallow);
@@ -1178,7 +1170,6 @@ static void proxy_process_command(conn *c, char *command, size_t cmdlen, bool mu
11781170
mcp_rcontext_t *rctx = mcp_funcgen_start(L, hook_ref.ctx, &pr);
11791171
if (rctx == NULL) {
11801172
proxy_out_errstring(c->resp, PROXY_SERVER_ERROR, "lua start failure");
1181-
WSTAT_DECR(c->thread, proxy_req_active, 1);
11821173
if (pr.vlen != 0) {
11831174
c->sbytes = pr.vlen;
11841175
conn_set_state(c, conn_swallow);
@@ -1218,7 +1209,6 @@ static void proxy_process_command(conn *c, char *command, size_t cmdlen, bool mu
12181209
// normal cleanup
12191210
lua_settop(L, 0);
12201211
proxy_out_errstring(c->resp, PROXY_SERVER_ERROR, "out of memory");
1221-
WSTAT_DECR(c->thread, proxy_req_active, 1);
12221212
c->sbytes = pr.vlen;
12231213
conn_set_state(c, conn_swallow);
12241214
return;

proxy_luafgen.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ void mcp_funcgen_return_rctx(mcp_rcontext_t *rctx) {
374374
}
375375
return;
376376
}
377+
WSTAT_DECR(rctx->fgen->thread, proxy_req_active, 1);
377378
_mcp_funcgen_return_rctx(rctx);
378379
_mcplib_funcgen_cache(fgen, rctx);
379380
}
@@ -441,7 +442,7 @@ mcp_rcontext_t *mcp_funcgen_start(lua_State *L, mcp_funcgen_t *fgen, mcp_parser_
441442

442443
// TODO: could probably move a few more lines from proto_proxy into here,
443444
// but that's splitting hairs.
444-
445+
WSTAT_INCR(fgen->thread, proxy_req_active, 1);
445446
return rctx;
446447
}
447448

0 commit comments

Comments
 (0)