Simplify staged receive workers and buffer handoff

This commit is contained in:
JackDoan committed 2026-10-15 16:59:53 -05:00
1 parent 556747d9b7
commit 1fb77bdda5
5 files changed
+83 -82

No files matched your search

+24 -20
View File
@@ -246,22 +246,27 @@ func getFrom(names [][]byte, i int, isV4 bool) netip.AddrPort {
}
func (u *StdConn) ListenOut(r EncReader, flush func()) error {
return u.listenOut(r, flush, nil)
return u.ListenOutHandoff(r, func(bufs [][]byte) [][]byte {
flush()
return bufs
})
}
// ListenOutHandoff is ListenOut for a reader that keeps the packets: after each batch, handoff takes the batch's
// buffers and returns replacements, of the same count and at least the same size, to receive the next one into.
// HandoffShape is the count and size of the buffers ListenOutHandoff hands over after each batch.
func (u *StdConn) HandoffShape() (n, size int) {
if u.groSupported {
return u.batch, udpGROBufferSize
}
return u.batch, MTU
}
// ListenOutHandoff is the receive loop. After each batch, handoff takes the batch's buffers and returns the ones to
// receive the next batch into, in HandoffShape: the same ones for a reader that is done with the packets, or
// replacements for one that keeps them.
func (u *StdConn) ListenOutHandoff(r EncReader, handoff func(bufs [][]byte) [][]byte) error {
return u.listenOut(r, nil, handoff)
}
// listenOut is the receive loop. After each batch it calls flush, or with handoff set, hands the batch's buffers
// over and points the recvmmsg iovecs at the ones it gets back.
func (u *StdConn) listenOut(r EncReader, flush func(), handoff func(bufs [][]byte) [][]byte) error {
bufSize := MTU
_, bufSize := u.HandoffShape()
cmsgSpace := 0
if u.groSupported {
bufSize = udpGROBufferSize
cmsgSpace = unix.CmsgSpace(udpGROCmsgPayload)
}
msgs, buffers, names, _ := prepareRawMessages(u.batch, bufSize, cmsgSpace)
@@ -295,21 +300,20 @@ func (u *StdConn) listenOut(r EncReader, flush func(), handoff func(bufs [][]byt
deliverSegments(r, from, payload, segSize)
}
if handoff == nil {
flush()
continue
}
next := handoff(buffers)
if len(next) != len(buffers) {
panic(fmt.Sprintf("udp: handoff returned %d buffers, need %d", len(next), len(buffers)))
}
for i := range next {
if len(next[i]) < bufSize {
panic(fmt.Sprintf("udp: handoff buffer is %d bytes, need %d", len(next[i]), bufSize))
if &next[0] != &buffers[0] {
// Replacements, point the recvmmsg iovecs at them
for i := range next {
if len(next[i]) < bufSize {
panic(fmt.Sprintf("udp: handoff buffer is %d bytes, need %d", len(next[i]), bufSize))
}
msgs[i].Hdr.Iov.Base = &next[i][0]
}
msgs[i].Hdr.Iov.Base = &next[i][0]
buffers = next
}
buffers = next
}
}
+14 -7
View File
@@ -185,22 +185,29 @@ func (u *TesterConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, err
func (u *TesterConn) ListenOut(r EncReader, flush func()) error {
// The batcher borrows plaintext decrypted in place inside p.Data
// until Flush, so the packet must stay alive across flush()
return u.listenOut(r, func(p *Packet) { flush() })
return u.ListenOutHandoff(r, func(bufs [][]byte) [][]byte {
flush()
return bufs
})
}
// ListenOutHandoff hands each packet's buffer over, the replacement goes back to the pool.
// HandoffShape is one buffer per handoff; the pool resizes the replacements it gets back as it needs.
func (u *TesterConn) HandoffShape() (n, size int) { return 1, MTU }
// ListenOutHandoff hands each packet's buffer over as a batch of one, the buffer it gets back goes to the pool. Like
// StdConn it carries on with the slice handoff returns, since a reader that keeps the packets keeps the one it was
// given.
func (u *TesterConn) ListenOutHandoff(r EncReader, handoff func(bufs [][]byte) [][]byte) error {
return u.listenOut(r, func(p *Packet) { p.Data = handoff([][]byte{p.Data})[0] })
}
func (u *TesterConn) listenOut(r EncReader, finish func(*Packet)) error {
bufs := make([][]byte, 1)
for {
select {
case <-u.done:
return os.ErrClosed
case p := <-u.RxPackets:
r(p.From, p.Data[:len(p.Data):len(p.Data)])
finish(p)
bufs[0] = p.Data
bufs = handoff(bufs)
p.Data = bufs[0]
p.Release()
}
}