Debian Codegraph @

pkg/storage/cacher/progress/watch_progress.go

raw
1 /*
2 Copyright 2023 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package progress
18
19 import (
20 "context"
21 "sync"
22 "time"
23
24 "google.golang.org/grpc/metadata"
25
26 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
27 "k8s.io/apimachinery/pkg/util/wait"
28
29 "k8s.io/klog/v2"
30 "k8s.io/utils/clock"
31 )
32
33 const (
34 // progressRequestPeriod determines period of requesting progress
35 // from etcd when there is a request waiting for watch cache to be fresh.
36 progressRequestPeriod = 100 * time.Millisecond
37 )
38
39 func NewConditionalProgressRequester(requestWatchProgress WatchProgressRequester, clock TickerFactory, contextMetadata metadata.MD) *ConditionalProgressRequester {
40 pr := &ConditionalProgressRequester{
41 clock: clock,
42 requestWatchProgress: requestWatchProgress,
43 contextMetadata: contextMetadata,
44 }
45 pr.cond = sync.NewCond(&pr.mux)
46 return pr
47 }
48
49 type WatchProgressRequester func(ctx context.Context) error
50
51 type TickerFactory interface {
52 NewTimer(time.Duration) clock.Timer
53 }
54
55 // ConditionalProgressRequester will request progress notification if there
56 // is a request waiting for watch cache to be fresh.
57 type ConditionalProgressRequester struct {
58 clock TickerFactory
59 requestWatchProgress WatchProgressRequester
60 contextMetadata metadata.MD
61
62 mux sync.Mutex
63 cond *sync.Cond
64 waiting int
65 stopped bool
66 }
67
68 func (pr *ConditionalProgressRequester) Run(stopCh <-chan struct{}) {
69 ctx := wait.ContextForChannel(stopCh)
70 if pr.contextMetadata != nil {
71 ctx = metadata.NewOutgoingContext(ctx, pr.contextMetadata)
72 }
73 go func() {
74 defer utilruntime.HandleCrash()
75 <-stopCh
76 pr.mux.Lock()
77 defer pr.mux.Unlock()
78 pr.stopped = true
79 pr.cond.Signal()
80 }()
81 timer := pr.clock.NewTimer(progressRequestPeriod)
82 defer timer.Stop()
83 for {
84 stopped := func() bool {
85 pr.mux.Lock()
86 defer pr.mux.Unlock()
87 for pr.waiting == 0 && !pr.stopped {
88 pr.cond.Wait()
89 }
90 return pr.stopped
91 }()
92 if stopped {
93 return
94 }
95
96 select {
97 case <-timer.C():
98 shouldRequest := func() bool {
99 pr.mux.Lock()
100 defer pr.mux.Unlock()
101 return pr.waiting > 0 && !pr.stopped
102 }()
103 if !shouldRequest {
104 timer.Reset(0)
105 continue
106 }
107 timer.Reset(progressRequestPeriod)
108 err := pr.requestWatchProgress(ctx)
109 if err != nil {
110 klog.V(4).InfoS("Error requesting bookmark", "err", err)
111 }
112 case <-stopCh:
113 return
114 }
115 }
116 }
117
118 func (pr *ConditionalProgressRequester) Add() {
119 pr.mux.Lock()
120 defer pr.mux.Unlock()
121 pr.waiting += 1
122 pr.cond.Signal()
123 }
124
125 func (pr *ConditionalProgressRequester) Remove() {
126 pr.mux.Lock()
127 defer pr.mux.Unlock()
128 pr.waiting -= 1
129 }