Debian Codegraph @

pkg/server/sotw/v3/watches.go

raw symbols SCIP
1 package sotw
2
3 import (
4 "context"
5 "reflect"
6
7 discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
8 "github.com/envoyproxy/go-control-plane/pkg/cache/types"
9 "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
10 )
11
12 // watches for all xDS resource types
13 type watches struct {
14 responders map[string]*watch
15
16 // cases is a dynamic select case for the watched channels.
17 cases []reflect.SelectCase
18 }
19
20 // newWatches creates and initializes watches.
21 func newWatches() watches {
22 return watches{
23 responders: make(map[string]*watch, int(types.UnknownType)),
24 cases: make([]reflect.SelectCase, 0),
25 }
26 }
27
28 // addWatch creates a new watch entry in the watches map.
29 // Watches are sorted by typeURL.
30 func (w *watches) addWatch(typeURL string, watch *watch) {
31 w.responders[typeURL] = watch
32 }
33
34 // close all open watches
35 func (w *watches) close() {
36 for _, watch := range w.responders {
37 watch.close()
38 }
39 }
40
41 // recomputeWatches rebuilds the known list of dynamic channels if needed
42 func (w *watches) recompute(ctx context.Context, req <-chan *discovery.DiscoveryRequest) {
43 w.cases = w.cases[:0] // Clear the existing cases while retaining capacity.
44
45 w.cases = append(w.cases,
46 reflect.SelectCase{
47 Dir: reflect.SelectRecv,
48 Chan: reflect.ValueOf(ctx.Done()),
49 }, reflect.SelectCase{
50 Dir: reflect.SelectRecv,
51 Chan: reflect.ValueOf(req),
52 },
53 )
54
55 for _, watch := range w.responders {
56 w.cases = append(w.cases, reflect.SelectCase{
57 Dir: reflect.SelectRecv,
58 Chan: reflect.ValueOf(watch.response),
59 })
60 }
61 }
62
63 // watch contains the necessary modifiable data for receiving resource responses
64 type watch struct {
65 cancel func()
66 nonce string
67 response chan cache.Response
68 }
69
70 // close cancels an open watch
71 func (w *watch) close() {
72 if w.cancel != nil {
73 w.cancel()
74 }
75 }