forked from heroku/log-shuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.go
More file actions
47 lines (37 loc) · 774 Bytes
/
reader.go
File metadata and controls
47 lines (37 loc) · 774 Bytes
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
package main
import (
"bufio"
"io"
"time"
)
type LogLine struct {
line []byte
when time.Time
}
type Reader struct {
Outbox chan LogLine
stats chan<- NamedValue
}
func NewReader(frontBuff int, stats chan<- NamedValue) *Reader {
return &Reader{
Outbox: make(chan LogLine, frontBuff),
stats: stats,
}
}
func (rdr *Reader) Read(input io.ReadCloser) error {
rdrIo := bufio.NewReader(input)
lastLogTime := time.Now()
for {
line, err := rdrIo.ReadBytes('\n')
currentLogTime := time.Now()
if err != nil {
input.Close()
return err
}
logLine := LogLine{line, currentLogTime}
rdr.Outbox <- logLine
rdr.stats <- NewNamedValue("reader.line.delay", currentLogTime.Sub(lastLogTime).Seconds())
lastLogTime = currentLogTime
}
return nil
}