-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathwatcher.go
More file actions
63 lines (54 loc) · 1.25 KB
/
watcher.go
File metadata and controls
63 lines (54 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package k8s
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
)
var (
initializationTimeout = 30 * time.Second
sleepBetweenChecks = 500 * time.Millisecond
)
type resourceToWatch interface {
LastSyncResourceVersion() string
}
type watcher struct {
resource resourceToWatch
resourceType string
timeout time.Duration
}
func newWatcher(resource resourceToWatch, resourceType string) *watcher {
return &watcher{
resource: resource,
resourceType: resourceType,
timeout: initializationTimeout,
}
}
func (w *watcher) run() error {
timedOut := make(chan struct{}, 1)
defer close(timedOut)
initialized := make(chan struct{}, 1)
defer close(initialized)
go func() {
for {
select {
case <-timedOut:
log.Warnf("[%s watcher] timed out", w.resourceType)
return
case <-time.Tick(sleepBetweenChecks):
if w.resource.LastSyncResourceVersion() != "" {
log.Infof("[%s watcher] initialized", w.resourceType)
initialized <- struct{}{}
return
}
log.Debugf("[%s watcher] waiting for initialization", w.resourceType)
}
}
}()
select {
case <-initialized:
return nil
case <-time.After(w.timeout):
timedOut <- struct{}{}
return fmt.Errorf("[%s watcher] timed out", w.resourceType)
}
}