Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 49bbfa6

Browse files
committed
fix a bunch of races + simpler and faster draining
1 parent b69e602 commit 49bbfa6

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

idx/memory/find_cache.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ func (c *FindCache) Add(orgId uint32, pattern string, nodes []*Node) {
100100
return
101101
}
102102
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+
}
104108
c.Unlock()
105109
}
106110
cache.Add(pattern, nodes)
@@ -139,11 +143,12 @@ func (c *FindCache) PurgeAll() {
139143
// disable it for `backoffTime`. Future InvalidateFor calls made during
140144
// the backoff time will then return immediately.
141145
func (c *FindCache) InvalidateFor(orgId uint32, path string) {
146+
c.RLock()
142147
if time.Now().Before(c.backoff[orgId]) {
148+
c.RUnlock()
143149
return
144150
}
145151

146-
c.RLock()
147152
cache, ok := c.cache[orgId]
148153
c.RUnlock()
149154
if !ok || cache.Len() < 1 {
@@ -157,17 +162,18 @@ func (c *FindCache) InvalidateFor(orgId uint32, path string) {
157162
select {
158163
case c.invalidateReqs <- req:
159164
default:
165+
log.Infof("memory-idx: findCache invalidate-queue full. Disabling cache for %s. num-cached-entries=%d", c.backoffTime.String(), cache.Len())
160166
c.Lock()
161167
c.backoff[orgId] = time.Now().Add(c.backoffTime)
162168
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
168174
}
169175
}
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()
171177
return
172178
}
173179
}

0 commit comments

Comments
 (0)