- bufchan := make(chan *bytes.Buffer)
- bufterm := make(chan bool)
-
- // Use a separate goroutine for the actual write so that the writes are
- // actually initiated closer every 1s instead of every
- // 1s + (time to it takes to write).
- go goWriter(tl.writer, bufchan, bufterm)
-
- // We use a separate "stopping" var here to ensure we flush
- // tl.buf after tl.stop becomes true.
- stopping := false
- for !stopping {
- time.Sleep(time.Second)
- stopping = tl.stop
- tl.Mutex.Lock()
- if tl.buf != nil && tl.buf.Len() > 0 {
- oldbuf := tl.buf
- tl.buf = nil
- bufchan <- oldbuf
+ ticker := time.NewTicker(time.Second)
+ defer ticker.Stop()
+ for stopping := false; !stopping; {
+ select {
+ case <-tl.stopping:
+ // flush tl.buf, then exit the loop
+ stopping = true
+ case <-ticker.C: