From 010f4e0d292a47d6c48f486c328b9e56a3b17a20 Mon Sep 17 00:00:00 2001 From: Leonard Cohnen Date: Wed, 3 Jan 2024 14:06:29 +0100 Subject: [PATCH] move clearing to get calls --- internal/attestation/snp/cachedClient.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/attestation/snp/cachedClient.go b/internal/attestation/snp/cachedClient.go index 016f6b40e..0ff8ee9a9 100644 --- a/internal/attestation/snp/cachedClient.go +++ b/internal/attestation/snp/cachedClient.go @@ -26,11 +26,17 @@ func NewCachedKDSHTTPClient(ticker clock.Ticker, log *slog.Logger) *cachedKDSHTT gcTicker: ticker, } - go c.garbageCollect() return c } func (c *cachedKDSHTTPClient) Get(url string) ([]byte, error) { + select { + case <-c.gcTicker.C(): + c.logger.Debug("Garbage collecting") + c.cache.Clear() + default: + } + if cached, ok := c.cache.Get(url); ok { c.logger.Debug("Get cached", "url", url) return cached, nil @@ -44,10 +50,3 @@ func (c *cachedKDSHTTPClient) Get(url string) ([]byte, error) { c.cache.Set(url, res) return res, nil } - -func (c *cachedKDSHTTPClient) garbageCollect() { - for range c.gcTicker.C() { - c.logger.Debug("Garbage collecting") - c.cache.Clear() - } -}