Logs: a thread and a queue for each log; build-time defaults

Once 3proxy runs (after the first configuration is in force, so after a
daemon's fork), each logger has a thread of its own writing from a
queue, and a client never waits for a log: ODBC, RADIUS, a full disk or
a pipe nobody reads hold up only that log's thread.

A queued line is fixed size: the part of struct clientparam logging
reads (everything before the filters: addresses, counters, names and
credentials, which hold no pointer it follows but srv), the time it was
logged, and its text, 256 bytes inline or a copy of its own if longer.
The thread formats it: struct tm is per call now, timechanged() and
dologname() included, and the type's format() takes the time.

minlogqueue (64) slots are allocated as the thread starts and kept,
maxlogqueue (4096) lines are queued at most. A line which finds the
queue full is dropped and counted on the last one queued; once that is
written, "Log X has its queue full, N records dropped" goes to where
errors go. A log which fails is no longer written to the error log
instead: its lines are dropped and counted while it is retried every
minute, and the count is told when it is open again, or when it was
reopened after failed writes.

A service which ended is stopped at once (srvstop(): sockets, listener,
its clients gone), but freed (srvrelease()) through its log's queue,
behind the lines which refer to it and to its configuration: the event
is part of struct srvparam, so a full queue never drops it. logfinish()
writes what is queued, 10 seconds at most, also as 3proxy exits on Unix.
Standalone services write as before, as each line comes.

LOGQUEUEMIN, LOGQUEUEMAX, DEFAULT_MAXCHILD, DEFAULT_POOLMAX,
DEFAULT_POOLTIMEOUT and POOLPERSERVICE (the automatic thread pool cap
for each service, which was 32 in the code) can be set with -D at build
time.

Tests: a log nobody reads (a FIFO) holds no client up, drops past its
queue and says so, and lines queued for a service a reload removed are
written as it formatted them; /dev/full says what it dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-23 19:41:48 +03:00
1 parent a3c417c82a
commit 2052dc0a4f
13 files changed
+930 -158

No files matched your search

+33 -8
View File
@@ -145,13 +145,28 @@ void setopts(SOCKET s, const struct sockoptset *opts, int maxseg){
}
}
/* Releases what the preparation and the run took, s itself excepted. */
static void srvdiscard(struct srvstart *s){
srvfree(&s->srv);
static void srvstrings(struct srvstart *s){
if(s->cbc_string) free(s->cbc_string);
if(s->cbl_string) free(s->cbl_string);
}
/* Releases what the preparation and the run took, s itself excepted. */
static void srvdiscard(struct srvstart *s){
srvfree(&s->srv);
srvstrings(s);
}
#ifndef STDMAIN
/* A service which ran, released once its lines are written, s with it. */
static void srvlater(void *p){
struct srvstart *s = (struct srvstart *)p;
srvrelease(&s->srv);
srvstrings(s);
free(s);
}
#endif
#ifndef STDMAIN
static void srvunlink(struct srvparam *srv){
if(srv->next)srv->next->prev = srv->prev;
@@ -673,11 +688,9 @@ int mainprepare(int argc, char** argv, void **service){
return 0;
}
/* service is freed once its lines are written */
int mainrun(void *service){
int res = srvrun((struct srvstart *)service);
free(service);
return res;
return srvrun((struct srvstart *)service);
}
#endif
@@ -782,7 +795,9 @@ void srvinit2(struct srvparam * srv, struct clientparam *param){
param->sinsr = srv->extsa;
}
void srvfree(struct srvparam * srv){
/* A service ended: its sockets closed, its clients gone. What its lines
refer to is kept for srvrelease(). */
void srvstop(struct srvparam * srv){
int i;
#ifndef STDMAIN
@@ -810,6 +825,11 @@ void srvfree(struct srvparam * srv){
for(i = srv->maxchild; i; i--) _3proxy_sem_lock(srv->childslots);
_3proxy_mutex_lock(&srv->counter_mutex);
_3proxy_mutex_unlock(&srv->counter_mutex);
}
/* What a stopped service holds: once the lines logged for it, which refer
to it and to its configuration, are written. */
void srvrelease(struct srvparam * srv){
#ifndef STDMAIN
if(srv->filter){
while(srv->nfilters){
@@ -861,6 +881,11 @@ void srvfree(struct srvparam * srv){
#endif
}
void srvfree(struct srvparam * srv){
srvstop(srv);
srvrelease(srv);
}
#ifndef STDMAIN
static void * itcopy (void * from, size_t size){
void * ret;