-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathbuffered_stream.go
More file actions
95 lines (87 loc) · 2.44 KB
/
buffered_stream.go
File metadata and controls
95 lines (87 loc) · 2.44 KB
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
package sensor
import (
"time"
"github.com/pkg/errors"
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/sensor/common/messagestream"
"github.com/stackrox/rox/sensor/common/metrics"
)
const (
loggingRateLimiter = "buffered-stream"
)
var (
stopTimeout = 10 * time.Second
)
type bufferedStream struct {
buffer chan *central.MsgFromSensor
stopC concurrency.ReadOnlyErrorSignal
stream messagestream.SensorMessageStream
}
func (s bufferedStream) Send(msg *central.MsgFromSensor) error {
select {
case s.buffer <- msg:
metrics.ResponsesChannelAdd(msg)
default:
// The buffer is full, we drop the message and return
logging.GetRateLimitedLogger().WarnL(loggingRateLimiter, "Dropping message in the gRPC stream")
metrics.ResponsesChannelDrop(msg)
return nil
}
return nil
}
// NewBufferedStream returns a SensorMessageStream 'buffStream' that implements a buffer for MsgFromSensor messages.
// If the buffer limit is reached, new messages will be dropped.
// buffStream is the bufferedStream.
// errC is a channel containing errors coming from the internal Send function.
// onStop is a function that waits for errC to be closed. This is needed to
// avoid races between the Send and the CloseSend functions.
func NewBufferedStream(stream messagestream.SensorMessageStream, msgC chan *central.MsgFromSensor, stopC concurrency.ReadOnlyErrorSignal) (buffStream messagestream.SensorMessageStream, errC <-chan error, onStop func() error) {
// if the capacity of the buffer is zero then we just return the inner stream
if cap(msgC) == 0 {
return stream, nil, nil
}
ret := bufferedStream{
buffer: msgC,
stopC: stopC,
stream: stream,
}
errC = ret.run()
return ret, errC, func() error {
for {
select {
case _, ok := <-errC:
if !ok {
return nil
}
case <-time.After(stopTimeout):
// If we reach this timeout we could have a deadlock in the gRPC stream
return errors.New("timeout waiting for the buffered stream to stop")
}
}
}
}
func (s bufferedStream) run() <-chan error {
errC := make(chan error)
go func() {
defer close(errC)
for {
select {
case <-s.stopC.Done():
return
case msg, ok := <-s.buffer:
if !ok {
return
}
metrics.ResponsesChannelRemove(msg)
select {
case errC <- s.stream.Send(msg):
case <-s.stopC.Done():
return
}
}
}
}()
return errC
}