-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinewriter.go
46 lines (40 loc) · 979 Bytes
/
linewriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"io"
"sync"
)
// A SyncLineWriter is a LineWriter that is safe to use across goroutines.
type SyncLineWriter struct {
out io.Writer
buf chan string
wg sync.WaitGroup
}
// NewSyncLineWriter returns a SyncLineWriter that writes its lines out to the
// provided writer. It uses one goroutine, which is cleaned up when calling
// Close.
func NewSyncLineWriter(w io.Writer) *SyncLineWriter {
s := &SyncLineWriter{out: w, buf: make(chan string, 512)}
s.run()
return s
}
func (s *SyncLineWriter) run() {
s.wg.Add(1)
go func() {
for l := range s.buf {
s.out.Write([]byte(l))
}
s.wg.Done()
}()
}
// Close flushes the rest of the output and closes the writer.
// It is not legal to write to this LineBufferedWriter after closing.
func (s *SyncLineWriter) Close() error {
close(s.buf)
s.wg.Wait()
return nil
}
// WriteLine writes the str to this writer.
func (s *SyncLineWriter) WriteLine(str string) error {
s.buf <- str
return nil
}