-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathconsumers.go
More file actions
57 lines (49 loc) · 1.31 KB
/
consumers.go
File metadata and controls
57 lines (49 loc) · 1.31 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package queue // import "go.opentelemetry.io/collector/exporter/internal/queue"
import (
"context"
"sync"
"go.opentelemetry.io/collector/component"
)
type Consumers[T any] struct {
queue Queue[T]
numConsumers int
consumeFunc func(context.Context, T) error
stopWG sync.WaitGroup
}
func NewQueueConsumers[T any](q Queue[T], numConsumers int, consumeFunc func(context.Context, T) error) *Consumers[T] {
return &Consumers[T]{
queue: q,
numConsumers: numConsumers,
consumeFunc: consumeFunc,
stopWG: sync.WaitGroup{},
}
}
// Start ensures that queue and all consumers are started.
func (qc *Consumers[T]) Start(_ context.Context, _ component.Host) error {
var startWG sync.WaitGroup
for i := 0; i < qc.numConsumers; i++ {
qc.stopWG.Add(1)
startWG.Add(1)
go func() {
startWG.Done()
defer qc.stopWG.Done()
for {
index, ctx, req, ok := qc.queue.Read(context.Background())
if !ok {
return
}
consumeErr := qc.consumeFunc(ctx, req)
qc.queue.OnProcessingFinished(index, consumeErr)
}
}()
}
startWG.Wait()
return nil
}
// Shutdown ensures that queue and all consumers are stopped.
func (qc *Consumers[T]) Shutdown(_ context.Context) error {
qc.stopWG.Wait()
return nil
}