/
stream.go
153 lines (130 loc) · 2.88 KB
/
stream.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package kinesis
import (
"fmt"
"log"
"text/template"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis"
"github.com/gliderlabs/logspout/router"
)
// StreamNotReadyError is returned while the stream is being created.
type StreamNotReadyError struct {
Stream string
}
func (e *StreamNotReadyError) Error() string {
return fmt.Sprintf("not ready, stream: %s", e.Stream)
}
// Stream represents a stream that will send messages to its writer.
type Stream struct {
client Client
name string
tags *map[string]*string
writers map[string]*writer
pKeyTmpl *template.Template
ready bool
readyWrite chan bool
err error
errChan chan error
}
// NewStream instantiates a new stream.
func NewStream(name string, tags *map[string]*string, pKeyTmpl *template.Template) *Stream {
session := session.New(&aws.Config{})
client := &client{
kinesis: kinesis.New(session),
}
s := &Stream{
client: client,
name: name,
tags: tags,
writers: make(map[string]*writer),
pKeyTmpl: pKeyTmpl,
readyWrite: make(chan bool),
errChan: make(chan error),
}
return s
}
// Start runs the goroutines making calls to create and tag the stream on
// AWS.
func (s *Stream) Start() {
go s.start()
}
func (s *Stream) start() {
if err := s.create(); err != nil {
s.errChan <- err
ErrorHandler(err)
return
}
if err := s.tag(); err != nil {
s.errChan <- err
ErrorHandler(err)
return
}
s.readyWrite <- true
log.Printf("ready! stream: %s", s.name)
}
// Write sends the message to the writer if the stream is ready
// i.e created and tagged.
func (s *Stream) Write(m *router.Message) error {
select {
case s.ready = <-s.readyWrite:
case s.err = <-s.errChan:
default:
}
switch {
case s.err != nil:
return s.err
case s.ready:
return s.write(m)
default:
return &StreamNotReadyError{Stream: s.name}
}
}
func (s *Stream) write(m *router.Message) error {
if w, ok := s.writers[m.Container.ID]; ok {
w.write(m)
return nil
}
w := newWriter(
newBuffer(s.pKeyTmpl, s.name),
newFlusher(s.client),
)
w.start()
s.writers[m.Container.ID] = w
w.write(m)
return nil
}
func (s *Stream) create() error {
created, err := s.client.Create(&kinesis.CreateStreamInput{
ShardCount: aws.Int64(1),
StreamName: aws.String(s.name),
})
if err != nil {
return err
}
if created {
return nil
}
debug("need to create stream: %s", s.name)
for {
status := s.client.Status(&kinesis.DescribeStreamInput{
StreamName: aws.String(s.name),
})
if status == "ACTIVE" {
return nil
}
time.Sleep(4 * time.Second) // wait a bit
debug("stream %s status: %s", s.name, status)
}
}
func (s *Stream) tag() error {
err := s.client.Tag(&kinesis.AddTagsToStreamInput{
StreamName: aws.String(s.name),
Tags: *s.tags,
})
if err != nil {
return err
}
return nil
}