agent/cache: Inline the refresh function to make recursion more obvious

fetch is already an exceptionally long function, but hiding the
recrusion in a function call likely does not help.
This commit is contained in:
Daniel Nephin 2020-04-02 17:08:51 -04:00
parent 98ef66e70a
commit 4d398d26ae
1 changed files with 19 additions and 29 deletions

48
agent/cache/cache.go vendored
View File

@ -599,7 +599,25 @@ func (c *Cache) fetch(tEntry typeEntry, key string, r Request, allowNew bool, at
// If refresh is enabled, run the refresh in due time. The refresh // If refresh is enabled, run the refresh in due time. The refresh
// below might block, but saves us from spawning another goroutine. // below might block, but saves us from spawning another goroutine.
if tEntry.Opts.Refresh { if tEntry.Opts.Refresh {
c.refresh(tEntry.Opts, attempt, tEntry, key, r) // Check if cache was stopped
if atomic.LoadUint32(&c.stopped) == 1 {
return
}
// If we're over the attempt minimum, start an exponential backoff.
if wait := backOffWait(attempt); wait > 0 {
time.Sleep(wait)
}
// If we have a timer, wait for it
if tEntry.Opts.RefreshTimer > 0 {
time.Sleep(tEntry.Opts.RefreshTimer)
}
// Trigger. The "allowNew" field is false because in the time we were
// waiting to refresh we may have expired and got evicted. If that
// happened, we don't want to create a new entry.
c.fetch(tEntry, key, r, false, attempt, 0, true, true)
} }
}() }()
@ -630,34 +648,6 @@ func backOffWait(failures uint) time.Duration {
return 0 return 0
} }
// refresh triggers a fetch for a specific Request according to the
// registration options.
func (c *Cache) refresh(opts *RegisterOptions, attempt uint, tEntry typeEntry, key string, r Request) {
// Sanity-check, we should not schedule anything that has refresh disabled
if !opts.Refresh {
return
}
// Check if cache was stopped
if atomic.LoadUint32(&c.stopped) == 1 {
return
}
// If we're over the attempt minimum, start an exponential backoff.
if wait := backOffWait(attempt); wait > 0 {
time.Sleep(wait)
}
// If we have a timer, wait for it
if opts.RefreshTimer > 0 {
time.Sleep(opts.RefreshTimer)
}
// Trigger. The "allowNew" field is false because in the time we were
// waiting to refresh we may have expired and got evicted. If that
// happened, we don't want to create a new entry.
c.fetch(tEntry, key, r, false, attempt, 0, true, true)
}
// runExpiryLoop is a blocking function that watches the expiration // runExpiryLoop is a blocking function that watches the expiration
// heap and invalidates entries that have expired. // heap and invalidates entries that have expired.
func (c *Cache) runExpiryLoop() { func (c *Cache) runExpiryLoop() {