Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ result before returning. Listener shutdown still allows accepted connections
to drain before releasing the socket. Treat this as an experimental option:
measure playback quality and CPU under your workload before enabling it.

For two parallel writers, also pass `-udp-send-writers 2` (or
`relaynet.WithUDPSendWriters(2)`). This is opt-in and requires batching.
Destinations stay on a fixed writer; both Go handles share the source socket
and port. The total queue remains 256 writes, divided between the writers.
The default is one writer. With two writers an observer can be called
concurrently, and summed socket-call durations can exceed elapsed time.

Subgroup fanout reuses empty inbox storage after each writer has finished.
The relay-wide cache retains at most 1024 queues, only for queue capacities up
to 256 objects; larger queues use the ordinary allocation path. Closing a
subgroup still drains its queued objects before completing.

For send-path diagnostics, pass `relaynet.WithUDPSendMetrics(observer, 128)`
alongside batching. `UDPSendMetrics` observes sampled queue/admission wait and
every socket call's requested/sent message counts, duration and error. Partial
Expand Down
3 changes: 2 additions & 1 deletion cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func main() {
addr := flag.String("addr", "0.0.0.0:4433", "listen address")
udpSendBatchSize := flag.Int("udp-send-batch-size", 0,
"maximum ready UDP writes per Linux batch (0 disables, 1-64 enables)")
udpSendWriters := flag.Int("udp-send-writers", 1, "Linux UDP batch writers (1 or 2; two requires batching)")
certFile := flag.String("cert", "", "TLS certificate file (PEM); generated if empty")
keyFile := flag.String("key", "", "TLS private key file (PEM); generated if empty")
wtPath := flag.String(
Expand Down Expand Up @@ -137,7 +138,7 @@ func main() {
}

listener, err := relaynet.Listen(*addr, *wtPath, tlsCfg, logger,
relaynet.WithUDPSendBatchSize(*udpSendBatchSize))
relaynet.WithUDPSendBatchSize(*udpSendBatchSize), relaynet.WithUDPSendWriters(*udpSendWriters))
if err != nil {
log.Fatalf("listen %s: %v", *addr, err)
}
Expand Down
55 changes: 39 additions & 16 deletions pkg/relay/handler_fanout.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,13 +801,23 @@ func (h *sessionHandler) openWriterForSub(
// reading (see [joinWriters]), and resets the open stream of one that
// cancelled (§3.1.2).
ioCtx, cancelIO := context.WithCancel(ctx)
var inbox chan fwdObject
var inboxDone chan struct{}
if h.subgroupInboxes != nil {
inbox = h.subgroupInboxes.get()
inboxDone = make(chan struct{})
} else {
inbox = make(chan fwdObject, h.sendQueueSize)
}
w := &subgroupWriter{
sub: sub,
ctx: ioCtx,
cancelIO: cancelIO,
unwatchSub: context.AfterFunc(sub.Cancelled(), cancelIO),
hdr: subHdr,
inbox: make(chan fwdObject, h.sendQueueSize),
inbox: inbox,
inboxDone: inboxDone,
inboxPool: h.subgroupInboxes,
done: make(chan struct{}),
log: h.log,
metrics: h.metrics,
Expand Down Expand Up @@ -852,6 +862,8 @@ type subgroupWriter struct {
out *session.OutgoingSubgroupStream // nil until run opens it lazily
unbridge func() bool // stops the current stream's ctx→Cancel bridge
inbox chan fwdObject
inboxDone chan struct{}
inboxPool *subgroupInboxPool
done chan struct{}
log *slog.Logger
metrics Metrics
Expand Down Expand Up @@ -981,27 +993,28 @@ func (w *subgroupWriter) publish(fwd fwdObject) {
w.dropsMu.Unlock()
return
}
w.dropsMu.Unlock()

fwd.enqueuedAt = time.Now()
select {
case w.inbox <- fwd:
w.dropsMu.Unlock()
w.metrics.ObjectForwarded(w.ref, w.hdr.SubgroupID)
default:
w.metrics.ObjectDropped(w.ref, w.hdr.SubgroupID)
w.lastPos = inboundPos{} // the next Object does not follow a sent one
w.dropsMu.Lock()
w.lastPos = inboundPos{}
w.drops++
w.markIncompleteLocked(moqt.StreamResetExcessiveLoad)
drops := w.drops
capped := w.maxDropsBeforeReset > 0 && w.drops > w.maxDropsBeforeReset
w.dropsMu.Unlock()

w.log.Debug("fanout: dropped object on full inbox",
"sub_id", w.sub.ID, "drops", drops)
w.metrics.ObjectDropped(w.ref, w.hdr.SubgroupID)
w.log.Debug("fanout: dropped object on full inbox", "sub_id", w.sub.ID, "drops", drops)
if capped {
w.log.Warn("fanout: subscriber hit MaxDropsBeforeReset cap, terminating",
"sub_id", w.sub.ID, "drops", drops)
w.log.Warn(
"fanout: subscriber hit MaxDropsBeforeReset cap, terminating",
"sub_id",
w.sub.ID,
"drops",
drops,
)
w.close(true, moqt.StreamResetExcessiveLoad)
}
}
Expand Down Expand Up @@ -1063,6 +1076,7 @@ func (w *subgroupWriter) dropOut() {
// draining, so publish never blocks.
func (w *subgroupWriter) run() {
defer close(w.done)
defer w.releaseInbox()
defer w.unwatchSub()

var (
Expand Down Expand Up @@ -1120,7 +1134,7 @@ func (w *subgroupWriter) run() {
}()

// failWrites latches this writer broken and stops contributors
// enqueueing. Only close, under sg.Mu, closes the inbox.
// enqueueing. Only close, under sg.Mu, ends normal draining.
var writeFailedLatched bool
failWrites := func() {
writeFailed = true
Expand All @@ -1134,7 +1148,7 @@ func (w *subgroupWriter) run() {

var lagExceeded bool
queueMetrics, _ := w.metrics.(ObjectQueueMetrics)
for fwd := range w.inbox {
for fwd, ok := w.nextObject(); ok; fwd, ok = w.nextObject() {
if queueMetrics != nil {
queueMetrics.ObjectDequeued(w.ref, false, fwd.enqueuedAt, fwd.headerAt)
}
Expand Down Expand Up @@ -1221,6 +1235,11 @@ func (w *subgroupWriter) run() {
// it SHOULD.
}

w.finishStream(lagExceeded, writeFailed)
}

// finishStream applies the terminal stream/reset state after the inbox drained.
func (w *subgroupWriter) finishStream(lagExceeded, writeFailed bool) {
w.dropsMu.Lock()
dropCapped := w.maxDropsBeforeReset > 0 && w.drops > w.maxDropsBeforeReset
inboundReset := w.inboundReset
Expand All @@ -1238,7 +1257,7 @@ func (w *subgroupWriter) run() {
cause = ResetCauseExcessiveLoad
}
w.metrics.SubscriptionResetSlowReader(w.ref, cause)
// Refuse further enqueues; only close may close the inbox.
// Refuse further enqueues; close still owns the drain boundary.
w.dropsMu.Lock()
w.closed = true
w.dropsMu.Unlock()
Expand Down Expand Up @@ -1349,7 +1368,7 @@ func (w *subgroupWriter) applyPriority() {
))
}

// close closes the inbox, recording whether the writer ends its stream with
// close stops admission, recording whether the writer ends its stream with
// a reset (and code) or a FIN. The first call wins. Queued objects are still
// written; a wedged writer is bounded by [joinWriters].
func (w *subgroupWriter) close(reset bool, code moqt.StreamResetCode) {
Expand All @@ -1359,7 +1378,11 @@ func (w *subgroupWriter) close(reset bool, code moqt.StreamResetCode) {
w.inboundReset = reset
w.inboundResetCode = code
w.dropsMu.Unlock()
close(w.inbox)
if w.inboxDone != nil {
close(w.inboxDone)
} else {
close(w.inbox)
}
})
}

Expand Down
27 changes: 16 additions & 11 deletions pkg/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ type Config struct {
// more memory locked up.
// Datagrams share one queue of this size per downstream session, across
// all its subscriptions, so a congested connection cannot stall others.
// Zero means: use the default of 64.
// Zero means: use the default of 64. Subgroup inbox storage is reused in
// a relay-wide cache of at most 1024 empty queues when this is at most 256;
// larger configured queues keep the ordinary allocation path.
SendQueueSize int

// MaxFanoutLag bounds how far behind the live edge a downstream
Expand Down Expand Up @@ -331,7 +333,8 @@ type Relay struct {

// statusRounds shares forwarded TRACK_STATUS rounds across every
// session handler; see [sessionHandler.forwardTrackStatus].
statusRounds *trackStatusRounds
statusRounds *trackStatusRounds
subgroupInboxes *subgroupInboxPool

// upstreams dials and pools relay-to-relay sessions for Discovery-driven
// cross-relay upstream SUBSCRIBE. nil when Config.Dialer is unset (the
Expand Down Expand Up @@ -432,15 +435,16 @@ func New(listener Listener, cfg Config) *Relay {
}

r := &Relay{
listener: listener,
cfg: cfg,
log: log.With("component", "relay"),
tracks: registry.NewTrackRegistry(trackOpts...),
names: registry.NewNamespaceRegistry(nameOpts...),
fetch: registry.NewFetchRouter(),
statusRounds: newTrackStatusRounds(),
sessions: make(map[*session.Session]struct{}),
stopCh: make(chan struct{}),
listener: listener,
cfg: cfg,
log: log.With("component", "relay"),
tracks: registry.NewTrackRegistry(trackOpts...),
names: registry.NewNamespaceRegistry(nameOpts...),
fetch: registry.NewFetchRouter(),
statusRounds: newTrackStatusRounds(),
subgroupInboxes: newSubgroupInboxPool(cfg.SendQueueSize),
sessions: make(map[*session.Session]struct{}),
stopCh: make(chan struct{}),
}
if cfg.PauseUnwatchedPublishes > 0 {
r.pauser = &unwatchedPauser{delay: cfg.PauseUnwatchedPublishes, stop: r.stopCh, run: r.handlers.Go}
Expand Down Expand Up @@ -724,6 +728,7 @@ func (r *Relay) serveSession(ctx context.Context, sess *session.Session, leg Leg
auth, r.cfg.Metrics, leg, r.fetch, r.statusRounds, r.upstreams,
r.cfg.Discovery, r.cfg.RelayAddr,
r.cfg.SendQueueSize, r.cfg.MaxDropsBeforeReset, r.cfg.MaxFanoutLag,
r.subgroupInboxes,
r.cfg.MaxSubscriptionsPerSession, r.cfg.MaxNamespaceRequestsPerSession,
max(r.cfg.MaxRendezvousTimeout, 0),
r.handlers.Go,
Expand Down
2 changes: 1 addition & 1 deletion pkg/relay/relaynet/dual.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Listen(addr, wtPath string, tlsCfg *tls.Config, logger *slog.Logger, opts .
// SETUP as 0.5-RTT data to a peer whose certificate is unverified. 0-RTT would
// need Config.Allow0RTT, which defaultQUICConfig deliberately leaves unset.
ql, closeSocket, err := listenQUIC(
addr, tlsCfg, qcfg, cfg.udpSendBatchSize, cfg.udpSendMetrics, cfg.udpSendSampleEvery,
addr, tlsCfg, qcfg, cfg.udpSendBatchSize, cfg.udpSendWriters, cfg.udpSendMetrics, cfg.udpSendSampleEvery,
)
if err != nil {
return nil, fmt.Errorf("relaynet: listen %s: %w", addr, err)
Expand Down
36 changes: 28 additions & 8 deletions pkg/relay/relaynet/dual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@ import (
// keep dialing raw QUIC directly: the two do not have to agree on a transport.
func TestDualListenerServesBothTransports(t *testing.T) {
t.Parallel()
for _, size := range []int{0, 32} {
t.Run(fmt.Sprintf("batch-%d", size), func(t *testing.T) { testDualListenerTransports(t, size) })
for _, mode := range []struct{ batch, writers int }{{0, 1}, {32, 1}, {32, 2}} {
t.Run(
fmt.Sprintf("batch-%d-writers-%d", mode.batch, mode.writers),
func(t *testing.T) { testDualListenerTransports(t, mode.batch, mode.writers) },
)
}
}

func testDualListenerTransports(t *testing.T, batchSize int) {
func testDualListenerTransports(t *testing.T, batchSize, writers int) {
const wtPath = "/moq"

tlsCfg, err := relaynet.TLSConfig("", "", relaynet.DualALPNs)
if err != nil {
t.Fatalf("TLSConfig: %v", err)
}
l, err := relaynet.Listen("127.0.0.1:0", wtPath, tlsCfg, nil, relaynet.WithUDPSendBatchSize(batchSize))
l, err := relaynet.Listen(
"127.0.0.1:0",
wtPath,
tlsCfg,
nil,
relaynet.WithUDPSendBatchSize(batchSize),
relaynet.WithUDPSendWriters(writers),
)
if err != nil {
t.Fatalf("Listen: %v", err)
}
Expand Down Expand Up @@ -102,17 +112,27 @@ func testDualListenerTransports(t *testing.T, batchSize int) {
// GOAWAY. An earlier version of this listener did exactly that.
func TestDualListenerCloseKeepsLiveSessions(t *testing.T) {
t.Parallel()
for _, size := range []int{0, 32} {
t.Run(fmt.Sprintf("batch-%d", size), func(t *testing.T) { testDualListenerDrain(t, size) })
for _, mode := range []struct{ batch, writers int }{{0, 1}, {32, 1}, {32, 2}} {
t.Run(
fmt.Sprintf("batch-%d-writers-%d", mode.batch, mode.writers),
func(t *testing.T) { testDualListenerDrain(t, mode.batch, mode.writers) },
)
}
}

func testDualListenerDrain(t *testing.T, batchSize int) {
func testDualListenerDrain(t *testing.T, batchSize, writers int) {
tlsCfg, err := relaynet.TLSConfig("", "", relaynet.DualALPNs)
if err != nil {
t.Fatalf("TLSConfig: %v", err)
}
l, err := relaynet.Listen("127.0.0.1:0", "/moq", tlsCfg, nil, relaynet.WithUDPSendBatchSize(batchSize))
l, err := relaynet.Listen(
"127.0.0.1:0",
"/moq",
tlsCfg,
nil,
relaynet.WithUDPSendBatchSize(batchSize),
relaynet.WithUDPSendWriters(writers),
)
if err != nil {
t.Fatalf("Listen: %v", err)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/relay/relaynet/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type config struct {
quic *quic.Config
wt webtransport.Config
udpSendBatchSize int
udpSendWriters int
udpSendMetrics UDPSendMetrics
udpSendSampleEvery uint32
}
Expand All @@ -32,6 +33,15 @@ func WithUDPSendBatchSize(size int) Option {
return func(c *config) { c.udpSendBatchSize = size }
}

// WithUDPSendWriters opts Listen into one or two Linux UDP batch writers.
// Zero and one select the default single writer. Two requires a positive
// WithUDPSendBatchSize and uses separate Go handles to the same socket, with
// stable destination affinity and a shared total budget of 256 queued writes.
// Other operating systems keep their ordinary socket. Dial options ignore it.
func WithUDPSendWriters(count int) Option {
return func(c *config) { c.udpSendWriters = count }
}

// WithQUICConfig returns an [Option] that runs tune over the [quic.Config] the
// entry point is about to use, once the relay's defaults have been populated.
// Passing it more than once applies the hooks in order.
Expand Down
Loading
Loading