Skip to content

quic/server: wire SharedThreadedPacketWriter into QuicServer and QuicServerWorker#451

Open
afrind wants to merge 4 commits into
facebook:mainfrom
afrind:wire-shared-threaded-packet-writer
Open

quic/server: wire SharedThreadedPacketWriter into QuicServer and QuicServerWorker#451
afrind wants to merge 4 commits into
facebook:mainfrom
afrind:wire-shared-threaded-packet-writer

Conversation

@afrind
Copy link
Copy Markdown
Contributor

@afrind afrind commented May 15, 2026

QuicServer::setDrainEventBase() propagates a drain EventBase to each worker.
Each worker constructs a SharedThreadedPacketWriter backed by its listening
AsyncUDPSocket and installs a ConnectionPacketWriter into every new connection
before accept(). queueCapacity is fixed at 4096; maxSegmentsPerMsg is taken
from transportSettings_.maxBatchSize. Fatal-error and resume-producer callbacks
close connections and call scheduleWrite() respectively.

QuicTransportBaseLite gains setPacketWriter() so the worker can install a
ConnectionPacketWriter after connection creation. workerQuicEvb_ resolves
pre-existing TODO comments in bind() and applyAllSocketOptions().

afrind and others added 4 commits May 14, 2026 17:48
Uses eventfd on Linux (EFD_NONBLOCK; counter semantics coalesce multiple
flush() calls) and a non-blocking self-pipe on macOS. Wraps a lock-free
folly::ProducerConsumerQueue for the data path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Defines the write/flush/getResult contract for pluggable packet writers.
BufQuicBatchResult is moved from IoBufQuicBatch.h to break an include cycle
(IoBufQuicBatch → QuicBatchWriter → StateData). Adds conn.packetWriter
(unique_ptr<QuicPacketWriter>) to QuicConnectionStateBase; null on all
existing paths. Conditionalizes write/flush sites on packetWriter so the
inline path is unchanged when no writer is installed.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Drains an EventFdQueue<PacketEntry> on a dedicated EventBase, batching
packets with writemGSO and UDP_SEGMENT (GSO) coalescing where supported.

Key design points:
- GSO chains are restricted to same-connection packets; connIds_ is a flat
  per-slot vector (one entry per mmsg slot, the chain-opener's connId).
  segsInChain tracks the segment count per chain so the maxSegmentsPerMsg
  cap is applied to segments appended, not the mmsg slot index.
- Drain-thread arrays (bufs_, addrs_, opts_, connIds_, segCounts_) are
  preallocated to maxMsgsPerCall_ at construction; no per-batch heap
  allocation.
- bufs_ is preserved across EAGAIN so retryAndDrain resends the identical
  batch via EPOLLOUT without rebuilding.
- maxMsgsBeforeYield (default 10) controls drain-thread fairness.

ConnectionPacketWriter is a per-connection adaptor that forwards write/flush
to the shared writer with the connection's connId attached.

QuicTransportBaseLite gains scheduleWrite() to let the drain thread signal
the connection EVB when the producer should resume after backpressure.

Six USDT tracepoints (shared_packet_writer_{queue_full,enqueue,batch_result,
partial_send,drain_done,yield}) instrument the enqueue and drain paths.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ServerWorker

QuicServer::setDrainEventBase() propagates a drain EventBase to each worker.
Each worker constructs a SharedThreadedPacketWriter backed by its listening
AsyncUDPSocket and installs a ConnectionPacketWriter into every new connection
before accept(). queueCapacity is fixed at 4096; maxSegmentsPerMsg is taken
from transportSettings_.maxBatchSize. Fatal-error and resume-producer callbacks
close connections and call scheduleWrite() respectively.

QuicTransportBaseLite gains setPacketWriter() so the worker can install a
ConnectionPacketWriter after connection creation. workerQuicEvb_ resolves
pre-existing TODO comments in bind() and applyAllSocketOptions().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label May 15, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 15, 2026

@afrind has imported this pull request. If you are a Meta employee, you can view this in D105351238.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant