Skip to main content

Module udp

Module udp 

Source
Expand description

UDP stream proxy: relay datagrams between clients and a load-balanced upstream, tracking one session per client address.

UDP has no connections, so a single owning task demultiplexes the shared listener socket by client address into a session map (no lock needed — the map is touched only by that task). Each session owns an ephemeral socket connected to the chosen upstream and a reader task that pumps upstream replies back to the client. A session is torn down after idle with no traffic in either direction (tracked by a shared last_seen), reaped by a 1-second prune tick.

Structs§

Session 🔒
One client’s proxy session: the ephemeral upstream socket, shared activity clock, liveness flag, and the in-flight guard held for the session’s life.

Constants§

BUF_SIZE 🔒
Max UDP payload plus headroom.

Functions§

create_session 🔒
Creates a session for client: an ephemeral socket connected to a selected upstream, plus a spawned reader task pumping replies back to the client.
now_ms 🔒
reader 🔒
Pumps upstream replies back to the client, exiting when the session has been idle in both directions for idle (so an active client with a briefly quiet upstream is not torn down).
spawn
Binds the UDP listener (fail-fast) and spawns its receive loop, returning the bound address (the OS-assigned port when cfg.port == 0).