mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-09 02:53:09 -05:00
derp.Client.ForwardPacket flushed on every call, so each packet a derper forwards to a mesh peer cost its own TLS record and write syscall, serialized behind the client's write mutex with the syscall inside the critical section. ForwardPacket now appends the frame to one of two 16 KiB buffers and returns. A flusher goroutine, started if none is running, swaps the buffer for the spare under the mutex and writes it with the mutex released, so callers keep filling the spare while a write is in progress and frames from concurrent callers share a write. A caller that finds the spare full waits for the write to complete, preserving backpressure. The buffers are allocated on first forward, so only mesh clients carry them. Forward buffers are written directly to the connection rather than through the client's bufio.Writer. A new mutex serializes those writes with the client's other senders, since not every Conn tolerates concurrent writes and bufio can split one frame over several. A failed write is logged with the number of frames dropped, closes the connection, wakes waiting callers with the error, and is returned by every later call, so derphttp reconnects as before. Adds BenchmarkForwardPacket, which also reports percentiles of ForwardPacket's own latency, unit tests for batching, backpressure, oversized frames, write failure, and write serialization, and an end-to-end test that concurrently forwarded packets all reach the destination. Updates tailscale/corp#50628 Signed-off-by: Mike O'Driscoll <mikeo@tailscale.com>