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

+48 -6
View File
@@ -204,6 +204,14 @@ int
#define UDPBUFSIZE 16384
#define CLIENTBUFSIZE (16384 + 64)
#define CLIENTTMPBUFSIZE 8192
/* The defaults of minlogqueue and maxlogqueue: the lines a log's queue
keeps slots for, and holds at most; -D at build time sets others. */
#ifndef LOGQUEUEMIN
#define LOGQUEUEMIN 64
#endif
#ifndef LOGQUEUEMAX
#define LOGQUEUEMAX 4096
#endif
#define HOSTNAMELEN 256
#define USERNAMELEN 128
@@ -638,20 +646,35 @@ struct logtype {
/* opens what it writes to, lg->data: 0, an error (an errno value), or
< 0 if it cannot be reached now but is to be tried again */
int (*init)(struct logger *lg);
/* the line for a connection into buf, CLIENTTMPBUFSIZE: its length, -1
for nothing to write; NULL if write() takes the connection itself */
int (*format)(struct logger *lg, struct clientparam *param, const unsigned char *s, unsigned char *buf);
/* the line for a connection, logged at sec and msec, into buf,
CLIENTTMPBUFSIZE: its length, -1 for nothing to write; NULL if
write() takes the connection itself */
int (*format)(struct logger *lg, struct clientparam *param, const unsigned char *s, time_t sec, unsigned msec, unsigned char *buf);
/* 0, or an error */
int (*write)(struct logger *lg, struct clientparam *param, const unsigned char *buf, int len);
void (*close)(struct logger *lg);
/* every second or so: rotation, flushing */
void (*tick)(struct logger *lg, time_t t);
int stacksize; /* the client thread stack its writing needs */
int stacksize; /* the stack its writing needs */
int standard; /* kept open for the life of the process */
int text; /* writes a line of text: errors can go there */
int params; /* the string after a comma is parameters */
};
/* What a log's queue holds: a line (log.c's struct logslot, of which this
is the head), or something to do once the lines before it are written,
such as freeing a service they refer to. */
struct logevent {
struct logevent *next;
int kind;
unsigned dropped; /* lines dropped after this one, the queue full */
void (*func)(void *arg);
void *arg;
};
/* Once 3proxy runs, a logger has a thread of its own, which writes what its
queue holds: the clients do not wait for a log. Until then, and in a
standalone service, lines are written as they come. */
struct logger {
struct logger *next;
const struct logtype *type;
@@ -660,20 +683,38 @@ struct logger {
void *data; /* the type's own */
int refs;
int errors; /* writes failed in a row */
int failed; /* could not be reopened: lines go to standard error */
int failed; /* could not be reopened: its lines are dropped */
time_t failtime;
unsigned faildropped; /* lines dropped as it failed, not yet told */
ROTATION rotation;
int rotate, archiverc;
unsigned char **archiver;
time_t rotated; /* when the file in use was opened */
int reopen; /* the rotation changed: the file name too */
_3proxy_mutex_t mutex; /* writing, reopening */
_3proxy_mutex_t mutex; /* the queue; writing, while there is no thread */
/* the queue: the lines in it counted against maxlogqueue, the slots
allocated kept down to minlogqueue as they come back */
struct logevent *head, *tail;
void *free;
int lines, slots;
_3proxy_sem_t wake;
int running, /* has its thread */
stop, /* its thread is to end once the queue is empty */
more; /* for its thread: lines follow the one written */
/* given while its thread runs, for the thread to take */
int newrot, newparams;
ROTATION nrotation;
int nrotate, narchiverc;
unsigned char **narchiver;
unsigned char *nparams;
};
struct srvparam {
struct sockfuncs so;
struct srvparam *next;
struct srvparam *prev;
/* freeing it, queued to its log behind the lines which refer to it */
struct logevent logrelease;
struct listener *listener; /* its listening socket, which a newer
service may take over on a reload */
struct srvparam *lisnext; /* the next service on the listener */
@@ -941,6 +982,7 @@ struct extparam {
struct srvparam *services;
int stacksize,
counterd, rotate, paused, archiverc,
minlogqueue, maxlogqueue,
demon, maxchild, backlog, version, noforce, bandlimver, parentretries;
unsigned authcachetype, authcachetime;
int filtermaxsize;