passthrough.go 748 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package batch
  2. import (
  3. "io"
  4. )
  5. // Passthrough is MultiCoalescer's verbatim lane: no batching, packets are written at Flush in the
  6. // order enqueued.
  7. type Passthrough struct {
  8. out io.Writer
  9. slots [][]byte
  10. }
  11. func NewPassthrough(w io.Writer) *Passthrough {
  12. return &Passthrough{
  13. out: w,
  14. slots: make([][]byte, 0, 128),
  15. }
  16. }
  17. // enqueue accepts one packet, already sorted into transmission order by dispatch.
  18. func (p *Passthrough) enqueue(pkt []byte) error {
  19. p.slots = append(p.slots, pkt)
  20. return nil
  21. }
  22. func (p *Passthrough) Flush() error {
  23. var firstErr error
  24. for _, s := range p.slots {
  25. _, err := p.out.Write(s)
  26. if err != nil && firstErr == nil {
  27. firstErr = err
  28. }
  29. }
  30. clear(p.slots)
  31. p.slots = p.slots[:0]
  32. return firstErr
  33. }