Merge pull request #10058 from ellemouton/fix/rpc-graph-cache-race
rpcserver: fix race condition in graph cache eviction
This commit is contained in:
1 file changed
+31
-10
+31
-10
@@ -874,18 +874,39 @@ func (r *rpcServer) addDeps(ctx context.Context, s *server,
|
|||||||
|
|
||||||
graphCacheDuration := r.cfg.Caches.RPCGraphCacheDuration
|
graphCacheDuration := r.cfg.Caches.RPCGraphCacheDuration
|
||||||
if graphCacheDuration != 0 {
|
if graphCacheDuration != 0 {
|
||||||
r.graphCacheEvictor = time.AfterFunc(graphCacheDuration, func() {
|
r.graphCacheEvictor = time.NewTimer(graphCacheDuration)
|
||||||
// Grab the mutex and purge the current populated
|
|
||||||
// describe graph response.
|
|
||||||
r.graphCache.Lock()
|
|
||||||
defer r.graphCache.Unlock()
|
|
||||||
|
|
||||||
r.describeGraphResp = nil
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
// The timer fired, so we'll purge the graph
|
||||||
|
// cache.
|
||||||
|
case <-r.graphCacheEvictor.C:
|
||||||
|
r.graphCache.Lock()
|
||||||
|
r.describeGraphResp = nil
|
||||||
|
r.graphCache.Unlock()
|
||||||
|
|
||||||
// Reset ourselves as well at the end so we run again
|
// Reset the timer so we'll fire
|
||||||
// after the duration.
|
// again after the specified
|
||||||
r.graphCacheEvictor.Reset(graphCacheDuration)
|
// duration.
|
||||||
})
|
r.graphCacheEvictor.Reset(
|
||||||
|
graphCacheDuration,
|
||||||
|
)
|
||||||
|
|
||||||
|
// The server is quitting, so we'll stop the
|
||||||
|
// timer and exit.
|
||||||
|
case <-r.quit:
|
||||||
|
if !r.graphCacheEvictor.Stop() {
|
||||||
|
// Drain the channel if Stop()
|
||||||
|
// returns false, meaning the
|
||||||
|
// timer has already fired.
|
||||||
|
<-r.graphCacheEvictor.C
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Reference in new issue
Block a user