Skip to content

Commit eba7aed

Browse files
committed
remove selectCase and remove any signs of 2 defaults
Signed-off-by: alecholmez <[email protected]> oops remove that Signed-off-by: alecholmez <[email protected]> stash changes Signed-off-by: alecholmez <[email protected]>
1 parent 8e3e122 commit eba7aed

File tree

2 files changed

+46
-45
lines changed

2 files changed

+46
-45
lines changed

pkg/server/sotw/v3/server.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ func (s *server) process(str stream.Stream, reqCh <-chan *discovery.DiscoveryReq
8585
lastDiscoveryResponses := map[string]lastDiscoveryResponse{}
8686

8787
// a collection of stack allocated watches per request type
88-
watches := newWatches(reqCh)
88+
watches := newWatches()
8989

9090
defer func() {
91-
watches.Cancel()
91+
watches.close()
9292
if s.callbacks != nil {
9393
s.callbacks.OnStreamClosed(streamID)
9494
}
@@ -124,14 +124,6 @@ func (s *server) process(str stream.Stream, reqCh <-chan *discovery.DiscoveryReq
124124
return out.Nonce, str.Send(out)
125125
}
126126

127-
open := func(w *watch, req *discovery.DiscoveryRequest, responder chan cache.Response) {
128-
w.cancel = s.cache.CreateWatch(req, streamState, responder)
129-
watches.cases[w.index] = reflect.SelectCase{
130-
Dir: reflect.SelectRecv,
131-
Chan: reflect.ValueOf(responder),
132-
}
133-
}
134-
135127
if s.callbacks != nil {
136128
if err := s.callbacks.OnStreamOpen(str.Context(), streamID, defaultTypeURL); err != nil {
137129
return err
@@ -142,7 +134,7 @@ func (s *server) process(str stream.Stream, reqCh <-chan *discovery.DiscoveryReq
142134
var node = &core.Node{}
143135

144136
// recompute dynamic channels for this stream
145-
watches.RecomputeWatches(s.ctx, reqCh)
137+
watches.recompute(s.ctx, reqCh)
146138

147139
for {
148140
// The list of select cases looks like this:
@@ -204,27 +196,36 @@ func (s *server) process(str stream.Stream, reqCh <-chan *discovery.DiscoveryReq
204196
// We've found a pre-existing watch, lets check and update if needed.
205197
// If these requirements aren't satisfied, leave an open watch.
206198
if w.nonce == "" || w.nonce == nonce {
207-
w.Cancel()
199+
w.close()
208200

209-
open(w, req, responder)
201+
watches.addWatch(typeURL, &watch{
202+
cancel: s.cache.CreateWatch(req, streamState, responder),
203+
response: responder,
204+
})
210205
}
211206
} else {
212207
// No pre-existing watch exists, let's create one.
213208
// We need to precompute the watches first then open a watch in the cache.
214209
watches.responders[typeURL] = &watch{}
215210
w = watches.responders[typeURL]
216-
watches.RecomputeWatches(s.ctx, reqCh)
211+
watches.recompute(s.ctx, reqCh)
217212

218-
open(w, req, responder)
213+
watches.addWatch(typeURL, &watch{
214+
cancel: s.cache.CreateWatch(req, streamState, responder),
215+
response: responder,
216+
})
219217
}
218+
219+
// Recompute the dynamic select cases for this stream.
220+
watches.recompute(s.ctx, reqCh)
220221
default:
221222
// Channel n -> these are the dynamic list of responders that correspond to the stream request typeURL
222223
if !ok {
223-
return status.Errorf(codes.Unavailable, "resource watch failed")
224+
return status.Errorf(codes.Unavailable, "resource watch %d -> failed", index)
224225
}
225226

226227
res := value.Interface().(cache.Response)
227-
nonce, err := send(value.Interface().(cache.Response))
228+
nonce, err := send(res)
228229
if err != nil {
229230
return err
230231
}

pkg/server/sotw/v3/watches.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
88
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
9+
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
910
)
1011

1112
// watches for all xDS resource types
@@ -17,57 +18,56 @@ type watches struct {
1718
}
1819

1920
// newWatches creates and initializes watches.
20-
func newWatches(req <-chan *discovery.DiscoveryRequest) watches {
21+
func newWatches() watches {
2122
return watches{
2223
responders: make(map[string]*watch, int(types.UnknownType)),
23-
cases: make([]reflect.SelectCase, 2), // We use 2 for the default computation here: ctx.Done() + reqCh.Recv()
24+
cases: make([]reflect.SelectCase, 0),
2425
}
2526
}
2627

27-
// Cancel all watches
28-
func (w *watches) Cancel() {
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() {
2936
for _, watch := range w.responders {
30-
watch.Cancel()
37+
watch.close()
3138
}
3239
}
3340

3441
// recomputeWatches rebuilds the known list of dynamic channels if needed
35-
func (w *watches) RecomputeWatches(ctx context.Context, reqCh <-chan *discovery.DiscoveryRequest) {
36-
newCases := []reflect.SelectCase{
37-
{
42+
func (w *watches) recompute(ctx context.Context, req <-chan *discovery.DiscoveryRequest) {
43+
w.cases = w.cases[0:]
44+
w.cases = append(w.cases,
45+
reflect.SelectCase{
3846
Dir: reflect.SelectRecv,
3947
Chan: reflect.ValueOf(ctx.Done()),
40-
},
41-
{
48+
}, reflect.SelectCase{
4249
Dir: reflect.SelectRecv,
43-
Chan: reflect.ValueOf(reqCh),
50+
Chan: reflect.ValueOf(req),
4451
},
45-
}
52+
)
4653

47-
index := len(newCases)
4854
for _, watch := range w.responders {
49-
newCases = append(newCases, watch.selectCase)
50-
watch.index = index
51-
index++
55+
w.cases = append(w.cases, reflect.SelectCase{
56+
Dir: reflect.SelectRecv,
57+
Chan: reflect.ValueOf(watch.response),
58+
})
5259
}
53-
54-
w.cases = newCases
5560
}
5661

5762
// watch contains the necessary modifiables for receiving resource responses
5863
type watch struct {
59-
selectCase reflect.SelectCase
60-
cancel func()
61-
nonce string
62-
63-
// Index is used to track the location of this channel in watches. This allows us
64-
// to update the channel used at this slot without recomputing the entire list of select
65-
// statements.
66-
index int
64+
cancel func()
65+
nonce string
66+
response chan cache.Response
6767
}
6868

69-
// Cancel calls terminate and cancel
70-
func (w *watch) Cancel() {
69+
// close cancels an open watch
70+
func (w *watch) close() {
7171
if w.cancel != nil {
7272
w.cancel()
7373
}

0 commit comments

Comments
 (0)