Skip to content

Commit

Permalink
narrow lock time to not include network request
Browse files Browse the repository at this point in the history
  • Loading branch information
natenjoy committed Mar 1, 2024
1 parent 2a3e36c commit 2d1b1b4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
1 change: 0 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2d1b1b4

Please sign in to comment.