-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.go
More file actions
63 lines (55 loc) · 1.26 KB
/
background.go
File metadata and controls
63 lines (55 loc) · 1.26 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
package ttlmap
import "sync"
var backgroundMutex = newKeyMutex()
type keyMutex struct {
c *sync.Cond
l sync.Locker
s map[string]struct{}
}
// Create new keyMutex
func newKeyMutex() *keyMutex {
l := sync.Mutex{}
return &keyMutex{c: sync.NewCond(&l), l: &l, s: make(map[string]struct{})}
}
// Unlock keyMutex by unique ID
func (km *keyMutex) Unlock(key string) {
km.l.Lock()
defer km.l.Unlock()
delete(km.s, key)
km.c.Broadcast()
}
// Lock keyMutex by unique ID
func (km *keyMutex) Lock(key string) bool {
km.l.Lock()
defer km.l.Unlock()
if _, ok := km.s[key]; ok {
return false
}
km.s[key] = struct{}{}
return true
}
// Wait blocks until the given key is unlocked by a prior Lock call.
// It does not acquire the lock; callers typically call Get after waiting
// or attempt to Lock again to become the next owner.
func (km *keyMutex) Wait(key string) {
km.l.Lock()
for {
if _, ok := km.s[key]; !ok {
km.l.Unlock()
return
}
km.c.Wait()
}
}
func (m CacheMap) BackgroundUpdate(key string, updater func() (interface{}, error)) {
// Lock the key from writes
locked := backgroundMutex.Lock(key)
if locked {
// Defer release write lock
defer backgroundMutex.Unlock(key)
value, err := updater()
if err == nil {
m.Set(key, value, nil)
}
}
}