/
intent.go
174 lines (142 loc) · 3.63 KB
/
intent.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
package anidb
import (
"github.com/Kovensky/go-fscache"
"strings"
"sync"
)
type notification interface{}
type intentStruct struct {
sync.Mutex
chs []chan notification
}
type intentMapStruct struct {
sync.Mutex
intentLock sync.Mutex // used by the Intent function
m map[string]*intentStruct
}
var intentMap = &intentMapStruct{
m: map[string]*intentStruct{},
}
func intentKey(key ...fscache.CacheKey) string {
return strings.Join(fscache.Stringify(key...), "-")
}
// Register a channel to be notified when the specified keys are notified.
// Returns whether the caller was the first to register intent for the given
// keys.
//
// Cache checks should be done after registering intent, since it's possible to
// register Intent while a Notify is running, and the Notify is done after
// setting the cache.
func (m *intentMapStruct) Intent(ch chan notification, keys ...fscache.CacheKey) bool {
key := intentKey(keys...)
m.intentLock.Lock()
defer m.intentLock.Unlock()
m.Lock()
s, ok := m.m[key]
if !ok {
s = &intentStruct{}
}
m.Unlock()
s.Lock()
s.chs = append(s.chs, ch)
s.Unlock()
m.Lock()
// key might have been deleted while only the struct itself was locked -- recheck
_, ok = m.m[key]
m.m[key] = s
m.Unlock()
return ok
}
// Locks the requested keys and return the locked intentStruct.
//
// The intentStruct can be directly unlocked, or given to Free to also
// remove it from the intent map.
func (m *intentMapStruct) LockIntent(keys ...fscache.CacheKey) *intentStruct {
m.Lock()
defer m.Unlock()
return m._lockIntent(keys...)
}
func (m *intentMapStruct) _lockIntent(keys ...fscache.CacheKey) *intentStruct {
s, ok := m.m[intentKey(keys...)]
if !ok {
return nil
}
s.Lock()
return s
}
// Removes the given intent from the intent map and unlocks the intentStruct.
func (m *intentMapStruct) Free(is *intentStruct, keys ...fscache.CacheKey) {
m.Lock()
defer m.Unlock()
m._free(is, keys...)
}
func (m *intentMapStruct) _free(is *intentStruct, keys ...fscache.CacheKey) {
// deletes the key before unlocking, Intent needs to recheck key status
delete(m.m, intentKey(keys...))
// better than unlocking then deleting -- could delete a "brand new" entry
if is != nil {
is.Unlock()
}
}
// Notifies and closes all channels that are listening for the specified keys;
// also removes them from the intent map.
//
// Should be called after setting the cache.
func (m *intentMapStruct) NotifyClose(v notification, keys ...fscache.CacheKey) {
m.Lock()
defer m.Unlock()
is := m._lockIntent(keys...)
defer m._free(is, keys...)
if is != nil {
is.NotifyClose(v)
}
}
// Closes all channels that are listening for the specified keys
// and removes them from the intent map.
func (m *intentMapStruct) Close(keys ...fscache.CacheKey) {
m.Lock()
defer m.Unlock()
is := m._lockIntent(keys...)
defer m._free(is, keys...)
if is != nil {
is.Close()
}
}
// Notifies all channels that are listening for the specified keys,
// but doesn't close or remove them from the intent map.
func (m *intentMapStruct) Notify(v notification, keys ...fscache.CacheKey) {
m.Lock()
defer m.Unlock()
is := m._lockIntent(keys...)
if is != nil {
defer is.Unlock()
is.Notify(v)
}
}
// NOTE: does not lock the stuct
func (s *intentStruct) Notify(v notification) {
for _, ch := range s.chs {
if ch != nil {
ch <- v
}
}
}
// NOTE: does not lock the struct
func (s *intentStruct) Close() {
for _, ch := range s.chs {
if ch != nil {
close(ch)
}
}
s.chs = nil
}
// NOTE: does not lock the struct
func (s *intentStruct) NotifyClose(v notification) {
for _, ch := range s.chs {
if ch != nil {
ch <- v
close(ch)
}
}
s.chs = nil
}