forked from phuslu/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync.go
61 lines (52 loc) · 1.16 KB
/
async.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
package log
import (
"io"
"sync"
)
// AsyncWriter is an Writer that writes asynchronously.
type AsyncWriter struct {
// ChannelSize is the size of the data channel, the default size is 1.
ChannelSize uint
// Writer specifies the writer of output.
Writer Writer
once sync.Once
ch chan *Entry
chClose chan error
}
// Close implements io.Closer, and closes the underlying Writer.
func (w *AsyncWriter) Close() (err error) {
w.ch <- nil
err = <-w.chClose
if closer, ok := w.Writer.(io.Closer); ok {
if err1 := closer.Close(); err1 != nil {
err = err1
}
}
return
}
// WriteEntry implements Writer.
func (w *AsyncWriter) WriteEntry(e *Entry) (int, error) {
w.once.Do(func() {
// channels
w.ch = make(chan *Entry, w.ChannelSize)
w.chClose = make(chan error)
go func() {
var err error
for entry := range w.ch {
if entry == nil {
break
}
_, err = w.Writer.WriteEntry(entry)
epool.Put(entry)
}
w.chClose <- err
}()
})
// cheating to logger pool
entry := epool.Get().(*Entry)
entry.Level = e.Level
entry.buf, e.buf = e.buf, entry.buf
w.ch <- entry
return len(entry.buf), nil
}
var _ Writer = (*AsyncWriter)(nil)