gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/pkg/cache/v3/mux.go (about) 1 // Copyright 2020 Envoyproxy Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cache 16 17 import ( 18 "context" 19 "errors" 20 "gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/stream/v3" 21 ) 22 23 // MuxCache multiplexes across several caches using a classification function. 24 // If there is no matching cache for a classification result, the cache 25 // responds with an empty closed channel, which effectively terminates the 26 // stream on the server. It might be preferred to respond with a "nil" channel 27 // instead which will leave the stream open in case the stream is aggregated by 28 // making sure there is always a matching cache. 29 type MuxCache struct { 30 // Classification functions. 31 Classify func(*Request) string 32 ClassifyDelta func(*DeltaRequest) string 33 // Muxed caches. 34 Caches map[string]Cache 35 } 36 37 var _ Cache = &MuxCache{} 38 39 func (mux *MuxCache) CreateWatch(request *Request, value chan Response) func() { 40 key := mux.Classify(request) 41 cache, exists := mux.Caches[key] 42 if !exists { 43 value <- nil 44 return nil 45 } 46 return cache.CreateWatch(request, value) 47 } 48 49 func (mux *MuxCache) CreateDeltaWatch(request *DeltaRequest, state stream.StreamState, value chan DeltaResponse) func() { 50 key := mux.ClassifyDelta(request) 51 cache, exists := mux.Caches[key] 52 if !exists { 53 value <- nil 54 return nil 55 } 56 return cache.CreateDeltaWatch(request, state, value) 57 } 58 59 //goland:noinspection GoUnusedParameter 60 func (mux *MuxCache) Fetch(ctx context.Context, request *Request) (Response, error) { 61 return nil, errors.New("not implemented") 62 }