-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi_idempotency.go
More file actions
343 lines (305 loc) · 8.32 KB
/
Copy pathapi_idempotency.go
File metadata and controls
343 lines (305 loc) · 8.32 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"log"
"os"
"path/filepath"
"sync"
"time"
)
type idempotencyResult struct {
status int
body []byte
}
type idempotencyState string
const (
idempotencyStateAccepted idempotencyState = "accepted"
idempotencyStateInProgress idempotencyState = "in_progress"
idempotencyStateCompleted idempotencyState = "completed"
idempotencyStateFailed idempotencyState = "failed"
)
type idempotencyCache struct {
mu sync.Mutex
ttl time.Duration
maxEntries int
entries map[string]*idempotencyEntry
persistPath string
}
type idempotencyEntry struct {
createdAt time.Time
updatedAt time.Time
reqHash [32]byte
state idempotencyState
result idempotencyResult
}
type persistedIdempotencyEntry struct {
CreatedAtUnixNano int64 `json:"created_at_unix_nano"`
UpdatedAtUnixNano int64 `json:"updated_at_unix_nano"`
ReqHashHex string `json:"req_hash_hex"`
State string `json:"state"`
Status int `json:"status"`
BodyBase64 string `json:"body_base64"`
}
type persistedIdempotencyState struct {
Entries map[string]persistedIdempotencyEntry `json:"entries"`
}
func newIdempotencyCache(ttl time.Duration, maxEntries int, persistPath string) *idempotencyCache {
c := &idempotencyCache{
ttl: ttl,
maxEntries: maxEntries,
entries: make(map[string]*idempotencyEntry),
persistPath: persistPath,
}
c.loadPersisted()
return c
}
func (c *idempotencyCache) getOrStart(now time.Time, key string, reqHash [32]byte) (state string, res idempotencyResult) {
// state:
// - "replay": res is valid
// - "start": caller should process request and then complete()
// - "inflight": same key currently running
// - "mismatch": key exists but request differs
c.mu.Lock()
c.pruneLocked(now)
if e, ok := c.entries[key]; ok {
if e.reqHash != reqHash {
c.mu.Unlock()
return "mismatch", idempotencyResult{}
}
if !e.isTerminal() {
c.mu.Unlock()
return "inflight", idempotencyResult{}
}
c.mu.Unlock()
return "replay", e.result
}
c.entries[key] = &idempotencyEntry{
createdAt: now,
updatedAt: now,
reqHash: reqHash,
state: idempotencyStateInProgress,
}
c.enforceCapLocked()
data, path := c.snapshotForPersist()
c.mu.Unlock()
if err := writePersistData(data, path); err != nil {
log.Printf("Warning: failed to persist idempotency cache: %v", err)
}
return "start", idempotencyResult{}
}
func (c *idempotencyCache) complete(now time.Time, key string, reqHash [32]byte, status int, body []byte) {
c.mu.Lock()
c.pruneLocked(now)
e, ok := c.entries[key]
if !ok {
c.mu.Unlock()
return
}
if e.reqHash != reqHash {
delete(c.entries, key)
c.mu.Unlock()
return
}
e.updatedAt = now
if status >= 200 && status < 300 {
e.state = idempotencyStateCompleted
} else {
e.state = idempotencyStateFailed
}
e.result = idempotencyResult{status: status, body: append([]byte(nil), body...)}
c.enforceCapLocked()
data, path := c.snapshotForPersist()
c.mu.Unlock()
if err := writePersistData(data, path); err != nil {
log.Printf("Warning: failed to persist idempotency cache: %v", err)
}
}
func (c *idempotencyCache) abandon(key string) {
c.mu.Lock()
if _, ok := c.entries[key]; !ok {
c.mu.Unlock()
return
}
delete(c.entries, key)
data, path := c.snapshotForPersist()
c.mu.Unlock()
if err := writePersistData(data, path); err != nil {
log.Printf("Warning: failed to persist idempotency cache: %v", err)
}
}
func (c *idempotencyCache) pruneLocked(now time.Time) {
if c.ttl <= 0 {
return
}
for k, e := range c.entries {
// Never expire unresolved entries; dropping them re-allows duplicate processing.
if !e.isTerminal() {
continue
}
if now.Sub(e.updatedAt) > c.ttl {
delete(c.entries, k)
}
}
}
func (c *idempotencyCache) enforceCapLocked() {
if c.maxEntries <= 0 || len(c.entries) <= c.maxEntries {
return
}
// Evict oldest completed entries first.
// Never evict in-flight entries; callers use those to prevent duplicate processing.
for len(c.entries) > c.maxEntries {
var oldestKey string
var oldestTime time.Time
first := true
for k, e := range c.entries {
if !e.isTerminal() {
continue
}
if first || e.updatedAt.Before(oldestTime) {
oldestKey = k
oldestTime = e.updatedAt
first = false
}
}
if oldestKey == "" {
// All remaining entries are unresolved; keep them even if we're over cap.
return
}
delete(c.entries, oldestKey)
}
}
func hashRequestBody(body []byte) [32]byte {
return sha256.Sum256(body)
}
type idempotencyLookup struct {
CreatedAt time.Time
UpdatedAt time.Time
State idempotencyState
Result idempotencyResult
}
func (c *idempotencyCache) lookup(now time.Time, key string) (idempotencyLookup, bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.pruneLocked(now)
entry, ok := c.entries[key]
if !ok {
return idempotencyLookup{}, false
}
return idempotencyLookup{
CreatedAt: entry.createdAt,
UpdatedAt: entry.updatedAt,
State: entry.state,
Result: idempotencyResult{
status: entry.result.status,
body: append([]byte(nil), entry.result.body...),
},
}, true
}
func (c *idempotencyCache) loadPersisted() {
if c.persistPath == "" {
return
}
data, err := os.ReadFile(c.persistPath)
if err != nil {
if os.IsNotExist(err) {
return
}
log.Printf("Warning: failed to read idempotency cache %s: %v", c.persistPath, err)
return
}
var persisted persistedIdempotencyState
if err := json.Unmarshal(data, &persisted); err != nil {
log.Printf("Warning: failed to parse idempotency cache %s: %v", c.persistPath, err)
return
}
now := time.Now()
for key, entry := range persisted.Entries {
reqHashBytes, err := hex.DecodeString(entry.ReqHashHex)
if err != nil || len(reqHashBytes) != sha256.Size {
continue
}
body := []byte(nil)
if entry.BodyBase64 != "" {
body, err = base64.StdEncoding.DecodeString(entry.BodyBase64)
if err != nil {
continue
}
}
var reqHash [32]byte
copy(reqHash[:], reqHashBytes)
state := parsePersistedIdempotencyState(entry.State)
if state == idempotencyStateInProgress {
// A restarted daemon cannot prove whether work was still executing, but it can
// prove the request was durably accepted and must not silently disappear.
state = idempotencyStateAccepted
}
updatedAt := time.Unix(0, entry.UpdatedAtUnixNano)
if entry.UpdatedAtUnixNano == 0 {
updatedAt = time.Unix(0, entry.CreatedAtUnixNano)
}
c.entries[key] = &idempotencyEntry{
createdAt: time.Unix(0, entry.CreatedAtUnixNano),
updatedAt: updatedAt,
reqHash: reqHash,
state: state,
result: idempotencyResult{
status: entry.Status,
body: body,
},
}
}
c.pruneLocked(now)
c.enforceCapLocked()
}
// snapshotForPersist marshals current entries under the lock and returns
// the serialized bytes plus the persist path. Caller must hold c.mu.
func (c *idempotencyCache) snapshotForPersist() ([]byte, string) {
if c.persistPath == "" {
return nil, ""
}
persisted := persistedIdempotencyState{
Entries: make(map[string]persistedIdempotencyEntry),
}
for key, entry := range c.entries {
if entry == nil {
continue
}
persisted.Entries[key] = persistedIdempotencyEntry{
CreatedAtUnixNano: entry.createdAt.UnixNano(),
UpdatedAtUnixNano: entry.updatedAt.UnixNano(),
ReqHashHex: hex.EncodeToString(entry.reqHash[:]),
State: string(entry.state),
Status: entry.result.status,
BodyBase64: base64.StdEncoding.EncodeToString(entry.result.body),
}
}
data, _ := json.Marshal(persisted)
return data, c.persistPath
}
func writePersistData(data []byte, path string) error {
if path == "" || data == nil {
return nil
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return err
}
tmpPath := path + ".tmp"
if err := os.WriteFile(tmpPath, data, 0o600); err != nil {
return err
}
return os.Rename(tmpPath, path)
}
func (e *idempotencyEntry) isTerminal() bool {
return e != nil && (e.state == idempotencyStateCompleted || e.state == idempotencyStateFailed)
}
func parsePersistedIdempotencyState(value string) idempotencyState {
switch idempotencyState(value) {
case idempotencyStateAccepted, idempotencyStateInProgress, idempotencyStateCompleted, idempotencyStateFailed:
return idempotencyState(value)
default:
return idempotencyStateCompleted
}
}