UDP loop: deadlines in a heap, on a monotonic clock
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI Linux / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI MacOS / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI Windows / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (macos-15) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-24.04-arm) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (ubuntu-latest) (push) Waiting to run
C/C++ CI cmake / ${{ matrix.target }} (windows-2022) (push) Waiting to run
C/C++ CI cmake / ubuntu-latest (wolfSSL) (push) Waiting to run

The loop went through every session three times a turn: for the nearest
deadline, to register its sockets with epoll, and for the deadlines come
and the sessions ended. A datagram cost as much as the sessions there
were, idle ones too.

- The deadlines are in a binary heap, the nearest at its top: the wait
  is until the top, and the sessions timed out are taken from the top
  down to the first not due. A datagram only moves a session's deadline
  later, which leaves the heap as it is: the session is looked at again
  when its old deadline comes, and put back where its deadline is then,
  once in its timeout however many datagrams it had. A deadline set
  earlier by a handler goes up the heap.
- A session ended is put on a list and let go at the end of the turn,
  and its sockets are registered with epoll after the loop or a handler
  had it, not every session every turn.
- Deadlines are milliseconds of monotime(), CLOCK_MONOTONIC (the
  performance counter on Windows), which the time of day being set does
  not move: before, a clock set back kept sessions for as long, and one
  set forward ended them all. The loop reads it once a turn, in u->now.

With epoll, a datagram costs the same with 20000 idle sessions as with
none (38-40 us of the proxy's CPU a round trip, ARM), where it took 59
before. poll() elsewhere still builds its set every turn.

Tests: a session heard from every half second lives through two seconds
of idle timeout, and ends two seconds after the last datagram.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Vladimir DubrovinandClaude Opus 5.5 committed 2026-10-22 23:53:06 +03:00
1 parent f69eb13e9a
commit 57818ad497
6 files changed
+266 -48

No files matched your search

+14 -1
View File
@@ -249,8 +249,17 @@ struct udpsession {
struct udpsession *next, *prev;
struct clientparam *param;
const struct udpservice *svc;
time_t deadline; /* when it ends unless refreshed */
/* when it ends unless refreshed, in ms of monotime() (u->now), 0:
never; a handler may set it, later or earlier */
uint64_t deadline;
int idle; /* seconds of silence it lasts, 0: deadline fixed */
/* the loop's timers: a heap ordered by hkey, where it is at hidx (-1:
not in it). hkey is the deadline as it was when it last went in or
up: a later deadline, as each datagram sets, leaves it where it is,
to be looked at again only once its old one comes. */
uint64_t hkey;
int hidx;
struct udpsession *dnext; /* the loop's list of those ended */
int keyed; /* found by its client's address */
int done; /* to be ended, param->res saying how */
short events; /* polled for on remsock, 0: POLLIN */
@@ -285,8 +294,12 @@ struct udploop {
struct clientparam *defparam;
/* scratch of UDPBUFSIZE for a handler */
unsigned char *buf;
/* monotime() as the loop last read it, for the deadlines */
uint64_t now;
};
/* an answer to the session's client, from the service's socket */
/* ms on a clock only going forward, whatever is done to the time of day */
uint64_t monotime(void);
int udpanswer(struct udploop *u, struct clientparam *param, unsigned char *buf, int len);
extern const struct udpservice udppmservice, dnsprservice;