From 2d1b1b4f76b9c61cd2f6c92c2318eda7dbcd0d2c Mon Sep 17 00:00:00 2001 From: natenjoy Date: Fri, 1 Mar 2024 13:37:49 -0600 Subject: [PATCH] narrow lock time to not include network request --- cache.go | 1 - queue.go | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cache.go b/cache.go index 2d69513..df99bb1 100644 --- a/cache.go +++ b/cache.go @@ -147,7 +147,6 @@ func (c *Cache) getOrFetch(ctx context.Context, u string, forceRefresh bool) (in // has this entry been fetched? (but ignore and do a fetch // if forceRefresh is true) if forceRefresh || !e.hasBeenFetched() { - // Only one goroutine may enter this section. // redundant checks allow cached gets avoid the semaphore, // which allows them to return cached data immediately diff --git a/queue.go b/queue.go index 84a1e3e..227c749 100644 --- a/queue.go +++ b/queue.go @@ -318,12 +318,12 @@ func (q *queue) refreshLoop(ctx context.Context, errSink ErrSink) { } func (q *queue) fetchAndStore(ctx context.Context, e *entry) error { - e.mu.Lock() - defer e.mu.Unlock() - + now := time.Now() // synchronously go fetch - e.lastFetch = time.Now() res, err := q.fetch.fetch(ctx, e.request) + e.mu.Lock() + defer e.mu.Unlock() + e.lastFetch = now if err != nil { // Even if the request failed, we need to queue the next fetch q.enqueueNextFetch(nil, e)