forked from presslabs/dockerize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tail.go
56 lines (46 loc) · 1.37 KB
/
tail.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
package main
import (
"io"
"log"
"os"
"golang.org/x/net/context"
"github.com/presslabs/dockerize/pkg/tail"
)
const (
stdioBufSize = 4096
)
// here is some syntaxic sugar inspired by the Tomas Senart's video,
// it allows me to inline the Reader interface
type readerFunc func(p []byte) (n int, err error)
func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }
func tailFile(ctx context.Context, file string, dest *os.File) {
defer wg.Done()
t, err := tail.NewTail(file)
if err != nil {
log.Printf("cannot tail file %s: %s", file, err)
return
}
for true {
// Copy will call the Reader and Writer interface multiple time, in order
// to copy by chunk (avoiding loading the whole file in memory).
// I insert the ability to cancel before read time as it is the earliest
// possible in the call process.
buf := make([]byte, stdioBufSize)
_, err = io.CopyBuffer(dest, readerFunc(func(p []byte) (int, error) {
// golang non-blocking channel: https://gobyexample.com/non-blocking-channel-operations
select {
// if context has been canceled
case <-ctx.Done():
// stop process and propagate "context canceled" error
return 0, ctx.Err()
default:
// otherwise just run default io.Reader implementation
return t.Read(p)
}
}), buf)
if err != nil {
log.Printf("error while tailing file %s: %s", file, err)
return
}
}
}