/
inbox.go
169 lines (132 loc) · 3.62 KB
/
inbox.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
package inbox
import (
"context"
"database/sql"
"errors"
"time"
"github.com/Melenium2/go-iobox/backoff"
)
type Logger interface {
Print(...any)
Printf(string, ...any)
}
// Inbox is struct that implement inbox pattern.
//
// Writing all incoming events in a temporary table to future processing.
// Then we try to process each event with the provided handlers.
// In addition, Inbox filters new events. All events with the same event_id
// will be ignored.
//
// More about inbox pattern you can read at
// https://softwaremill.com/microservices-101.
type Inbox struct {
handlers map[string][]Handler
storage *defaultStorage
logger Logger
config config
backoff *backoff.Backoff
onDead ErrorCallback
}
func NewInbox(registry *Registry, conn *sql.DB, opts ...Option) *Inbox {
cfg := defaultConfig()
for _, opt := range opts {
cfg = opt(cfg)
}
return &Inbox{
handlers: registry.Handlers(),
storage: newStorage(conn),
logger: cfg.logger,
config: cfg,
backoff: backoff.NewBackoff(),
onDead: cfg.onDeadCallback,
}
}
// Writer creates new Client to store incoming events to the temporary table.
func (i *Inbox) Writer() Client {
return newClient(i.storage, i.handlers)
}
// Start creates new inbox table if it not created and starts worker
// which process records from the table. To stop inbox worker, you can
// call context close() function.
func (i *Inbox) Start(ctx context.Context) error {
if err := i.storage.InitInboxTable(ctx); err != nil {
return err
}
go i.run(ctx)
return nil
}
func (i *Inbox) run(ctx context.Context) {
ticker := time.NewTicker(i.config.iterationRate)
for {
select {
case <-ticker.C:
err := i.iteration() //nolint:contextcheck
if errors.Is(err, ErrNoRecords) {
continue
}
if err != nil {
i.logger.Print(err.Error())
}
case <-ctx.Done():
return
}
}
}
// iteration fetches all incoming events from a temporary table
// and trying to process it. In some cases the worker can not process
// incoming events. 1) If we received an unknown event_type. 2) If the handler with
// required key not found in the Registry. In this cases we skip
// current record and sets its status to Null. In the next iteration
// we again try to handle the event. In other cases we set Fail or
// Done status to the record depends on in the result of handler.
func (i *Inbox) iteration() error {
ctx := context.Background()
records, err := i.storage.Fetch(ctx, time.Now().UTC())
if err != nil {
return err
}
for _, record := range records {
handlers, ok := i.handlers[record.eventType]
if !ok {
record.Null()
continue
}
handler, ok := i.lookForHandler(record.handlerKey, handlers)
if !ok {
record.Null()
continue
}
if err = i.process(ctx, handler, record.payload); err != nil {
// function mutate record inside itself.
_ = i.failOrDead(record, err)
continue
}
record.Done()
}
return i.storage.Update(ctx, records)
}
func (i *Inbox) lookForHandler(handlerKey string, handlers []Handler) (Handler, bool) {
for _, handler := range handlers {
if handler.Key() == handlerKey {
return handler, true
}
}
return nil, false
}
func (i *Inbox) process(ctx context.Context, handler Handler, payload []byte) error {
ctx, cancel := context.WithTimeout(ctx, i.config.handlerTimeout)
defer cancel()
return handler.Process(ctx, payload)
}
func (i *Inbox) failOrDead(record *Record, err error) *Record {
record.Fail(err)
attempt := record.Attempt()
if attempt >= i.config.maxRetryAttempts {
record.Dead()
i.onDead(record.id, err.Error())
return record
}
dur := i.backoff.Next(attempt)
record.CalcNewDeadline(dur)
return record
}