-
Notifications
You must be signed in to change notification settings - Fork 15
/
reader.go
119 lines (93 loc) · 2.29 KB
/
reader.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cloudwatch
import (
"bytes"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
// Reader is an io.Reader implementation that streams log lines from cloudwatch
// logs.
type Reader struct {
group, stream, nextToken *string
client client
throttle <-chan time.Time
b lockingBuffer
// If an error occurs when getting events from the stream, this will be
// populated and subsequent calls to Read will return the error.
err error
}
func NewReader(group, stream string, client *cloudwatchlogs.CloudWatchLogs) *Reader {
return newReader(group, stream, client)
}
func newReader(group, stream string, client client) *Reader {
r := &Reader{
group: aws.String(group),
stream: aws.String(stream),
client: client,
throttle: time.Tick(readThrottle),
}
go r.start()
return r
}
func (r *Reader) start() {
for {
<-r.throttle
if r.err = r.read(); r.err != nil {
return
}
}
}
func (r *Reader) read() error {
params := &cloudwatchlogs.GetLogEventsInput{
LogGroupName: r.group,
LogStreamName: r.stream,
StartFromHead: aws.Bool(true),
NextToken: r.nextToken,
}
resp, err := r.client.GetLogEvents(params)
if err != nil {
return err
}
// We want to re-use the existing token in the event that
// NextForwardToken is nil, which means there's no new messages to
// consume.
if resp.NextForwardToken != nil {
r.nextToken = resp.NextForwardToken
}
// If there are no messages, return so that the consumer can read again.
if len(resp.Events) == 0 {
return nil
}
for _, event := range resp.Events {
r.b.WriteString(*event.Message)
}
return nil
}
func (r *Reader) Read(b []byte) (int, error) {
// Return the AWS error if there is one.
if r.err != nil {
return 0, r.err
}
// If there is not data right now, return. Reading from the buffer would
// result in io.EOF being returned, which is not what we want.
if r.b.Len() == 0 {
return 0, nil
}
return r.b.Read(b)
}
// lockingBuffer is a bytes.Buffer that locks Reads and Writes.
type lockingBuffer struct {
sync.Mutex
bytes.Buffer
}
func (r *lockingBuffer) Read(b []byte) (int, error) {
r.Lock()
defer r.Unlock()
return r.Buffer.Read(b)
}
func (r *lockingBuffer) Write(b []byte) (int, error) {
r.Lock()
defer r.Unlock()
return r.Buffer.Write(b)
}