-
Notifications
You must be signed in to change notification settings - Fork 35
/
async_writer_syncer.go
62 lines (54 loc) · 1.25 KB
/
async_writer_syncer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package log
import (
"io"
"go.uber.org/multierr"
)
func AddSync(w io.Writer) AsyncWriter {
switch w := w.(type) {
case AsyncWriter:
return w
default:
return writerWrapper{w}
}
}
type multiAsyncWriteSyncer []AsyncWriter
// NewMultiWriteSyncer creates a WriteSyncer that duplicates its writes
// and sync calls, much like io.MultiWriter.
func NewMultiWriteSyncer(ws ...AsyncWriter) AsyncWriter {
if len(ws) == 1 {
return ws[0]
}
return multiAsyncWriteSyncer(ws)
}
// See https://golang.org/src/io/multi.go
// When not all underlying syncers write the same number of bytes,
// the smallest number is returned even though Write() is called on
// all of them.
func (ws multiAsyncWriteSyncer) Write(p []byte) (int, error) {
var writeErr error
nWritten := 0
for _, w := range ws {
n, err := w.Write(p)
writeErr = multierr.Append(writeErr, err)
if nWritten == 0 && n != 0 {
nWritten = n
} else if n < nWritten {
nWritten = n
}
}
return nWritten, writeErr
}
func (ws multiAsyncWriteSyncer) Sync() error {
var err error
for _, w := range ws {
err = multierr.Append(err, w.Sync())
}
return err
}
func (ws multiAsyncWriteSyncer) Stop() error {
var err error
for _, w := range ws {
err = multierr.Append(err, w.Stop())
}
return err
}