batch.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package batch
  2. import "net/netip"
  3. type RxBatcher interface {
  4. // Reserve creates a pkt to borrow
  5. Reserve(sz int) []byte
  6. // Commit borrows pkt. The caller must keep pkt valid until the next Flush.
  7. // Walks IP+L4 headers itself; prefer CommitInbound when the caller already
  8. // has an RxParsed in hand from ParseInbound.
  9. Commit(pkt []byte) error
  10. // CommitInbound is Commit with a hint produced by ParseInbound, so the
  11. // batcher can skip the IP+L4 re-parse. Borrowed slice contract is the
  12. // same as Commit. Implementations that don't coalesce may delegate to
  13. // Commit.
  14. CommitInbound(pkt []byte, parsed *RxParsed) error
  15. // Flush emits every queued packet in arrival order. Returns the
  16. // first error observed; keeps draining so one bad packet doesn't hold up
  17. // the rest. After Flush returns, borrowed payload slices may be recycled.
  18. Flush() error
  19. }
  20. type TxBatcher interface {
  21. // Reserve creates a pkt to borrow
  22. Reserve(sz int) []byte
  23. // Commit borrows pkt and records its destination plus the 2-bit
  24. // IP-level ECN codepoint to set on the outer (carrier) header. The
  25. // caller must keep pkt valid until the next Flush. Pass 0 (Not-ECT)
  26. // to leave the outer ECN field unset.
  27. Commit(pkt []byte, dst netip.AddrPort, outerECN byte)
  28. // Flush emits every queued packet via the underlying batch writer in
  29. // arrival order. Returns an errors.Join of one or more errors. After Flush returns,
  30. // borrowed payload slices may be recycled.
  31. Flush() error
  32. }