-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathenvelope_stream_connector.go
205 lines (175 loc) · 4.79 KB
/
envelope_stream_connector.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package loggregator
import (
"context"
"crypto/tls"
"io"
"log"
"time"
gendiodes "code.cloudfoundry.org/go-diodes"
"code.cloudfoundry.org/go-loggregator/v10/rpc/loggregator_v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// EnvelopeStreamConnector provides a way to connect to loggregator and
// consume a stream of envelopes. It handles reconnecting and provides
// a stream for the lifecycle of the given context. It should be created with
// the NewEnvelopeStreamConnector constructor.
type EnvelopeStreamConnector struct {
addr string
tlsConf *tls.Config
// Buffering
bufferSize int
alerter func(int)
log Logger
dialOptions []grpc.DialOption
}
// NewEnvelopeStreamConnector creates a new EnvelopeStreamConnector. Its TLS
// configuration must share a CA with the loggregator server.
func NewEnvelopeStreamConnector(
addr string,
t *tls.Config,
opts ...EnvelopeStreamOption,
) *EnvelopeStreamConnector {
c := &EnvelopeStreamConnector{
addr: addr,
tlsConf: t,
log: log.New(io.Discard, "", 0),
}
for _, o := range opts {
o(c)
}
return c
}
// EnvelopeStreamOption configures a EnvelopeStreamConnector.
type EnvelopeStreamOption func(*EnvelopeStreamConnector)
// WithEnvelopeStreamLogger allows for the configuration of a logger.
// By default, the logger is disabled.
func WithEnvelopeStreamLogger(l Logger) EnvelopeStreamOption {
return func(c *EnvelopeStreamConnector) {
c.log = l
}
}
// WithEnvelopeStreamConnectorDialOptions allows for configuration of
// grpc dial options.
func WithEnvelopeStreamConnectorDialOptions(opts ...grpc.DialOption) EnvelopeStreamOption {
return func(c *EnvelopeStreamConnector) {
c.dialOptions = opts
}
}
// WithEnvelopeStreamBuffer enables the EnvelopeStream to read more quickly
// from the stream. It puts each envelope in a buffer that overwrites data if
// it is not being drained quick enough. If the buffer drops data, the
// 'alerter' function will be invoked with the number of envelopes dropped.
func WithEnvelopeStreamBuffer(size int, alerter func(missed int)) EnvelopeStreamOption {
return func(c *EnvelopeStreamConnector) {
c.bufferSize = size
c.alerter = alerter
}
}
// EnvelopeStream returns batches of envelopes. It blocks until its context
// is done or a batch of envelopes is available.
type EnvelopeStream func() []*loggregator_v2.Envelope
// Stream returns a new EnvelopeStream for the given context and request. The
// lifecycle of the EnvelopeStream is managed by the given context. If the
// underlying gRPC stream dies, it attempts to reconnect until the context
// is done.
func (c *EnvelopeStreamConnector) Stream(ctx context.Context, req *loggregator_v2.EgressBatchRequest) EnvelopeStream {
s := newStream(ctx, c.addr, req, c.tlsConf, c.dialOptions, c.log)
if c.alerter != nil || c.bufferSize > 0 {
d := NewOneToOneEnvelopeBatch(
c.bufferSize,
gendiodes.AlertFunc(c.alerter),
gendiodes.WithPollingContext(ctx),
)
go func() {
for {
select {
case <-ctx.Done():
return
default:
}
d.Set(s.recv())
}
}()
return d.Next
}
return s.recv
}
type stream struct {
log Logger
ctx context.Context
req *loggregator_v2.EgressBatchRequest
client loggregator_v2.EgressClient
rx loggregator_v2.Egress_BatchedReceiverClient
}
func newStream(
ctx context.Context,
addr string,
req *loggregator_v2.EgressBatchRequest,
c *tls.Config,
opts []grpc.DialOption,
log Logger,
) *stream {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(c)))
conn, err := grpc.NewClient(
addr,
opts...,
)
if err != nil {
// This error occurs on invalid configuration. And more notably,
// it does NOT occur if the server is not up.
log.Panicf("invalid gRPC dial configuration: %s", err)
}
// Protect against a go-routine leak. gRPC will keep a go-routine active
// within the connection to keep the connectin alive. We have to close
// this or the go-routine leaks. This is untested. We had trouble exposing
// the underlying connectin was still active.
go func() {
<-ctx.Done()
conn.Close()
}()
client := loggregator_v2.NewEgressClient(conn)
return &stream{
ctx: ctx,
req: req,
client: client,
log: log,
}
}
func (s *stream) recv() []*loggregator_v2.Envelope {
for {
ok := s.connect(s.ctx)
if !ok {
return nil
}
batch, err := s.rx.Recv()
if err != nil {
s.rx = nil
continue
}
return batch.Batch
}
}
func (s *stream) connect(ctx context.Context) bool {
for {
select {
case <-ctx.Done():
return false
default:
if s.rx != nil {
return true
}
var err error
s.rx, err = s.client.BatchedReceiver(
ctx,
s.req,
)
if err != nil {
s.log.Printf("Error connecting to Logs Provider: %s", err)
time.Sleep(50 * time.Millisecond)
continue
}
return true
}
}
}