-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathoutbox.go
More file actions
175 lines (146 loc) · 3.76 KB
/
outbox.go
File metadata and controls
175 lines (146 loc) · 3.76 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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(
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
disabled bool
}
func WithOutboxOptions(opts ...OutboxOption) BuildOption {
return func(bo *buildOptions) {
options := defaultOutboxConfig()
for _, set := range opts {
set(&options)
}
bo.outboxConfig = options
}
}
type OutboxOption func(w *outboxConfig)
func OutboxPollingFrequency(d time.Duration) OutboxOption {
return func(bo *outboxConfig) {
bo.pollingFrequency = d
}
}
func OutboxErrBackOff(d time.Duration) OutboxOption {
return func(bo *outboxConfig) {
bo.errBackOff = d
}
}
func OutboxLookupLimit(limit int64) OutboxOption {
return func(bo *outboxConfig) {
bo.limit = limit
}
}
func OutboxLagAlert(d time.Duration) OutboxOption {
return func(bo *outboxConfig) {
bo.lagAlert = d
}
}
func purgeOutbox(
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)
}
// Cache senders by topic to avoid creating a new connection per event.
senders := make(map[string]EventSender)
defer func() {
for _, s := range senders {
_ = s.Close()
}
}()
// 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, ok := senders[topic]
if !ok {
producer, err = stream.NewSender(ctx, topic)
if err != nil {
return err
}
senders[topic] = producer
}
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
}