Skip to content

Commit 67ba599

Browse files
committed
gopls/internal/lsp/cache: fix boundary cond in parseCache age eviction
Setting maxAge=0 should degenerate the parse cache to evict immediately (necessary to avoid flakiness in tests on platforms with coarse clock granularity). Fixes golang/go#61550 Change-Id: I6cb03cfb670faa6c6995fd3c4c5a98dd1dadb344 Reviewed-on: https://go-review.googlesource.com/c/tools/+/512635 Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 478577b commit 67ba599

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

gopls/internal/lsp/cache/parse_cache.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ var parsePadding = 1000 // mutable for testing
9595
// caching) multiple files. This is necessary for type-checking, where files
9696
// must be parsed in a common fileset.
9797
type parseCache struct {
98-
maxAge time.Duration // interval at which to collect expired cache entries
99-
done chan struct{} // closed when GC is stopped
98+
expireAfter time.Duration // interval at which to collect expired cache entries
99+
done chan struct{} // closed when GC is stopped
100100

101101
mu sync.Mutex
102102
m map[parseKey]*parseCacheEntry
@@ -106,14 +106,14 @@ type parseCache struct {
106106
}
107107

108108
// newParseCache creates a new parse cache and starts a goroutine to garbage
109-
// collect old entries that are older than maxAge.
109+
// collect entries whose age is at least expireAfter.
110110
//
111111
// Callers must call parseCache.stop when the parse cache is no longer in use.
112-
func newParseCache(maxAge time.Duration) *parseCache {
112+
func newParseCache(expireAfter time.Duration) *parseCache {
113113
c := &parseCache{
114-
maxAge: maxAge,
115-
m: make(map[parseKey]*parseCacheEntry),
116-
done: make(chan struct{}),
114+
expireAfter: expireAfter,
115+
m: make(map[parseKey]*parseCacheEntry),
116+
done: make(chan struct{}),
117117
}
118118
go c.gc()
119119
return c
@@ -275,7 +275,7 @@ func (c *parseCache) gcOnce() {
275275

276276
for len(c.m) > parseCacheMinFiles {
277277
e := heap.Pop(&c.lru).(*parseCacheEntry)
278-
if now.Sub(e.walltime) > c.maxAge {
278+
if now.Sub(e.walltime) >= c.expireAfter {
279279
delete(c.m, e.key)
280280
} else {
281281
heap.Push(&c.lru, e)

gopls/internal/lsp/cache/parse_cache_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func TestParseCache(t *testing.T) {
5151
files = append(files, dummyFileHandles(parseCacheMinFiles-1)...)
5252

5353
pgfs3, err := cache.parseFiles(ctx, fset, source.ParseFull, false, files...)
54+
if err != nil {
55+
t.Fatal(err)
56+
}
5457
pgf3 := pgfs3[0]
5558
if pgf3 != pgf1 {
5659
t.Errorf("parseFiles(%q, ...): unexpected cache miss", uri)

0 commit comments

Comments
 (0)