-
Notifications
You must be signed in to change notification settings - Fork 0
/
with_idempotent.go
242 lines (226 loc) · 7.26 KB
/
with_idempotent.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package opinionatedsagas
import (
"context"
"database/sql"
"errors"
"fmt"
"math/rand"
"strings"
"time"
"github.com/lib/pq"
events "github.com/markusylisiurunen/go-opinionatedevents"
)
const setSerializableIsolationLevelQuery = `
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
`
const getIdempotencyKeyByKeyQuery = `
SELECT id, key, created_at, locked_at, is_processed
FROM :SCHEMA.idempotency_keys
WHERE key = $1
LIMIT 1
`
const createIdempotencyKeyQuery = `
INSERT INTO :SCHEMA.idempotency_keys (key, created_at, locked_at, is_processed)
VALUES ($1, $2, $3, $4)
RETURNING id
`
const lockIdempotencyKeyByKeyQuery = `
UPDATE :SCHEMA.idempotency_keys
SET locked_at = $1, is_processed = FALSE
WHERE key = $2
`
const markIdempotencyKeyAsProcessedByKeyQuery = `
UPDATE :SCHEMA.idempotency_keys
SET locked_at = NULL, is_processed = TRUE
WHERE key = $1
`
const deleteIdempotencyKeyByKeyQuery = `
DELETE FROM :SCHEMA.idempotency_keys
WHERE key = $1
`
type idempotencyKey struct {
CreatedAt time.Time
ID int64
IsProcessed bool
Key string
LockedAt *time.Time
}
var (
errIdempotencyKeyRepositoryIsLocked = errors.New("the idempotency key is already locked")
errIdempotencyKeyRepositoryIsProcessed = errors.New("the idempotency key has already been marked as processed")
errIdempotencyKeyRepositorySerialization = errors.New("could not serialize access due to read/write dependencies among transactions")
)
type idempotencyKeyRepository struct {
db *sql.DB
schema string
keepLockedFor time.Duration
}
func (r *idempotencyKeyRepository) withSchema(query string) string {
return strings.ReplaceAll(query, ":SCHEMA", r.schema)
}
func (r *idempotencyKeyRepository) begin(ctx context.Context, key string) error {
type result struct {
isLocked bool
isProcessed bool
shouldAttempt bool
}
isLocked := func() (*result, error) { return &result{true, false, false}, nil }
isProcessed := func() (*result, error) { return &result{false, true, false}, nil }
shouldAttempt := func() (*result, error) { return &result{false, false, true}, nil }
unexpectedError := func(err error) (*result, error) { return nil, err }
res, err := sqlWithTx(r.db, func(tx *sql.Tx) (*result, error) {
if _, err := tx.ExecContext(ctx, r.withSchema(setSerializableIsolationLevelQuery)); err != nil {
return unexpectedError(err)
}
// attempt to find an existing idempotency key
idempotencyKey, err := sqlFindOne(ctx, tx, r.withSchema(getIdempotencyKeyByKeyQuery),
func(item *idempotencyKey) []any {
return []any{&item.ID, &item.Key, &item.CreatedAt, &item.LockedAt, &item.IsProcessed}
},
key,
)
if err != nil {
// this is the first attempt, create a new (locked) idempotency key
if err == sql.ErrNoRows {
now := time.Now()
_, err := sqlCreate(ctx, tx, r.withSchema(createIdempotencyKeyQuery),
key, now.UTC(), now.UTC(), false,
)
if err != nil {
return unexpectedError(err)
}
// the idempotency key was created, let the client attempt processing it
return shouldAttempt()
}
return unexpectedError(err)
}
if idempotencyKey.IsProcessed {
// the idempotency key was found, and it is marked as processed
return isProcessed()
}
// TODO: should `now` be passed in a parameter?
locksValidAfter := time.Now().Add(-1 * r.keepLockedFor)
if idempotencyKey.LockedAt != nil && idempotencyKey.LockedAt.After(locksValidAfter) {
// the idempotency key was found and it is currently locked
return isLocked()
}
// idempotency key was found, but it is free to be acquired for processing
if _, err = tx.ExecContext(ctx, r.withSchema(lockIdempotencyKeyByKeyQuery),
time.Now().UTC(), key,
); err != nil {
return unexpectedError(err)
}
return shouldAttempt()
})
if err != nil {
if err, ok := err.(*pq.Error); ok && err.Code == "40001" {
return errIdempotencyKeyRepositorySerialization
}
return err
}
if res.shouldAttempt {
return nil
}
if res.isLocked {
return errIdempotencyKeyRepositoryIsLocked
}
if res.isProcessed {
return errIdempotencyKeyRepositoryIsProcessed
}
return errors.New("an unexpected begin result state")
}
func (r *idempotencyKeyRepository) commit(ctx context.Context, key string) error {
_, err := sqlWithTx(r.db, func(tx *sql.Tx) (any, error) {
query := r.withSchema(markIdempotencyKeyAsProcessedByKeyQuery)
result, err := tx.ExecContext(ctx, query, key)
if err != nil {
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return nil, err
}
if rowsAffected != 1 {
return nil, fmt.Errorf("expected affected rows to be exactly 1, got %d", rowsAffected)
}
return nil, nil
})
return err
}
func (r *idempotencyKeyRepository) rollback(ctx context.Context, key string) error {
_, err := sqlWithTx(r.db, func(tx *sql.Tx) (any, error) {
query := r.withSchema(deleteIdempotencyKeyByKeyQuery)
result, err := tx.ExecContext(ctx, query, key)
if err != nil {
return nil, err
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return nil, err
}
if rowsAffected != 1 {
return nil, fmt.Errorf("expected affected rows to be exactly 1, got %d", rowsAffected)
}
return nil, nil
})
return err
}
func withIdempotent(db *sql.DB, schema string) events.OnMessageMiddleware {
var (
errBeingProcessed = errors.New("an identical task is currently being processed")
errLockNotAcquired = errors.New("the idempotency lock could not be acquired")
)
repo := &idempotencyKeyRepository{
db: db,
schema: schema,
keepLockedFor: 10 * time.Second,
}
return func(next events.OnMessageHandler) events.OnMessageHandler {
return func(ctx context.Context, delivery events.Delivery) error {
// use the task's UUID as the idempotency key
key := delivery.GetMessage().GetUUID()
// attempt to acquire the idempotency key (retry `n` times if needed)
var err error
for i := 0; i < 3; i += 1 {
err = repo.begin(ctx, key)
if err == nil {
break
}
if err == errIdempotencyKeyRepositorySerialization {
// a conflict occurred with another worker -> should retry after a while (this should not happen in reality)
time.Sleep(time.Duration(rand.Intn(41)+41) * time.Millisecond)
continue
}
if err == errIdempotencyKeyRepositoryIsLocked {
// the task is currently locked by another worker -> should retry after a while (this should not happen in reality)
return errBeingProcessed
}
if err == errIdempotencyKeyRepositoryIsProcessed {
// the task has already been processed -> can be marked as successful
return nil
}
return errLockNotAcquired
}
if err != nil {
if err == errIdempotencyKeyRepositorySerialization {
return errBeingProcessed
}
return errLockNotAcquired
}
// the idempotency lock was acquired, execute the handler
result := next(ctx, delivery)
if result != nil {
// the handler returned an error, release the idempotency key lock
if err := repo.rollback(ctx, key); err != nil { //nolint
// NOTE: nothing can really be done here, the lock will be released after a few seconds
}
} else {
// the handler was successful, mark the idempotency key as processed
if err := repo.commit(ctx, key); err != nil { //nolint
// NOTE: nothing can really be done here... :/
}
}
return result
}
}
}