diff --git a/drive/driveimpl/compositedav/compositedav.go b/drive/driveimpl/compositedav/compositedav.go index c6ec79772..d4edbc523 100644 --- a/drive/driveimpl/compositedav/compositedav.go +++ b/drive/driveimpl/compositedav/compositedav.go @@ -222,8 +222,7 @@ func (h *Handler) GetChild(name string) *Child { return child } -// Close closes this Handler,including closing all idle connections on children -// and stopping the StatCache (if caching is enabled). +// Close closes this Handler, including closing all idle connections on children. func (h *Handler) Close() { h.childrenMu.RLock() oldChildren := h.children @@ -233,10 +232,6 @@ func (h *Handler) Close() { for _, child := range oldChildren { child.CloseIdleConnections() } - - if h.StatCache != nil { - h.StatCache.stop() - } } func (h *Handler) findChildLocked(name string) (int, *Child) { diff --git a/drive/driveimpl/compositedav/stat_cache.go b/drive/driveimpl/compositedav/stat_cache.go index 176a09d84..82d445315 100644 --- a/drive/driveimpl/compositedav/stat_cache.go +++ b/drive/driveimpl/compositedav/stat_cache.go @@ -216,9 +216,15 @@ func (c *StatCache) setLocked(name string, depth int, ce *cacheEntry) { cache = ttlcache.New( ttlcache.WithTTL[string, *cacheEntry](c.TTL), ) - go cache.Start() c.cachesByDepthAndPath[depth] = cache } + // Rather than running ttlcache's background cleanup goroutine (which + // would need to be started and stopped, and which races if Stop is + // called before Start runs), evict expired entries opportunistically + // on each write. Get already ignores expired entries, so this only + // bounds memory. DeleteExpired is cheap: it pops from the front of the + // expiration heap and stops at the first unexpired entry. + cache.DeleteExpired() cache.Set(name, ce, ttlcache.DefaultTTL) } @@ -236,15 +242,6 @@ func (c *StatCache) invalidate() { } } -func (c *StatCache) stop() { - c.mu.Lock() - defer c.mu.Unlock() - - for _, cache := range c.cachesByDepthAndPath { - cache.Stop() - } -} - type cacheEntry struct { Status int Raw []byte diff --git a/drive/driveimpl/compositedav/stat_cache_test.go b/drive/driveimpl/compositedav/stat_cache_test.go index 9be286389..2d5242b9c 100644 --- a/drive/driveimpl/compositedav/stat_cache_test.go +++ b/drive/driveimpl/compositedav/stat_cache_test.go @@ -70,7 +70,6 @@ func TestStatCacheNoTimeout(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 5 * time.Second} - defer c.stop() // check get before set fetched := c.get(childPath, 0) @@ -98,7 +97,6 @@ func TestStatCacheTimeout(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 250 * time.Millisecond} - defer c.stop() // set new stat ce := newCacheEntry(http.StatusMultiStatus, fullChild) @@ -130,7 +128,6 @@ func TestParentChildRelationship(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 24 * time.Hour} // don't expire - defer c.stop() missingParentPath := "/missingparent" unparseableParentPath := "/unparseable" @@ -224,7 +221,6 @@ func TestUnicodeNormalizationInsensitivity(t *testing.T) { tstest.ResourceCheck(t) c := &StatCache{TTL: 24 * time.Hour} // don't expire - defer c.stop() const ( nfcParent = "\u30ae\u30bf\u30fc" // ギター in NFC: ギ is the single code point U+30AE