-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathcomponent_queue.go
More file actions
69 lines (61 loc) · 2.11 KB
/
component_queue.go
File metadata and controls
69 lines (61 loc) · 2.11 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
package sensor
import (
"context"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/pkg/env"
op "github.com/stackrox/rox/pkg/metrics"
"github.com/stackrox/rox/pkg/queue"
"github.com/stackrox/rox/pkg/utils"
"github.com/stackrox/rox/sensor/common"
"github.com/stackrox/rox/sensor/common/metrics"
)
const (
// componentProcessTimeout is the maximum time allowed for a sensor component to process a message from Central
componentProcessTimeout = 30 * time.Second
)
type ComponentQueue struct {
component common.SensorComponent
q *queue.Queue[*central.MsgToSensor]
}
func NewComponentQueue(component common.SensorComponent) *ComponentQueue {
componentName := prometheus.Labels{
metrics.ComponentName: component.Name(),
}
componentQueueOperations, err := metrics.ComponentQueueOperations.CurryWith(componentName)
utils.CrashOnError(err)
c := &ComponentQueue{
component: component,
q: queue.NewQueue(
queue.WithQueueName[*central.MsgToSensor](component.Name()),
queue.WithMaxSize[*central.MsgToSensor](env.RequestsChannelBufferSize.IntegerSetting()),
queue.WithCounterVec[*central.MsgToSensor](componentQueueOperations),
queue.WithDroppedMetric[*central.MsgToSensor](componentQueueOperations.With(prometheus.Labels{
metrics.Operation: op.Dropped.String(),
})),
),
}
return c
}
func (c ComponentQueue) Push(msg *central.MsgToSensor) {
if !c.component.Accepts(msg) {
return
}
c.q.Push(msg)
}
func (c ComponentQueue) Start(ctx context.Context) {
go c.start(ctx)
}
func (c ComponentQueue) start(stopCtx context.Context) {
for msg := range c.q.Seq(stopCtx) {
start := time.Now()
processCtx, cancelFunc := context.WithTimeout(stopCtx, componentProcessTimeout)
if err := c.component.ProcessMessage(processCtx, msg); err != nil {
log.Errorf("%s.ProcessMessage(%q) errored: %v", c.component.Name(), msg.String(), err)
metrics.IncrementCentralReceiverProcessMessageErrors(c.component.Name())
}
cancelFunc()
metrics.ObserveCentralReceiverProcessMessageDuration(c.component.Name(), time.Since(start))
}
}