batch.go 1.2 KB

123456789101112131415161718192021222324252627282930313233
  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. Commit(pkt []byte) error
  8. // Flush emits every queued packet in arrival order. Returns the
  9. // first error observed; keeps draining so one bad packet doesn't hold up
  10. // the rest. After Flush returns, borrowed payload slices may be recycled.
  11. Flush() error
  12. }
  13. type TxBatcher interface {
  14. // Next returns a zero-length slice with slotCap capacity over the next unused
  15. // slot's backing bytes. The caller writes into the returned slice and then
  16. // calls Commit with the final length and destination. Next returns nil when
  17. // the batch is full.
  18. Next() []byte
  19. // Commit records the slot just returned by Next as a packet of length n
  20. // destined for dst.
  21. Commit(n int, dst netip.AddrPort)
  22. // Reset clears committed slots; backing storage is retained for reuse.
  23. Reset()
  24. // Len returns the number of committed packets.
  25. Len() int
  26. // Cap returns the maximum number of slots in the batch.
  27. Cap() int
  28. // Get returns the buffers needed to send the batch
  29. Get() ([][]byte, []netip.AddrPort)
  30. }