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

+23
View File
@@ -172,6 +172,29 @@ def run(t):
t.fail("two seconds of silence end it, logged with 92", "a 00092 line", "none")
s.close()
# --- a session heard from lives on past its first deadline, and ends
# its idle time after the last datagram, neither sooner nor much later
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
answered = 0
for i in range(10):
if exchange(s, idle, f"k{i}".encode()) == f"echo:k{i}".encode():
answered += 1
time.sleep(0.5)
cport = s.getsockname()[1]
last = time.time()
t.eq(10, answered, "a datagram every half second for five seconds is answered")
t.ok("and the session lives on through them") if not idle_ended() else \
t.fail("and the session lives on through them", "no 00092 line", "ended")
while time.time() < last + 6 and not idle_ended():
time.sleep(0.1)
took = time.time() - last
if idle_ended() and 1.5 <= took <= 3.5:
t.ok("then two seconds of silence end it")
else:
t.fail("then two seconds of silence end it", "ended 1.5-3.5 s after the last",
f"{'ended' if idle_ended() else 'not ended'} after {took:.1f} s")
s.close()
# --- maxconn: a client without a free slot is not answered -------------
held = [socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for _ in range(2)]
t.eq([b"echo:a", b"echo:a"], [exchange(h, limited, b"a") for h in held],