-
Notifications
You must be signed in to change notification settings - Fork 8
/
watcher.go
162 lines (125 loc) · 2.85 KB
/
watcher.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package kcache
import (
"context"
"time"
lifecycle "github.com/boz/go-lifecycle"
logutil "github.com/boz/go-logutil"
"github.com/boz/kcache/client"
"github.com/pkg/errors"
)
const (
watchRetryDelay = time.Second
)
type watcher interface {
reset(string) error
events() <-chan Event
Done() <-chan struct{}
Error() error
}
type _watcher struct {
version string
client client.WatchClient
resetch chan string
evtch chan chan (<-chan Event)
log logutil.Log
lc lifecycle.Lifecycle
ctx context.Context
}
func newWatcher(ctx context.Context, log logutil.Log, stopch <-chan struct{}, client client.WatchClient) watcher {
log = log.WithComponent("watcher")
lc := lifecycle.New()
w := &_watcher{
client: client,
resetch: make(chan string),
evtch: make(chan chan (<-chan Event)),
log: log,
lc: lc,
ctx: ctx,
}
go w.lc.WatchContext(ctx)
go w.lc.WatchChannel(stopch)
go w.run()
return w
}
func (w *_watcher) reset(vsn string) error {
select {
case w.resetch <- vsn:
return nil
case <-w.lc.ShuttingDown():
return errors.WithStack(ErrNotRunning)
}
}
func (w *_watcher) events() <-chan Event {
req := make(chan (<-chan Event), 1)
select {
case w.evtch <- req:
return <-req
case <-w.lc.ShuttingDown():
return nil
}
}
func (w *_watcher) Done() <-chan struct{} {
return w.lc.Done()
}
func (w *_watcher) Error() error {
return w.lc.Error()
}
func (w *_watcher) run() {
defer w.lc.ShutdownCompleted()
ctx, cancel := context.WithCancel(w.ctx)
defer cancel()
var session watchSession = nullWatchSession{}
var outch chan Event
var curVersion string
var retry *time.Timer
mainloop:
for {
select {
case err := <-w.lc.ShutdownRequest():
w.log.Debugf("shutdown request: %v", err)
w.lc.ShutdownInitiated(err)
break mainloop
case vsn := <-w.resetch:
w.log.Debugf("ressetting to version %v", vsn)
if retry != nil {
retry.Stop()
retry = nil
}
session.stop()
session = newWatchSession(ctx, w.log, w.client, vsn)
outch = make(chan Event, EventBufsiz)
curVersion = vsn
case <-session.done():
w.log.Debugf("session done. retrying version %v in %v", curVersion, watchRetryDelay)
session.stop()
session = nullWatchSession{}
outch = nil
retry = w.scheduleRetry(w.resetch, curVersion)
case evt := <-session.events():
select {
case outch <- evt:
default:
w.log.Errorf("output buffer full")
}
curVersion = evt.Resource().GetResourceVersion()
w.log.Debugf("session event: %v version: %v", evt, curVersion)
case reqch := <-w.evtch:
reqch <- outch
}
}
cancel()
if retry != nil {
retry.Stop()
}
if donech := session.done(); donech != nil {
<-donech
}
}
func (w *_watcher) scheduleRetry(ch chan string, vsn string) *time.Timer {
return time.AfterFunc(watchRetryDelay, func() {
select {
case ch <- vsn:
case <-w.lc.ShuttingDown():
}
})
}