-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.go
More file actions
363 lines (310 loc) · 8.75 KB
/
pool.go
File metadata and controls
363 lines (310 loc) · 8.75 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package gencache
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/gozephyr/gencache/errors"
)
// ObjectPool provides a pool of reusable objects
type ObjectPool[T any] struct {
pool sync.Pool
}
// NewObjectPool creates a new object pool
func NewObjectPool[T any](newFunc func() T) *ObjectPool[T] {
return &ObjectPool[T]{
pool: sync.Pool{
New: func() any {
return newFunc()
},
},
}
}
// Get retrieves an object from the pool
func (p *ObjectPool[T]) Get() T {
return p.pool.Get().(T)
}
// Put returns an object to the pool
func (p *ObjectPool[T]) Put(x T) {
p.pool.Put(x)
}
// EntryPool provides a pool of cache entries
type EntryPool[V any] struct {
pool *ObjectPool[Entry[V]]
}
// NewEntryPool creates a new pool of cache entries
func NewEntryPool[V any]() *EntryPool[V] {
return &EntryPool[V]{
pool: NewObjectPool(func() Entry[V] {
return Entry[V]{
AccessCount: 0,
}
}),
}
}
// Get retrieves an entry from the pool
func (p *EntryPool[V]) Get() Entry[V] {
return p.pool.Get()
}
// Put returns an entry to the pool
func (p *EntryPool[V]) Put(entry Entry[V]) {
// Reset the entry before putting it back in the pool
entry.AccessCount = 0
p.pool.Put(entry)
}
// BatchResultPool provides a pool of batch results
type BatchResultPool[K comparable, V any] struct {
pool *ObjectPool[BatchResult[K, V]]
}
// NewBatchResultPool creates a new pool of batch results
func NewBatchResultPool[K comparable, V any]() *BatchResultPool[K, V] {
return &BatchResultPool[K, V]{
pool: NewObjectPool(func() BatchResult[K, V] {
return BatchResult[K, V]{}
}),
}
}
// Get retrieves a batch result from the pool
func (p *BatchResultPool[K, V]) Get() BatchResult[K, V] {
return p.pool.Get()
}
// Put returns a batch result to the pool
func (p *BatchResultPool[K, V]) Put(result BatchResult[K, V]) {
// Reset the result before putting it back in the pool
result.Error = nil
p.pool.Put(result)
}
// PoolConfig represents configuration for the object pool
type PoolConfig struct {
MaxSize int
MinSize int
CleanupPeriod time.Duration
MaxIdleTime time.Duration
ShrinkFactor float64
}
// DefaultPoolConfig returns the default pool configuration
func DefaultPoolConfig() PoolConfig {
return PoolConfig{
MaxSize: 1000,
MinSize: 10,
CleanupPeriod: 5 * time.Minute,
MaxIdleTime: 10 * time.Minute,
ShrinkFactor: 0.5,
}
}
// PoolStats represents statistics for the object pool
type PoolStats struct {
TotalCreated atomic.Int64
TotalDestroyed atomic.Int64
CurrentSize atomic.Int64
MaxSize atomic.Int64
MinSize atomic.Int64
LastCleanup atomic.Value // time.Time
LastShrink atomic.Value // time.Time
}
// PooledCache extends Cache with object pooling
type PooledCache[K comparable, V any] interface {
Cache[K, V]
// GetPoolStats returns the current pool statistics
GetPoolStats() *PoolStats
// ResetPoolStats resets the pool statistics
ResetPoolStats()
// Cleanup performs manual cleanup of idle objects
Cleanup()
// OnEvent registers a callback for cache events, converting *V to V
OnEvent(cb CacheCallback[K, V])
// Close stops the cleanup goroutine and releases resources
Close() error
}
// pooledCache implements PooledCache
// Now, all cache operations use the pool for allocations and deallocations.
type pooledCache[K comparable, V any] struct {
Cache[K, *V] // Underlying cache stores *V
config PoolConfig
stats PoolStats
mu chan struct{} // channel-based mutex for try-lock
pool sync.Pool
stop chan struct{}
wg sync.WaitGroup // Add WaitGroup to track cleanup goroutine
}
// NewPooledCache creates a new cache with object pooling
func NewPooledCache[K comparable, V any](c Cache[K, *V], config PoolConfig) PooledCache[K, V] {
pc := &pooledCache[K, V]{
Cache: c,
config: config,
stats: PoolStats{},
mu: make(chan struct{}, 1), // channel-based mutex
stop: make(chan struct{}),
}
pc.mu <- struct{}{} // initialize the mutex as unlocked
// Initialize pool with New function
pc.pool = sync.Pool{
New: func() any {
var v V
if pc.tryLock() {
pc.stats.TotalCreated.Add(1)
pc.stats.CurrentSize.Add(1)
pc.unlock()
}
return &v
},
}
// Set initial values
pc.stats.MaxSize.Store(int64(config.MaxSize))
pc.stats.MinSize.Store(int64(config.MinSize))
// Register event callback for delete/eviction
pc.Cache.OnEvent(func(evt CacheEvent[K, *V]) {
if evt.Type == EventTypeDelete || evt.Type == EventTypeEviction {
if evt.Value != nil {
pc.pool.Put(evt.Value)
if pc.tryLock() {
pc.stats.CurrentSize.Add(1)
pc.unlock()
}
}
}
})
// Start cleanup goroutine
pc.wg.Add(1)
go pc.cleanupLoop()
return pc
}
// tryLock attempts to acquire the lock without blocking
func (pc *pooledCache[K, V]) tryLock() bool {
select {
case <-pc.mu:
return true
default:
return false
}
}
// unlock releases the lock
func (pc *pooledCache[K, V]) unlock() {
pc.mu <- struct{}{}
}
// Set stores a value in the cache, always using the pool
func (pc *pooledCache[K, V]) Set(key K, value V, ttl time.Duration) error {
ptr := pc.pool.Get().(*V)
*ptr = value // Copy value into pooled pointer
return pc.Cache.Set(key, ptr, ttl)
}
// Get retrieves a value from the cache (returns the pooled object)
func (pc *pooledCache[K, V]) Get(key K) (V, error) {
ptr, err := pc.Cache.Get(key)
if err != nil || ptr == nil {
var zero V
return zero, err
}
return *ptr, nil
}
// Delete removes a value from the cache (handled by event callback)
func (pc *pooledCache[K, V]) Delete(key K) error {
return pc.Cache.Delete(key)
}
// GetWithContext retrieves a value from the cache with context (returns the pooled object)
func (pc *pooledCache[K, V]) GetWithContext(ctx context.Context, key K) (V, error) {
ptr, err := pc.Cache.GetWithContext(ctx, key)
if err != nil || ptr == nil {
var zero V
return zero, err
}
return *ptr, nil
}
// Clear removes all values from the cache (calls Clear on the underlying cache)
func (pc *pooledCache[K, V]) Clear() error {
// If you want to return all objects to the pool, you need a way to get all keys.
// For now, just clear the underlying cache.
return pc.Cache.Clear()
}
// GetPoolStats returns the current pool statistics
func (pc *pooledCache[K, V]) GetPoolStats() *PoolStats {
return &pc.stats
}
// ResetPoolStats resets the pool statistics
func (pc *pooledCache[K, V]) ResetPoolStats() {
pc.stats = PoolStats{}
pc.stats.MaxSize.Store(int64(pc.config.MaxSize))
pc.stats.MinSize.Store(int64(pc.config.MinSize))
}
// Cleanup performs manual cleanup of idle objects
func (pc *pooledCache[K, V]) Cleanup() {
if !pc.tryLock() {
return
}
defer pc.unlock()
currentSize := pc.stats.CurrentSize.Load()
maxSize := pc.stats.MaxSize.Load()
// Cleanup if we're over max size
if currentSize > maxSize {
shrinkTarget := int64(float64(currentSize) * pc.config.ShrinkFactor)
if shrinkTarget < pc.stats.MinSize.Load() {
shrinkTarget = pc.stats.MinSize.Load()
}
// Remove excess objects
for pc.stats.CurrentSize.Load() > shrinkTarget {
pc.pool.Get() // Remove one object
pc.stats.CurrentSize.Add(-1)
pc.stats.TotalDestroyed.Add(1)
}
pc.stats.LastShrink.Store(time.Now())
}
pc.stats.LastCleanup.Store(time.Now())
}
// cleanupLoop periodically cleans up idle objects
func (pc *pooledCache[K, V]) cleanupLoop() {
defer pc.wg.Done()
ticker := time.NewTicker(pc.config.CleanupPeriod)
defer ticker.Stop()
for {
select {
case <-ticker.C:
pc.Cleanup()
case <-pc.stop:
return
default:
// Add a small delay to prevent busy waiting
time.Sleep(100 * time.Millisecond)
}
}
}
// TestGetFromPool is a test helper to directly get from the pool
func (pc *pooledCache[K, V]) TestGetFromPool() any {
return pc.pool.Get()
}
// OnEvent registers a callback for cache events, converting *V to V
func (pc *pooledCache[K, V]) OnEvent(cb CacheCallback[K, V]) {
pc.Cache.OnEvent(func(evt CacheEvent[K, *V]) {
var v V
if evt.Value != nil {
v = *evt.Value
}
cb(CacheEvent[K, V]{
Type: evt.Type,
Key: evt.Key,
Value: v,
Timestamp: evt.Timestamp,
})
})
}
// SetWithContext stores a value in the cache with context, always using the pool
func (pc *pooledCache[K, V]) SetWithContext(ctx context.Context, key K, value V, ttl time.Duration) error {
ptr := pc.pool.Get().(*V)
*ptr = value // Copy value into pooled pointer
return pc.Cache.SetWithContext(ctx, key, ptr, ttl)
}
// Close stops the cleanup goroutine and releases resources
func (pc *pooledCache[K, V]) Close() error {
close(pc.stop)
// Wait for cleanup goroutine to finish with a timeout
done := make(chan struct{})
go func() {
pc.wg.Wait()
close(done)
}()
select {
case <-done:
return nil
case <-time.After(1 * time.Second):
return errors.WrapError("Close", nil, errors.ErrStoreTimeout)
}
}