| 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 |
|
| 13 |
type watches struct { |
| 14 |
responders map[string]*watch |
| 15 |
|
| 16 |
|
| 17 |
cases []reflect.SelectCase |
| 18 |
} |
| 19 |
|
| 20 |
|
| 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 |
|
| 29 |
|
| 30 |
func (w *watches) addWatch(typeURL string, watch *watch) { |
| 31 |
w.responders[typeURL] = watch |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
func (w *watches) close() { |
| 36 |
for _, watch := range w.responders { |
| 37 |
watch.close() |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func (w *watches) recompute(ctx context.Context, req <-chan *discovery.DiscoveryRequest) { |
| 43 |
w.cases = w.cases[:0] |
| 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 |
|
| 64 |
type watch struct { |
| 65 |
cancel func() |
| 66 |
nonce string |
| 67 |
response chan cache.Response |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (w *watch) close() { |
| 72 |
if w.cancel != nil { |
| 73 |
w.cancel() |
| 74 |
} |
| 75 |
} |