-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcachePersistence.go
More file actions
273 lines (235 loc) · 6.45 KB
/
Copy pathcachePersistence.go
File metadata and controls
273 lines (235 loc) · 6.45 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
package geoblock
import (
"bytes"
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
lru "github.com/PascalMinder/geoblock/lrucache"
)
const (
DefaultPersistInterval = 10 * time.Second
PersistIntervalMultiplicate = 3
)
// Options describes how to initialize the in-memory cache and optional persistence.
type Options struct {
CacheSize int
CachePath string // file path for persisted cache; if empty or invalid > feature OFF
PersistInterval time.Duration // base interval; used for debounce + max interval
Logger *log.Logger
SilentStartUp bool
Name string
}
// CachePersist manages debounced, low-CPU persistence of the LRU cache.
type CachePersist struct {
path string
cache *lru.LRUCache
log *log.Logger
name string
ch chan struct{} // edge-trigger signal
quit chan struct{} // stop signal
minInterval time.Duration // debounce interval
maxInterval time.Duration // hard max between flushes
cacheDirty uint32 // 0 clean, 1 dirty
lastFlush atomic.Int64 // unix nano of last successful flush
}
// NewCachePersist constructs a new persistence controller.
// It does NOT start the worker; caller must call go p.Run(ctx).
func NewCachePersist(
path string, cache *lru.LRUCache, logger *log.Logger, name string, persistInterval time.Duration) *CachePersist {
if persistInterval <= 0 {
persistInterval = DefaultPersistInterval
}
p := &CachePersist{
path: path,
cache: cache,
log: logger,
name: name,
ch: make(chan struct{}, 1),
quit: make(chan struct{}),
minInterval: persistInterval,
maxInterval: PersistIntervalMultiplicate * persistInterval,
}
p.lastFlush.Store(time.Now().UnixNano())
return p
}
// MarkDirty marks the cache as needing a flush and nudges the worker.
func (p *CachePersist) MarkDirty() {
if p == nil {
return
}
atomic.StoreUint32(&p.cacheDirty, 1)
select {
case p.ch <- struct{}{}:
default:
// already scheduled; no need to push again
}
}
func (p *CachePersist) Run(ctx context.Context) {
if p == nil {
return
}
wait := func(d time.Duration) bool {
if d <= 0 {
return false
}
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
return true
case <-p.quit:
return true
case <-t.C:
return false
}
}
for {
select {
case <-ctx.Done():
p.flushIfDirty()
return
case <-p.quit:
p.flushIfDirty()
return
case <-p.ch:
// Drain additional nudges to coalesce bursts
drained := true
for drained {
select {
case <-p.ch:
default:
drained = false
}
}
now := time.Now()
last := time.Unix(0, p.lastFlush.Load())
since := now.Sub(last)
untilMin := p.minInterval - since
untilMax := p.maxInterval - since
// If we already exceeded max interval, flush immediately.
if untilMax <= 0 {
p.flushIfDirty()
continue
}
// Otherwise wait the rest of the debounce window.
if wait(untilMin) {
p.flushIfDirty()
return
}
p.flushIfDirty()
}
}
}
// Stop asks the worker to stop and does a final flush.
func (p *CachePersist) Stop() {
if p == nil {
return
}
close(p.quit)
}
func (p *CachePersist) flushIfDirty() {
if p == nil || atomic.LoadUint32(&p.cacheDirty) == 0 {
return
}
var buf bytes.Buffer
if err := p.cache.Export(&buf); err != nil {
p.log.Printf("%s: cache snapshot encode error: %v", p.name, err)
return
}
dir := filepath.Dir(p.path)
tmp, err := os.CreateTemp(dir, "ipdb-*.tmp")
if err != nil {
p.log.Printf("%s: snapshot temp file error: %v", p.name, err)
return
}
tmpPath := tmp.Name()
if _, err := tmp.Write(buf.Bytes()); err != nil {
tmp.Close()
_ = os.Remove(tmpPath)
p.log.Printf("%s: snapshot write error: %v", p.name, err)
return
}
if err := tmp.Sync(); err != nil {
tmp.Close()
_ = os.Remove(tmpPath)
p.log.Printf("%s: snapshot fsync error: %v", p.name, err)
return
}
if err := tmp.Close(); err != nil {
_ = os.Remove(tmpPath)
p.log.Printf("%s: snapshot close error: %v", p.name, err)
return
}
if err := os.Rename(tmpPath, p.path); err != nil {
_ = os.Remove(tmpPath)
p.log.Printf("%s: snapshot rename error: %v", p.name, err)
return
}
atomic.StoreUint32(&p.cacheDirty, 0)
p.lastFlush.Store(time.Now().UnixNano())
}
type sharedCacheEntry struct {
cache *lru.LRUCache
persist *CachePersist
}
var (
sharedCachesMu sync.Mutex
sharedCaches = map[string]*sharedCacheEntry{}
)
// GetOrInitCache shares one cache and persistence worker per middleware name,
// created once on first use. Traefik builds the middleware several times (per
// router and per config reload); sharing means an IP is looked up once, and a
// single worker owns the persisted file instead of several racing to write it.
// The worker uses a detached context to outlive any single instance (e.g. a
// reload), which would otherwise leave the shared cache without a worker.
func GetOrInitCache(opt Options) (*lru.LRUCache, *CachePersist, error) {
sharedCachesMu.Lock()
defer sharedCachesMu.Unlock()
if sc, ok := sharedCaches[opt.Name]; ok {
return sc.cache, sc.persist, nil
}
cache, persist, err := InitializeCache(context.Background(), opt)
if err != nil {
return nil, nil, err
}
sharedCaches[opt.Name] = &sharedCacheEntry{cache: cache, persist: persist}
return cache, persist, nil
}
func InitializeCache(ctx context.Context, opt Options) (*lru.LRUCache, *CachePersist, error) {
if opt.CacheSize <= 1 {
return nil, nil, fmt.Errorf("cache size must be bigger than 1")
}
cache, err := lru.NewLRUCache(opt.CacheSize)
if err != nil {
return nil, nil, fmt.Errorf("create lru cache: %w", err)
}
logger := opt.Logger
if logger == nil {
logger = log.New(os.Stdout, "", log.LstdFlags)
}
var persist *CachePersist
if opt.CachePath != "" {
path, err := ValidatePersistencePath(opt.CachePath)
if err != nil {
logger.Printf("%s: IP cache persistence disabled (path invalid): %v", opt.Name, err)
} else {
// Try warm load; ignore file-not-exist
if err := cache.ImportFromFile(path); err != nil && !os.IsNotExist(err) {
logger.Printf("%s: failed to warm-load IP cache from %s: %v", opt.Name, path, err)
}
persist = NewCachePersist(path, cache, logger, opt.Name, opt.PersistInterval)
go persist.Run(ctx)
if !opt.SilentStartUp {
logger.Printf("%s: IP cache persistence enabled -> %s", opt.Name, path)
}
}
} else {
logger.Printf("%s: IP cache persistence disabled (no path configured)", opt.Name)
}
return cache, persist, nil
}