Skip to content

Commit dc859a2

Browse files
committed
ptcache: reorganize periodicallyRefreshProtectedtsCache
Minor change to `periodicallyRefreshProtectedtsCache` to avoid having to look at `Timer.Read` (which will go away soon). The resulting code is easier to understand. Informs #143655 Release note: None
1 parent 1a22f58 commit dc859a2

File tree

1 file changed

+15
-16
lines changed
  • pkg/kv/kvserver/protectedts/ptcache

1 file changed

+15
-16
lines changed

pkg/kv/kvserver/protectedts/ptcache/cache.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -168,42 +168,41 @@ func (c *Cache) periodicallyRefreshProtectedtsCache(ctx context.Context) {
168168
defer timer.Stop()
169169
timer.Reset(0) // Read immediately upon startup
170170
var lastReset time.Time
171-
var future singleflight.Future
172171
// TODO(ajwerner): consider resetting the timer when the state is updated
173172
// due to a call to Refresh.
174173
for {
175174
select {
176175
case <-timer.C:
177-
// Let's not reset the timer until we get our response.
178176
timer.Read = true
179-
future, _ = c.sf.DoChan(ctx,
177+
future, _ := c.sf.DoChan(ctx,
180178
refreshKey,
181179
singleflight.DoOpts{
182180
Stop: c.stopper,
183181
InheritCancelation: false,
184182
},
185183
c.doSingleFlightUpdate,
186184
)
187-
case <-settingChanged:
188-
if timer.Read { // we're currently fetching
189-
continue
185+
186+
select {
187+
case <-future.C():
188+
case <-c.stopper.ShouldQuiesce():
189+
return
190+
}
191+
res := future.WaitForResult(ctx)
192+
if res.Err != nil && ctx.Err() != nil {
193+
log.Errorf(ctx, "failed to refresh protected timestamps: %v", res.Err)
190194
}
195+
timer.Reset(protectedts.PollInterval.Get(&c.settings.SV))
196+
lastReset = timeutil.Now()
197+
198+
case <-settingChanged:
191199
interval := protectedts.PollInterval.Get(&c.settings.SV)
192200
// NB: It's okay if nextUpdate is a negative duration; timer.Reset will
193201
// treat a negative duration as zero and send a notification immediately.
194202
nextUpdate := interval - timeutil.Since(lastReset)
195203
timer.Reset(nextUpdate)
196204
lastReset = timeutil.Now()
197-
case <-future.C():
198-
res := future.WaitForResult(ctx)
199-
if res.Err != nil {
200-
if ctx.Err() == nil {
201-
log.Errorf(ctx, "failed to refresh protected timestamps: %v", res.Err)
202-
}
203-
}
204-
future.Reset()
205-
timer.Reset(protectedts.PollInterval.Get(&c.settings.SV))
206-
lastReset = timeutil.Now()
205+
207206
case <-c.stopper.ShouldQuiesce():
208207
return
209208
}

0 commit comments

Comments
 (0)