-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
98 lines (83 loc) · 1.61 KB
/
base.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tailer
import (
"context"
"io"
"os"
"time"
"github.com/sirupsen/logrus"
)
// Tailer simple interface to control the tail-follow of the file or io.Reader
type Tailer interface {
Start() Tailer
Stop()
IsRunning() bool
WithPoll(time.Duration) Tailer
}
type tailer struct {
readFn func([]byte) (int, error)
out io.Writer
isRunning bool
ctx context.Context
cancel context.CancelFunc
poll time.Duration
}
// newTailer returns a new instance of Tailer
func newTailer(ctx context.Context, readFn func([]byte) (int, error), out io.Writer) Tailer {
if ctx == nil {
ctx = context.Background()
}
ctx2, cancel := context.WithCancel(ctx)
if out == nil {
out = os.Stderr
}
return &tailer{
readFn: readFn,
out: out,
ctx: ctx2,
cancel: cancel,
poll: time.Second,
}
}
func (f *tailer) loop() error {
var (
buf = make([]byte, 2048)
n int
err error
)
f.isRunning = true
defer func() { f.isRunning = false }()
for {
if n, err = f.readFn(buf); n > 0 {
logrus.Tracef("tailer: Read %d bytes", n)
f.out.Write(buf[0:n])
}
if n == 0 || err == io.EOF {
goto labSleep
} else if err != nil {
logrus.WithError(err).Warnf("tailer: Error reading")
return err
}
labSleep:
select {
case <-time.After(f.poll):
// nop
case <-f.ctx.Done():
logrus.Debugf("tailer loop done.")
return f.ctx.Err()
}
}
}
func (f *tailer) Start() Tailer {
go f.loop()
return f
}
func (f *tailer) Stop() {
f.cancel()
}
func (f *tailer) IsRunning() bool {
return f.isRunning
}
func (f *tailer) WithPoll(dur time.Duration) Tailer {
f.poll = dur
return f
}