12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package writers
- import (
- "io"
- "time"
- )
- type SpreadWriter struct {
- w io.Writer
- interval time.Duration
- buf [][]byte
- exitCh chan int
- }
- func NewSpreadWriter(w io.Writer, interval time.Duration, exitCh chan int) *SpreadWriter {
- return &SpreadWriter{
- w: w,
- interval: interval,
- buf: make([][]byte, 0),
- exitCh: exitCh,
- }
- }
- func (s *SpreadWriter) Write(p []byte) (int, error) {
- b := make([]byte, len(p))
- copy(b, p)
- s.buf = append(s.buf, b)
- return len(p), nil
- }
- func (s *SpreadWriter) Flush() {
- sleep := s.interval / time.Duration(len(s.buf))
- ticker := time.NewTicker(sleep)
- for _, b := range s.buf {
- s.w.Write(b)
- select {
- case <-ticker.C:
- case <-s.exitCh: // skip sleeps finish writes
- }
- }
- ticker.Stop()
- s.buf = s.buf[:0]
- }
|