-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
87 lines (72 loc) · 1.81 KB
/
writer.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
package zapcloudwatch
import (
"context"
"errors"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
)
var (
ErrBufferStillFull = errors.New("buffer still full after repeated sync-on-write")
)
type BufferedWriter struct {
buf *Buffer
st chan *string
cl CloudWatchClient
group string
stream string
}
func NewBufferedWriter(
cl CloudWatchClient,
group, stream string,
buf *Buffer,
startSeqTok *string,
) (w *BufferedWriter) {
w = &BufferedWriter{
buf: buf,
st: make(chan *string, 1),
cl: cl,
group: group,
stream: stream}
w.st <- startSeqTok
return
}
func (w *BufferedWriter) Write(ts int64, msg string) error {
fmt.Println(msg)
for i := 0; i < 2; i++ {
full, disc := w.buf.Push(types.InputLogEvent{Timestamp: &ts, Message: &msg})
if disc || !full {
return nil
}
fmt.Println("FULL!")
// buffer is full, sync in place and retry push on next iteration
if err := w.Sync(); err != nil {
return fmt.Errorf("failed to sync after write: %w", err)
}
}
return ErrBufferStillFull
}
func (w *BufferedWriter) Sync() error {
token, ctx := <-w.st, context.Background()
nextToken, err := w.putLogs(ctx, w.buf.Batch(), token)
if err != nil {
w.st <- token
return fmt.Errorf("failed to put logs: %w", err)
}
w.st <- nextToken
return nil
}
func (w *BufferedWriter) putLogs(ctx context.Context, batch []types.InputLogEvent, tok *string) (next *string, err error) {
fmt.Println("LEN", len(batch))
in := &cloudwatchlogs.PutLogEventsInput{
LogEvents: batch,
LogGroupName: &w.group,
LogStreamName: &w.stream,
SequenceToken: tok,
}
out, err := w.cl.PutLogEvents(ctx, in)
if err != nil {
return nil, fmt.Errorf("failed to call PutLogEvents: %w", err)
}
return out.NextSequenceToken, nil
}