-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathmanager.go
82 lines (70 loc) · 1.81 KB
/
manager.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
package exporter
import (
"context"
"time"
"github.com/google/uuid"
"github.com/thomaspoignant/go-feature-flag/utils/fflog"
)
const DefaultExporterCleanQueueInterval = 1 * time.Minute
type Manager[T ExportableEvent] interface {
AddEvent(event T)
Start()
Stop()
}
type managerImpl[T ExportableEvent] struct {
logger *fflog.FFLogger
consumers []DataExporter[T]
eventStore *EventStore[T]
}
func NewManager[T ExportableEvent](ctx context.Context, exporters []Config,
exporterCleanQueueInterval time.Duration, logger *fflog.FFLogger) Manager[T] {
if ctx == nil {
ctx = context.Background()
}
if exporterCleanQueueInterval == 0 {
// default value for the exporterCleanQueueDuration is 1 minute
exporterCleanQueueInterval = DefaultExporterCleanQueueInterval
}
evStore := NewEventStore[T](exporterCleanQueueInterval)
consumers := make([]DataExporter[T], len(exporters))
for index, exporter := range exporters {
consumerID := uuid.New().String()
exp := NewDataExporter[T](ctx, exporter, consumerID, &evStore, logger)
consumers[index] = exp
evStore.AddConsumer(consumerID)
}
return &managerImpl[T]{
logger: logger,
consumers: consumers,
eventStore: &evStore,
}
}
func (m *managerImpl[T]) AddEvent(event T) {
store := *m.eventStore
store.Add(event)
for _, consumer := range m.consumers {
if !consumer.IsBulk() {
consumer.Flush()
continue
}
count, err := store.GetPendingEventCount(consumer.GetConsumerID())
if err != nil {
m.logger.Error("error while fetching pending events", err)
continue
}
if count >= consumer.GetMaxEventInMemory() {
consumer.Flush()
continue
}
}
}
func (m *managerImpl[T]) Start() {
for _, consumer := range m.consumers {
go consumer.Start()
}
}
func (m *managerImpl[T]) Stop() {
for _, consumer := range m.consumers {
consumer.Stop()
}
}