-
Notifications
You must be signed in to change notification settings - Fork 10
/
timedmap.go
380 lines (326 loc) · 9.76 KB
/
timedmap.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package timedmap
import (
"sync"
"sync/atomic"
"time"
)
// Callback is a function which can be called when a key-value-pair has expired.
type Callback[TVal any] func(value TVal)
// TimedMap is a key-value map with lifetimes attached to values.
// Expired values are removed on access or via a cleanup coroutine,
// which can be enabled via the StartCleanerInternal method.
type TimedMap[TKey comparable, TVal any] struct {
mtx sync.RWMutex
container map[TKey]*Element[TVal]
elementPool *sync.Pool
cleanupTickTime time.Duration
cleanerTicker *time.Ticker
cleanerStopChan chan bool
cleanerRunning atomic.Bool
}
// Element contains the actual value as interface type,
// the time when the value expires and an array of
// callbacks, which will be executed when the Element
// expires.
type Element[TVal any] struct {
value TVal
expires time.Time
cbs []Callback[TVal]
}
// New creates and returns a new instance of TimedMap.
// The passed cleanupTickTime will be passed to the
// cleanup ticker, which iterates through the map and
// deletes expired key-value pairs on each iteration.
//
// Optionally, you can also pass a custom <-chan time.Time,
// which controls the cleanup cycle if you want to use
// a single synchronized timer or if you want to have more
// granular control over the cleanup loop.
//
// When passing 0 as cleanupTickTime and no tickerChan,
// the cleanup loop will not be started. You can call
// StartCleanerInternal or StartCleanerExternal to
// manually start the cleanup loop. These both methods
// can also be used to re-define the specification of
// the cleanup loop when already running if you want to.
func New[TKey comparable, TVal any](cleanupTickTime time.Duration, tickerChan ...<-chan time.Time) *TimedMap[TKey, TVal] {
return newTimedMap[TKey, TVal](make(map[TKey]*Element[TVal]), cleanupTickTime, tickerChan)
}
// FromMap creates a new TimedMap containing all entries from
// the passed map m. Each entry will get assigned the passed
// expiration duration.
func FromMap[TKey comparable, TVal any](
m map[TKey]TVal,
expiration time.Duration,
cleanupTickTime time.Duration,
tickerChan ...<-chan time.Time,
) (*TimedMap[TKey, TVal], error) {
if m == nil {
return nil, ErrValueNoMap
}
exp := time.Now().Add(expiration)
container := make(map[TKey]*Element[TVal])
for k, v := range m {
el := &Element[TVal]{
value: v,
expires: exp,
}
container[k] = el
}
return newTimedMap(container, cleanupTickTime, tickerChan), nil
}
// Set appends a key-value pair to the map or sets the value of
// a key. expiresAfter sets the expiry time after the key-value pair
// will automatically be removed from the map.
func (tm *TimedMap[TKey, TVal]) Set(key TKey, value TVal, expiresAfter time.Duration, cb ...Callback[TVal]) {
tm.set(key, value, expiresAfter, cb...)
}
// GetValue returns an interface of the value of a key in the
// map. The returned value is nil if there is no value to the
// passed key or if the value was expired.
func (tm *TimedMap[TKey, TVal]) GetValue(key TKey) (val TVal, ok bool) {
v := tm.get(key)
if v == nil {
return val, false
}
tm.mtx.RLock()
defer tm.mtx.RUnlock()
return v.value, true
}
// GetExpires returns the expiry time of a key-value pair.
// If the key-value pair does not exist in the map or
// was expired, this will return an error object.
func (tm *TimedMap[TKey, TVal]) GetExpires(key TKey) (time.Time, error) {
v := tm.get(key)
if v == nil {
return time.Time{}, ErrKeyNotFound
}
return v.expires, nil
}
// SetExpires sets the expiry time for a key-value
// pair to the passed duration. If there is no value
// to the key passed , this will return an error.
func (tm *TimedMap[TKey, TVal]) SetExpires(key TKey, d time.Duration) error {
return tm.setExpires(key, d)
}
// Contains returns true, if the key exists in the map.
// false will be returned, if there is no value to the
// key or if the key-value pair was expired.
func (tm *TimedMap[TKey, TVal]) Contains(key TKey) bool {
return tm.get(key) != nil
}
// Remove deletes a key-value pair in the map.
func (tm *TimedMap[TKey, TVal]) Remove(key TKey) {
tm.remove(key)
}
// Refresh extends the expiry time for a key-value pair
// about the passed duration. If there is no value to
// the key passed, this will return an error object.
func (tm *TimedMap[TKey, TVal]) Refresh(key TKey, d time.Duration) error {
return tm.refresh(key, d)
}
// Flush deletes all key-value pairs of the map.
func (tm *TimedMap[TKey, TVal]) Flush() {
tm.mtx.Lock()
defer tm.mtx.Unlock()
for k, v := range tm.container {
tm.elementPool.Put(v)
delete(tm.container, k)
}
}
// Size returns the current number of key-value pairs
// existent in the map.
func (tm *TimedMap[TKey, TVal]) Size() int {
return len(tm.container)
}
// StartCleanerInternal starts the cleanup loop controlled
// by an internal ticker with the given interval.
//
// If the cleanup loop is already running, it will be
// stopped and restarted using the new specification.
func (tm *TimedMap[TKey, TVal]) StartCleanerInternal(interval time.Duration) {
if tm.cleanerRunning.Load() {
tm.StopCleaner()
}
tm.cleanerTicker = time.NewTicker(interval)
go tm.cleanupLoop(tm.cleanerTicker.C)
}
// StartCleanerExternal starts the cleanup loop controlled
// by the given initiator channel. This is useful if you
// want to have more control over the cleanup loop or if
// you want to sync up multiple TimedMaps.
//
// If the cleanup loop is already running, it will be
// stopped and restarted using the new specification.
func (tm *TimedMap[TKey, TVal]) StartCleanerExternal(initiator <-chan time.Time) {
if tm.cleanerRunning.Load() {
tm.StopCleaner()
}
go tm.cleanupLoop(initiator)
}
// StopCleaner stops the cleaner go routine and timer.
// This should always be called after exiting a scope
// where TimedMap is used that the data can be cleaned
// up correctly.
func (tm *TimedMap[TKey, TVal]) StopCleaner() {
if !tm.cleanerRunning.Load() {
return
}
tm.cleanerStopChan <- true
if tm.cleanerTicker != nil {
tm.cleanerTicker.Stop()
}
}
// Snapshot returns a new map which represents the
// current key-value state of the internal container.
func (tm *TimedMap[TKey, TVal]) Snapshot() map[TKey]TVal {
return tm.getSnapshot()
}
// cleanupLoop holds the loop executing the cleanup
// when initiated by tc.
func (tm *TimedMap[TKey, TVal]) cleanupLoop(tc <-chan time.Time) {
tm.cleanerRunning.Store(true)
defer func() {
tm.cleanerRunning.Store(false)
}()
for {
select {
case <-tc:
tm.cleanUp()
case <-tm.cleanerStopChan:
return
}
}
}
// expireElement removes the specified key-value Element
// from the map and executes all defined Callback functions
func (tm *TimedMap[TKey, TVal]) expireElement(key TKey, v *Element[TVal]) {
for _, cb := range v.cbs {
cb(v.value)
}
tm.elementPool.Put(v)
delete(tm.container, key)
}
// cleanUp iterates through the map and expires all key-value
// pairs which expire time after the current time
func (tm *TimedMap[TKey, TVal]) cleanUp() {
now := time.Now()
tm.mtx.Lock()
defer tm.mtx.Unlock()
for k, v := range tm.container {
if now.After(v.expires) {
tm.expireElement(k, v)
}
}
}
// set sets the value for a key and section with the
// given expiration parameters
func (tm *TimedMap[TKey, TVal]) set(key TKey, val TVal, expiresAfter time.Duration, cb ...Callback[TVal]) {
// re-use Element when existent on this key
if v := tm.getRaw(key); v != nil {
tm.mtx.Lock()
defer tm.mtx.Unlock()
v.value = val
v.expires = time.Now().Add(expiresAfter)
v.cbs = cb
return
}
tm.mtx.Lock()
defer tm.mtx.Unlock()
v := tm.elementPool.Get().(*Element[TVal])
v.value = val
v.expires = time.Now().Add(expiresAfter)
v.cbs = cb
tm.container[key] = v
}
// get returns an Element object by key and section
// if the value has not already expired
func (tm *TimedMap[TKey, TVal]) get(key TKey) *Element[TVal] {
v := tm.getRaw(key)
if v == nil {
return nil
}
tm.mtx.Lock()
defer tm.mtx.Unlock()
if time.Now().After(v.expires) {
tm.expireElement(key, v)
return nil
}
return v
}
// getRaw returns the raw Element object by key,
// not depending on expiration time
func (tm *TimedMap[TKey, TVal]) getRaw(key TKey) *Element[TVal] {
tm.mtx.RLock()
v, ok := tm.container[key]
tm.mtx.RUnlock()
if !ok {
return nil
}
return v
}
// remove removes an Element from the map by give back the key
func (tm *TimedMap[TKey, TVal]) remove(key TKey) {
tm.mtx.Lock()
defer tm.mtx.Unlock()
v, ok := tm.container[key]
if !ok {
return
}
tm.elementPool.Put(v)
delete(tm.container, key)
}
// refresh extends the lifetime of the given key in the
// given section by the duration d.
func (tm *TimedMap[TKey, TVal]) refresh(key TKey, d time.Duration) error {
v := tm.get(key)
if v == nil {
return ErrKeyNotFound
}
tm.mtx.Lock()
v.expires = v.expires.Add(d)
tm.mtx.Unlock()
return nil
}
// setExpires sets the lifetime of the given key in the
// given section to the duration d.
func (tm *TimedMap[TKey, TVal]) setExpires(key TKey, d time.Duration) error {
v := tm.get(key)
if v == nil {
return ErrKeyNotFound
}
tm.mtx.Lock()
v.expires = time.Now().Add(d)
tm.mtx.Unlock()
return nil
}
func (tm *TimedMap[TKey, TVal]) getSnapshot() (m map[TKey]TVal) {
m = make(map[TKey]TVal)
tm.mtx.RLock()
defer tm.mtx.RUnlock()
for k, v := range tm.container {
m[k] = v.value
}
return
}
func newTimedMap[TKey comparable, TVal any](
container map[TKey]*Element[TVal],
cleanupTickTime time.Duration,
tickerChan []<-chan time.Time,
) *TimedMap[TKey, TVal] {
tm := &TimedMap[TKey, TVal]{
container: container,
cleanerStopChan: make(chan bool),
elementPool: &sync.Pool{
New: func() any {
return new(Element[TVal])
},
},
}
if len(tickerChan) > 0 {
tm.StartCleanerExternal(tickerChan[0])
} else if cleanupTickTime > 0 {
tm.StartCleanerInternal(cleanupTickTime)
}
return tm
}