-
Notifications
You must be signed in to change notification settings - Fork 53
/
listener.go
274 lines (243 loc) · 6.84 KB
/
listener.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package health
import (
"context"
"math"
"math/rand"
"sync"
"time"
agentv1 "github.com/rancher/opni/pkg/agent"
controlv1 "github.com/rancher/opni/pkg/apis/control/v1"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/auth/cluster"
"github.com/rancher/opni/pkg/auth/session"
"github.com/rancher/opni/pkg/util"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/sync/semaphore"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
)
type Listener struct {
controlv1.UnsafeHealthListenerServer
ListenerOptions
statusUpdate chan StatusUpdate
healthUpdate chan HealthUpdate
idLocksMu sync.Mutex
idLocks map[string]*sync.Mutex
incomingHealthUpdatesMu sync.RWMutex
incomingHealthUpdates map[string]chan HealthUpdate
closed chan struct{}
sem *semaphore.Weighted
}
type ListenerOptions struct {
interval time.Duration
maxJitter time.Duration
maxConnections int64
updateQueueCap int
}
type ListenerOption func(*ListenerOptions)
func (o *ListenerOptions) apply(opts ...ListenerOption) {
for _, op := range opts {
op(o)
}
}
func WithPollInterval(interval time.Duration) ListenerOption {
return func(o *ListenerOptions) {
o.interval = interval
}
}
func WithMaxJitter(duration time.Duration) ListenerOption {
return func(o *ListenerOptions) {
o.maxJitter = duration
}
}
// Max concurrent connections. Defaults to math.MaxInt64
func WithMaxConnections(max int64) ListenerOption {
return func(o *ListenerOptions) {
o.maxConnections = max
}
}
// Max concurrent queued updates before blocking. Defaults to 1000
func WithUpdateQueueCap(cap int) ListenerOption {
return func(o *ListenerOptions) {
o.updateQueueCap = cap
}
}
func NewListener(opts ...ListenerOption) *Listener {
options := ListenerOptions{
// poll slowly, health updates are also sent on demand by the agent
interval: 30 * time.Second,
maxJitter: 30 * time.Second,
updateQueueCap: 1000,
maxConnections: math.MaxInt64,
}
options.apply(opts...)
if options.maxJitter > options.interval {
options.maxJitter = options.interval
}
l := &Listener{
ListenerOptions: options,
statusUpdate: make(chan StatusUpdate, options.updateQueueCap),
healthUpdate: make(chan HealthUpdate, options.updateQueueCap),
incomingHealthUpdates: make(map[string]chan HealthUpdate),
idLocks: make(map[string]*sync.Mutex),
closed: make(chan struct{}),
sem: semaphore.NewWeighted(options.maxConnections),
}
return l
}
func (l *Listener) publishStatus(id string, connected bool, attrs []session.Attribute) {
s := StatusUpdate{
ID: id,
Status: &corev1.Status{
Connected: connected,
Timestamp: timestamppb.Now(),
},
}
for _, attr := range attrs {
s.Status.SessionAttributes = append(s.Status.SessionAttributes, attr.Name())
}
l.statusUpdate <- s
}
func (l *Listener) HandleConnection(ctx context.Context, clientset HealthClientSet) {
// if the listener is closed, exit immediately
select {
case <-l.closed:
return
default:
}
if err := l.sem.Acquire(ctx, 1); err != nil {
return // context canceled
}
defer l.sem.Release(1) // 5th
id := cluster.StreamAuthorizedID(ctx)
attrs := session.StreamAuthorizedAttributes(ctx)
l.idLocksMu.Lock()
var clientLock *sync.Mutex
if m, ok := l.idLocks[id]; !ok {
clientLock = &sync.Mutex{}
l.idLocks[id] = clientLock
} else {
clientLock = m
}
l.idLocksMu.Unlock()
// locks keyed based on agent id ensure this function is reentrant during
// agent reconnects
clientLock.Lock()
defer clientLock.Unlock() // 4th
l.incomingHealthUpdatesMu.Lock()
incomingHealthUpdates := make(chan HealthUpdate, 10)
l.incomingHealthUpdates[id] = incomingHealthUpdates
l.incomingHealthUpdatesMu.Unlock()
defer func() { // 3rd
l.incomingHealthUpdatesMu.Lock()
delete(l.incomingHealthUpdates, id)
l.incomingHealthUpdatesMu.Unlock()
}()
l.publishStatus(id, true, attrs)
defer func() { // 2nd
l.healthUpdate <- HealthUpdate{
ID: id,
Health: &corev1.Health{
Timestamp: timestamppb.Now(),
Ready: false,
},
}
l.publishStatus(id, false, nil)
}()
curHealth, err := clientset.GetHealth(ctx, &emptypb.Empty{}, grpc.WaitForReady(true))
if err == nil {
l.healthUpdate <- HealthUpdate{
ID: id,
Health: &corev1.Health{
Timestamp: timestamppb.Now(),
Ready: curHealth.GetReady(),
Conditions: slices.Clone(curHealth.GetConditions()),
Annotations: maps.Clone(curHealth.GetAnnotations()),
},
}
}
calcDuration := func() time.Duration {
var jitter time.Duration
if l.maxJitter > 0 {
jitter = time.Duration(rand.Int63n(int64(l.maxJitter)))
}
return l.interval + jitter
}
timer := time.NewTimer(calcDuration())
defer timer.Stop() // 1st
for {
select {
case <-ctx.Done():
return
case <-l.closed:
return
case <-timer.C:
health, err := clientset.GetHealth(ctx, &emptypb.Empty{})
if err == nil {
healthEq := health.Equal(curHealth)
healthNewer := health.NewerThan(curHealth)
if !healthEq && healthNewer {
curHealth = health
l.healthUpdate <- HealthUpdate{
ID: id,
Health: util.ProtoClone(curHealth),
}
}
}
timer.Reset(calcDuration())
case up := <-incomingHealthUpdates:
health, id := up.Health, up.ID
healthEq := health.Equal(curHealth)
healthNewer := health.NewerThan(curHealth)
if !healthEq && healthNewer {
curHealth = util.ProtoClone(health)
l.healthUpdate <- HealthUpdate{
ID: id,
Health: util.ProtoClone(curHealth),
}
}
// drain the timer channel if it happened to fire at the same time
if !timer.Stop() {
<-timer.C
}
timer.Reset(calcDuration())
}
}
}
func (l *Listener) StatusC() chan StatusUpdate {
return l.statusUpdate
}
func (l *Listener) HealthC() chan HealthUpdate {
return l.healthUpdate
}
func (l *Listener) Close() {
// Prevent any new connections from being handled
close(l.closed)
go func() {
// Wait for all existing active handlers to exit
l.sem.Acquire(context.Background(), l.maxConnections)
// Close the health and status channels
close(l.statusUpdate)
close(l.healthUpdate)
}()
}
// Implements gateway.ConnectionHandler
func (l *Listener) HandleAgentConnection(ctx context.Context, clientset agentv1.ClientSet) {
l.HandleConnection(ctx, clientset)
}
// Implements controlv1.HealthListenerServer
func (l *Listener) UpdateHealth(ctx context.Context, req *corev1.Health) (*emptypb.Empty, error) {
id := cluster.StreamAuthorizedID(ctx)
l.incomingHealthUpdatesMu.RLock()
defer l.incomingHealthUpdatesMu.RUnlock()
if ch, ok := l.incomingHealthUpdates[id]; ok {
h := HealthUpdate{
ID: id,
Health: util.ProtoClone(req),
}
ch <- h
}
return &emptypb.Empty{}, nil
}