This repository was archived by the owner on Jul 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidem.go
More file actions
189 lines (155 loc) · 4.01 KB
/
Copy pathidem.go
File metadata and controls
189 lines (155 loc) · 4.01 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
package idem
import (
"context"
"sync"
"time"
)
// New creates a new idempotency Middleware with the given options.
// It returns an error if the configuration is invalid
// (e.g. empty keyHeader or non-positive ttl).
func New(opts ...Option) (*Middleware, error) {
cfg := defaultConfig()
for _, opt := range opts {
opt(cfg)
}
if err := cfg.validate(); err != nil {
return nil, err
}
if cfg.storage == nil {
cfg.storage = NewMemoryStorage()
}
return &Middleware{cfg: cfg}, nil
}
type memoryEntry struct {
res *Response
expiresAt time.Time
}
// MemoryStorage is an in-memory implementation of Storage.
// It also implements Locker for per-key mutual exclusion.
//
// For production environments, consider using the redis.Storage backend
// which provides automatic TTL-based expiration without additional configuration.
type MemoryStorage struct {
mu sync.RWMutex
entries map[string]*memoryEntry
locks sync.Map
cleanupInterval time.Duration
done chan struct{}
}
// MemoryStorageOption configures a MemoryStorage.
type MemoryStorageOption func(*MemoryStorage)
// WithCleanupInterval sets the interval for background cleanup of expired entries.
// When set to a positive duration, a background goroutine periodically removes
// expired entries to prevent memory growth from unused keys.
// Call Close to stop the background goroutine.
func WithCleanupInterval(d time.Duration) MemoryStorageOption {
return func(s *MemoryStorage) {
s.cleanupInterval = d
}
}
// NewMemoryStorage creates a new in-memory Storage.
func NewMemoryStorage(opts ...MemoryStorageOption) *MemoryStorage {
s := &MemoryStorage{
entries: make(map[string]*memoryEntry),
done: make(chan struct{}),
}
for _, opt := range opts {
opt(s)
}
if s.cleanupInterval > 0 {
go s.startCleanup()
}
return s
}
// Close stops the background cleanup goroutine, if running.
func (s *MemoryStorage) Close() error {
select {
case <-s.done:
// already closed
default:
close(s.done)
}
return nil
}
func (s *MemoryStorage) startCleanup() {
ticker := time.NewTicker(s.cleanupInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.deleteExpired()
case <-s.done:
return
}
}
}
func (s *MemoryStorage) deleteExpired() {
now := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
for key, e := range s.entries {
if now.After(e.expiresAt) {
delete(s.entries, key)
s.locks.Delete(key)
}
}
}
// Get returns the cached response for the given key.
// If the key does not exist or has expired, it returns nil, nil.
func (s *MemoryStorage) Get(_ context.Context, key string) (*Response, error) {
s.mu.RLock()
e, ok := s.entries[key]
s.mu.RUnlock()
if !ok {
return nil, nil
}
if time.Now().After(e.expiresAt) {
s.mu.Lock()
if current, exists := s.entries[key]; exists && current == e {
delete(s.entries, key)
}
s.mu.Unlock()
return nil, nil
}
return e.res, nil
}
// Set stores the response for the given key with the specified TTL.
func (s *MemoryStorage) Set(_ context.Context, key string, res *Response, ttl time.Duration) error {
s.mu.Lock()
defer s.mu.Unlock()
s.entries[key] = &memoryEntry{
res: res,
expiresAt: time.Now().Add(ttl),
}
return nil
}
// Delete removes the cached response for the given key.
// If the key does not exist, it returns nil.
func (s *MemoryStorage) Delete(_ context.Context, key string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.entries, key)
return nil
}
// Lock acquires a per-key mutex lock.
// The TTL parameter is ignored for in-memory locking since the mutex
// is released explicitly via the returned unlock function.
func (s *MemoryStorage) Lock(ctx context.Context, key string, _ time.Duration) (func(), error) {
v, _ := s.locks.LoadOrStore(key, &sync.Mutex{})
mu := v.(*sync.Mutex)
locked := make(chan struct{})
go func() {
mu.Lock()
close(locked)
}()
select {
case <-locked:
return func() { mu.Unlock() }, nil
case <-ctx.Done():
go func() {
<-locked
mu.Unlock()
}()
return nil, ctx.Err()
}
}