-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.fifo.go
183 lines (140 loc) · 4.11 KB
/
subscriber.fifo.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
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
176
177
178
179
180
181
182
183
package box
import (
"context"
"runtime"
"sync"
"time"
"github.com/thefabric-io/fieldslog"
"github.com/thefabric-io/transactional"
)
type fifoSubscriber struct {
transactional transactional.Transactional
name string
box Box
handlers map[string]Handler
handlersLock sync.RWMutex
processedCount int
batchSize int
waitTime time.Duration
}
func NewFIFOSubscriber(tx transactional.Transactional, name string, box Box, batchSize int, waitTime time.Duration) Subscriber {
return &fifoSubscriber{
box: box,
name: name,
transactional: tx,
handlers: make(map[string]Handler),
batchSize: batchSize,
waitTime: waitTime,
}
}
func (es *fifoSubscriber) DefaultLogFields() map[string]any {
_, file, _, _ := runtime.Caller(0)
return map[string]any{
"metadata": map[string]any{
"file": file,
},
"subject": map[string]any{
"type": "fifoSubscriber",
"implements": "Subscriber",
"name": es.name,
"batchSize": es.batchSize,
"waitTime": es.waitTime,
"processedCount": es.processedCount,
},
"dependencies": map[string]any{
"transactional": es.transactional.DefaultLogFields(),
"box": es.box.DefaultLogFields(),
},
}
}
func (es *fifoSubscriber) Start(ctx context.Context) error {
fieldslog.Info(es, "fifoSubscriber started")
initTx, err := es.transactional.BeginTransaction(ctx, transactional.DefaultWriteTransactionOptions())
if err != nil {
fieldslog.Error(es, "beginning transaction failed", err)
return err
}
if err := es.box.Init(ctx, initTx); err != nil {
fieldslog.Error(es, "error while initializing box", err)
return err
}
if err := initTx.Commit(); err != nil {
fieldslog.Error(es, "error committing transaction when initialized box", err)
return err
}
for {
tx, err := es.transactional.BeginTransaction(ctx, transactional.DefaultWriteTransactionOptions())
if err != nil {
fieldslog.Error(es, "error beginning transaction", err)
es.wait(err.Error())
continue
}
messageRetrieved, err := es.processMessages(ctx, tx)
if err != nil {
fieldslog.Error(es, "error processing messages", err)
es.wait(err.Error())
continue
}
if err := tx.Commit(); err != nil {
fieldslog.Error(es, "error committing transaction", err)
continue
}
// If no messages are retrieved, wait for the specified duration
if messageRetrieved == 0 {
time.Sleep(es.waitTime)
}
}
}
func (es *fifoSubscriber) wait(cause string) {
fieldslog.Info(es, cause)
time.Sleep(es.waitTime)
}
func (es *fifoSubscriber) processMessages(ctx context.Context, tx transactional.Transaction) (int, error) {
messages, err := es.box.Retrieve(ctx, tx, es.name, es.messageTypesProcessing(), es.batchSize, es.waitTime)
if err != nil {
return 0, err
}
messageRetrieved := len(messages)
var lastAckedMessageOffset, lastConsumedMessageOffset int
for _, msg := range messages {
es.processedCount++
handler, exists := es.handlers[msg.Type()]
if exists {
lastConsumedMessageOffset = msg.Offset()
if err := handler.HandleEvent(ctx, tx, msg); err != nil {
return messageRetrieved, err
}
lastAckedMessageOffset = msg.Offset()
}
}
if messageRetrieved > 0 {
if err := es.box.UpdateConsumer(ctx, tx, es.name, lastAckedMessageOffset, lastConsumedMessageOffset); err != nil {
fieldslog.Error(es, "error updating consumer", err)
return messageRetrieved, nil
}
}
return messageRetrieved, nil
}
func (es *fifoSubscriber) RegisterHandler(messageType string, handler Handler) {
es.handlersLock.Lock()
defer es.handlersLock.Unlock()
es.handlers[messageType] = handler
}
func (es *fifoSubscriber) UnregisterHandler(messageType string) error {
es.handlersLock.Lock()
defer es.handlersLock.Unlock()
delete(es.handlers, messageType)
return nil
}
func (es *fifoSubscriber) Status() SubscriberStatus {
return SubscriberStatus{
ProcessedMessageCount: es.processedCount,
}
}
func (es *fifoSubscriber) messageTypesProcessing() []string {
keys := make([]string, 0, len(es.handlers))
for k := range es.handlers {
keys = append(keys, k)
}
return keys
}