/
mutex_cache.go
67 lines (58 loc) · 1.33 KB
/
mutex_cache.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
/*
Copyright (c) 2024 Diagrid Inc.
Licensed under the MIT License.
*/
package locking
import (
"context"
"sync"
"github.com/diagridio/go-etcd-cron/collector"
)
// MutexStore allows reuse of the same dist mutex in Etcd for a given key.
type MutexStore struct {
lock sync.RWMutex
cache map[string]DistributedMutex
mutexBuilderFunc func(string) (DistributedMutex, error)
collector collector.Collector
}
func NewMutexStore(mutexBuilderFunc func(string) (DistributedMutex, error), collector collector.Collector) *MutexStore {
return &MutexStore{
cache: map[string]DistributedMutex{},
mutexBuilderFunc: mutexBuilderFunc,
collector: collector,
}
}
func (m *MutexStore) Get(key string) (DistributedMutex, error) {
m.lock.RLock()
mutex := m.cache[key]
m.lock.RUnlock()
if mutex != nil {
return mutex, nil
}
m.lock.Lock()
defer m.lock.Unlock()
mutex = m.cache[key]
if mutex != nil {
return mutex, nil
}
mutex, err := m.mutexBuilderFunc(key)
if err != nil {
return nil, err
}
m.collector.Add(func(ctx context.Context) error {
m.Delete(key)
return nil
})
m.cache[key] = mutex
return mutex, nil
}
func (m *MutexStore) Delete(keys ...string) {
m.lock.Lock()
defer m.lock.Unlock()
for _, key := range keys {
_, ok := m.cache[key]
if ok {
delete(m.cache, key)
}
}
}