Debian Codegraph @

pkg/server/sotw/v3/ads.go

raw symbols SCIP
1 package sotw
2
3 import (
4 "google.golang.org/grpc/codes"
5 "google.golang.org/grpc/status"
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 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
11 )
12
13 // process handles a bi-di stream request
14 func (s *server) processADS(sw *streamWrapper, reqCh chan *discovery.DiscoveryRequest, defaultTypeURL string) error {
15 // We make a responder channel here so we can multiplex responses from the dynamic channels.
16 sw.watches.addWatch(resource.AnyType, &watch{
17 // Create a buffered channel the size of the known resource types.
18 response: make(chan cache.Response, types.UnknownType),
19 cancel: func() {
20 close(sw.watches.responders[resource.AnyType].response)
21 },
22 })
23
24 process := func(resp cache.Response) error {
25 nonce, err := sw.send(resp)
26 if err != nil {
27 return err
28 }
29
30 sw.watches.responders[resp.GetRequest().GetTypeUrl()].nonce = nonce
31 return nil
32 }
33
34 // Instead of creating a separate channel for each incoming request and abandoning the old one
35 // This algorithm uses (and reuses) a single channel for all request types and guarantees
36 // the server will send updates over the wire in an ordered fashion.
37 // Downside is there is no longer back pressure per resource.
38 // There is potential for a dropped response from the cache but this is not impactful
39 // to the client since SOTW version handling is global and a new sequence will be
40 // initiated on a new request.
41 processAllExcept := func(typeURL string) error {
42 for {
43 select {
44 // We watch the multiplexed ADS channel for incoming responses.
45 case res := <-sw.watches.responders[resource.AnyType].response:
46 if res.GetRequest().GetTypeUrl() != typeURL {
47 if err := process(res); err != nil {
48 return err
49 }
50 }
51 default:
52 return nil
53 }
54 }
55 }
56
57 // This control loop strictly orders resources when running in ADS mode.
58 // It should be treated as a child process of the original process() loop
59 // and should return on close of stream or error. This will cause the
60 // cleanup routines in the parent process() loop to execute.
61 for {
62 select {
63 case <-s.ctx.Done():
64 return nil
65 // We only watch the multiplexed channel since all values will come through from process.
66 case res := <-sw.watches.responders[resource.AnyType].response:
67 if err := process(res); err != nil {
68 return status.Errorf(codes.Unavailable, "%v", err)
69 }
70 case req, ok := <-reqCh:
71 // Input stream ended or failed.
72 if !ok {
73 return nil
74 }
75
76 // Received an empty request over the request channel. Can't respond.
77 if req == nil {
78 return status.Errorf(codes.Unavailable, "empty request")
79 }
80
81 // Only first request is guaranteed to hold node info so if it's missing, reassign.
82 if req.GetNode() != nil {
83 sw.node = req.GetNode()
84 } else {
85 req.Node = sw.node
86 }
87
88 // Nonces can be reused across streams; we verify nonce only if nonce is not initialized.
89 nonce := req.GetResponseNonce()
90
91 // type URL is required for ADS but is implicit for xDS
92 if defaultTypeURL == resource.AnyType {
93 if req.GetTypeUrl() == "" {
94 return status.Errorf(codes.InvalidArgument, "type URL is required for ADS")
95 }
96 }
97
98 if s.callbacks != nil {
99 if err := s.callbacks.OnStreamRequest(sw.ID, req); err != nil {
100 return err
101 }
102 }
103
104 if lastResponse, ok := sw.lastDiscoveryResponses[req.GetTypeUrl()]; ok {
105 if lastResponse.nonce == "" || lastResponse.nonce == nonce {
106 // Let's record Resource names that a client has received.
107 sw.streamState.SetKnownResourceNames(req.GetTypeUrl(), lastResponse.resources)
108 }
109 }
110
111 typeURL := req.GetTypeUrl()
112 // Use the multiplexed channel for new watches.
113 responder := sw.watches.responders[resource.AnyType].response
114 if w, ok := sw.watches.responders[typeURL]; ok {
115 // We've found a pre-existing watch, lets check and update if needed.
116 // If these requirements aren't satisfied, leave an open watch.
117 if w.nonce == "" || w.nonce == nonce {
118 w.close()
119
120 // Only process if we have an existing watch otherwise go ahead and create.
121 if err := processAllExcept(typeURL); err != nil {
122 return err
123 }
124
125 sw.watches.addWatch(typeURL, &watch{
126 cancel: s.cache.CreateWatch(req, sw.streamState, responder),
127 response: responder,
128 })
129 }
130 } else {
131 // No pre-existing watch exists, let's create one.
132 // We need to precompute the watches first then open a watch in the cache.
133 sw.watches.addWatch(typeURL, &watch{
134 cancel: s.cache.CreateWatch(req, sw.streamState, responder),
135 response: responder,
136 })
137 }
138 }
139 }
140 }