@@ -100,7 +100,11 @@ func (c *FindCache) Add(orgId uint32, pattern string, nodes []*Node) {
100
100
return
101
101
}
102
102
c .Lock ()
103
- c .cache [orgId ] = cache
103
+ // re-check. someone else may have added a cache in the meantime.
104
+ _ , ok := c .cache [orgId ]
105
+ if ! ok {
106
+ c .cache [orgId ] = cache
107
+ }
104
108
c .Unlock ()
105
109
}
106
110
cache .Add (pattern , nodes )
@@ -139,11 +143,12 @@ func (c *FindCache) PurgeAll() {
139
143
// disable it for `backoffTime`. Future InvalidateFor calls made during
140
144
// the backoff time will then return immediately.
141
145
func (c * FindCache ) InvalidateFor (orgId uint32 , path string ) {
146
+ c .RLock ()
142
147
if time .Now ().Before (c .backoff [orgId ]) {
148
+ c .RUnlock ()
143
149
return
144
150
}
145
151
146
- c .RLock ()
147
152
cache , ok := c .cache [orgId ]
148
153
c .RUnlock ()
149
154
if ! ok || cache .Len () < 1 {
@@ -157,17 +162,18 @@ func (c *FindCache) InvalidateFor(orgId uint32, path string) {
157
162
select {
158
163
case c .invalidateReqs <- req :
159
164
default :
165
+ log .Infof ("memory-idx: findCache invalidate-queue full. Disabling cache for %s. num-cached-entries=%d" , c .backoffTime .String (), cache .Len ())
160
166
c .Lock ()
161
167
c .backoff [orgId ] = time .Now ().Add (c .backoffTime )
162
168
delete (c .cache , orgId )
163
- c . Unlock ()
164
- for i := 0 ; i < len ( c . invalidateReqs ); i ++ {
165
- select {
166
- case <- c . invalidateReqs :
167
- default :
169
+ // drain queue
170
+ for {
171
+ _ , ok := c . invalidateReqs
172
+ if ! ok {
173
+ break
168
174
}
169
175
}
170
- log . Infof ( "memory-idx: findCache invalidate-queue full. Disabling cache for %s. num-cached-entries=%d" , c . backoffTime . String (), cache . Len () )
176
+ c . Unlock ( )
171
177
return
172
178
}
173
179
}
0 commit comments