mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-28 14:40:51 +02:00
Merge pull request #10058 from ellemouton/fix/rpc-graph-cache-race
rpcserver: fix race condition in graph cache eviction
This commit is contained in:
41
rpcserver.go
41
rpcserver.go
@@ -874,18 +874,39 @@ func (r *rpcServer) addDeps(ctx context.Context, s *server,
|
||||
|
||||
graphCacheDuration := r.cfg.Caches.RPCGraphCacheDuration
|
||||
if graphCacheDuration != 0 {
|
||||
r.graphCacheEvictor = time.AfterFunc(graphCacheDuration, func() {
|
||||
// Grab the mutex and purge the current populated
|
||||
// describe graph response.
|
||||
r.graphCache.Lock()
|
||||
defer r.graphCache.Unlock()
|
||||
r.graphCacheEvictor = time.NewTimer(graphCacheDuration)
|
||||
|
||||
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
|
||||
// after the duration.
|
||||
r.graphCacheEvictor.Reset(graphCacheDuration)
|
||||
})
|
||||
// Reset the timer so we'll fire
|
||||
// again after the specified
|
||||
// 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
|
||||
|
Reference in New Issue
Block a user