Skip to content

Commit c91455d

Browse files
authored
Merge pull request kubernetes#90249 from wojtek-t/bookmark_every_minute
Send watch bookmarks every minute
2 parents 340ac70 + d4b532e commit c91455d

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
@@ -171,10 +171,7 @@ func newTimeBucketWatchers(clock clock.Clock) *watcherBookmarkTimeBuckets {
171171
// adds a watcher to the bucket, if the deadline is before the start, it will be
172172
// added to the first one.
173173
func (t *watcherBookmarkTimeBuckets) addWatcher(w *cacheWatcher) bool {
174-
nextTime, ok := w.nextBookmarkTime(t.clock.Now())
175-
if !ok {
176-
return false
177-
}
174+
nextTime := w.nextBookmarkTime(t.clock.Now())
178175
bucketID := nextTime.Unix()
179176
t.lock.Lock()
180177
defer t.lock.Unlock()
@@ -1219,13 +1216,26 @@ func (c *cacheWatcher) add(event *watchCacheEvent, timer *time.Timer) bool {
12191216
}
12201217
}
12211218

1222-
func (c *cacheWatcher) nextBookmarkTime(now time.Time) (time.Time, bool) {
1223-
// For now we return 2s before deadline (and maybe +infinity is now already passed this time)
1224-
// but it gives us extensibility for the future(false when deadline is not set).
1219+
// bookmarkHeartbeatFrequency defines how frequently bookmarks should be
1220+
// under regular conditions.
1221+
const bookmarkHeartbeatFrequency = time.Minute
1222+
1223+
func (c *cacheWatcher) nextBookmarkTime(now time.Time) time.Time {
1224+
// We try to send bookmarks:
1225+
// (a) roughly every minute
1226+
// (b) right before the watcher timeout - for now we simply set it 2s before
1227+
// the deadline
1228+
// The former gives us periodicity if the watch breaks due to unexpected
1229+
// conditions, the later ensures that on timeout the watcher is as close to
1230+
// now as possible - this covers 99% of cases.
1231+
heartbeatTime := now.Add(bookmarkHeartbeatFrequency)
12251232
if c.deadline.IsZero() {
1226-
return c.deadline, false
1233+
return heartbeatTime
1234+
}
1235+
if pretimeoutTime := c.deadline.Add(-2 * time.Second); pretimeoutTime.Before(heartbeatTime) {
1236+
return pretimeoutTime
12271237
}
1228-
return c.deadline.Add(-2 * time.Second), true
1238+
return heartbeatTime
12291239
}
12301240

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

0 commit comments

Comments
 (0)