Skip to content

Commit d4b532e

Browse files
committed
Send watch bookmarks every minute
1 parent 0926c9c commit d4b532e

File tree

1 file changed

+19
-9
lines changed
  • staging/src/k8s.io/apiserver/pkg/storage/cacher

1 file changed

+19
-9
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,7 @@ func newTimeBucketWatchers(clock clock.Clock) *watcherBookmarkTimeBuckets {
193193
// adds a watcher to the bucket, if the deadline is before the start, it will be
194194
// added to the first one.
195195
func (t *watcherBookmarkTimeBuckets) addWatcher(w *cacheWatcher) bool {
196-
nextTime, ok := w.nextBookmarkTime(t.clock.Now())
197-
if !ok {
198-
return false
199-
}
196+
nextTime := w.nextBookmarkTime(t.clock.Now())
200197
bucketID := nextTime.Unix()
201198
t.lock.Lock()
202199
defer t.lock.Unlock()
@@ -1240,13 +1237,26 @@ func (c *cacheWatcher) add(event *watchCacheEvent, timer *time.Timer) bool {
12401237
}
12411238
}
12421239

1243-
func (c *cacheWatcher) nextBookmarkTime(now time.Time) (time.Time, bool) {
1244-
// For now we return 2s before deadline (and maybe +infinity is now already passed this time)
1245-
// but it gives us extensibility for the future(false when deadline is not set).
1240+
// bookmarkHeartbeatFrequency defines how frequently bookmarks should be
1241+
// under regular conditions.
1242+
const bookmarkHeartbeatFrequency = time.Minute
1243+
1244+
func (c *cacheWatcher) nextBookmarkTime(now time.Time) time.Time {
1245+
// We try to send bookmarks:
1246+
// (a) roughly every minute
1247+
// (b) right before the watcher timeout - for now we simply set it 2s before
1248+
// the deadline
1249+
// The former gives us periodicity if the watch breaks due to unexpected
1250+
// conditions, the later ensures that on timeout the watcher is as close to
1251+
// now as possible - this covers 99% of cases.
1252+
heartbeatTime := now.Add(bookmarkHeartbeatFrequency)
12461253
if c.deadline.IsZero() {
1247-
return c.deadline, false
1254+
return heartbeatTime
1255+
}
1256+
if pretimeoutTime := c.deadline.Add(-2 * time.Second); pretimeoutTime.Before(heartbeatTime) {
1257+
return pretimeoutTime
12481258
}
1249-
return c.deadline.Add(-2 * time.Second), true
1259+
return heartbeatTime
12501260
}
12511261

12521262
func getEventObject(object runtime.Object) runtime.Object {

0 commit comments

Comments
 (0)