Event loops: a relayed client's buffers given back while empty

A connection's two data buffers are malloc()'d with it (bufalloc())
instead of being part of struct clientparam, which is CLIENTBUFSIZE*2
smaller. The callbacks' relay of a client whose fiber gave up its stack
frees a side's buffer while it is empty and makes it again to read, and
gives both back before sockmap() or the child's tail goes on: a pool of
idle relayed connections holds no buffers. Fibers and threads keep
theirs as before.

mix scenarios, callbacks (-L -L2), peak memory before/after, throughput
the same:
  Intel: relay 972/632 MB, connect 981/581, socks 992/594
  ARM:   relay 1000/673 MB, connect 1000/615, socks 1020/629

struct clientparam's clibufdata and srvbufdata are pointers now:
plugins built against the old structure are not compatible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-26 23:47:24 +03:00
1 parent 80c3863055
commit 7963494b1c
7 files changed
+96 -6

No files matched your search

+25
View File
@@ -77,6 +77,31 @@ int sockrecvfrom(struct clientparam *param, SOCKET sock, struct sockaddr * sin,
}
}
/* The side's own buffer, made if it is not there, and the one used if
none is. 1 if it cannot be had. */
int bufget(struct clientparam * param, DIRECTION which){
unsigned char **own = which? &param->srvbufdata : &param->clibufdata;
unsigned char **buf = which? &param->srvbuf : &param->clibuf;
if(!*own && !(*own = malloc(CLIENTBUFSIZE))) return 1;
if(!*buf) *buf = *own;
return 0;
}
/* Both sides' buffers, as bufget(). */
int bufalloc(struct clientparam * param){
return bufget(param, CLIENT) || bufget(param, SERVER);
}
/* The own buffers freed, with the structure. */
void buffree(struct clientparam * param){
if(param->clibuf == param->clibufdata) param->clibuf = NULL;
if(param->srvbuf == param->srvbufdata) param->srvbuf = NULL;
free(param->clibufdata);
free(param->srvbufdata);
param->clibufdata = param->srvbufdata = NULL;
}
/* Starts an empty buffer over from its beginning, and goes back to the
connection's own buffer if a filter needed a bigger one. Does nothing
while anything is left unread. */