-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoutbox.go
149 lines (124 loc) · 3.34 KB
/
outbox.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
package workflow
import (
"context"
"time"
"google.golang.org/protobuf/proto"
"k8s.io/utils/clock"
"github.com/luno/workflow/internal/metrics"
"github.com/luno/workflow/internal/outboxpb"
)
func outboxConsumer[Type any, Status StatusType](w *Workflow[Type, Status], config outboxConfig) {
role := makeRole(w.Name(), "outbox", "consumer")
processName := makeRole("outbox", "consumer")
errBackOff := w.outboxConfig.errBackOff
if config.errBackOff > 0 {
errBackOff = config.errBackOff
}
pollingFrequency := w.outboxConfig.pollingFrequency
if config.pollingFrequency > 0 {
pollingFrequency = config.pollingFrequency
}
lagAlert := w.outboxConfig.lagAlert
if config.lagAlert > 0 {
lagAlert = config.lagAlert
}
w.run(role, processName, func(ctx context.Context) error {
return purgeOutbox[Type, Status](
ctx,
w.Name(),
processName,
w.recordStore,
w.eventStreamer,
w.clock,
pollingFrequency,
lagAlert,
config.limit,
)
}, errBackOff)
}
func defaultOutboxConfig() outboxConfig {
return outboxConfig{
errBackOff: defaultOutboxErrBackOff,
pollingFrequency: defaultOutboxPollingFrequency,
lagAlert: defaultOutboxLagAlert,
limit: 1000,
}
}
type outboxConfig struct {
errBackOff time.Duration
pollingFrequency time.Duration
lagAlert time.Duration
limit int64
}
func WithOutboxPollingFrequency(d time.Duration) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.pollingFrequency = d
}
}
func WithOutboxErrBackoff(d time.Duration) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.errBackOff = d
}
}
func WithOutboxLookupLimit(limit int64) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.limit = limit
}
}
func WithOutboxLagAlert(d time.Duration) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.lagAlert = d
}
}
func purgeOutbox[Type any, Status StatusType](
ctx context.Context,
workflowName string,
processName string,
recordStore RecordStore,
stream EventStreamer,
clock clock.Clock,
pollingFrequency time.Duration,
lagAlert time.Duration,
lookupLimit int64,
) error {
events, err := recordStore.ListOutboxEvents(ctx, workflowName, lookupLimit)
if err != nil {
return err
}
if len(events) == 0 {
return wait(ctx, pollingFrequency)
}
// Send the events to the EventStreamer.
for _, e := range events {
var outboxRecord outboxpb.OutboxRecord
err := proto.Unmarshal(e.Data, &outboxRecord)
if err != nil {
return err
}
headers := make(map[Header]string)
for k, v := range outboxRecord.Headers {
headers[Header(k)] = v
}
foreignID := outboxRecord.RunId
eventType := int(outboxRecord.Type)
// Push metrics and alerting around the age of the event being processed.
pushLagMetricAndAlerting(workflowName, processName, e.CreatedAt, lagAlert, clock)
t0 := clock.Now()
topic := headers[HeaderTopic]
producer, err := stream.NewSender(ctx, topic)
if err != nil {
return err
}
err = producer.Send(ctx, foreignID, eventType, headers)
if err != nil {
return err
}
err = recordStore.DeleteOutboxEvent(ctx, e.ID)
if err != nil {
return err
}
// Push the time it took to create the producer, send the event, and delete the outbox entry.
metrics.ProcessLatency.WithLabelValues(workflowName, processName).Observe(clock.Since(t0).Seconds())
}
return nil
}