github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/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  
    21  	"github.com/hxx258456/ccgo/go-control-plane/pkg/server/stream/v3"
    22  )
    23  
    24  // MuxCache multiplexes across several caches using a classification function.
    25  // If there is no matching cache for a classification result, the cache
    26  // responds with an empty closed channel, which effectively terminates the
    27  // stream on the server. It might be preferred to respond with a "nil" channel
    28  // instead which will leave the stream open in case the stream is aggregated by
    29  // making sure there is always a matching cache.
    30  type MuxCache struct {
    31  	// Classification functions.
    32  	Classify      func(*Request) string
    33  	ClassifyDelta func(*DeltaRequest) string
    34  	// Muxed caches.
    35  	Caches map[string]Cache
    36  }
    37  
    38  var _ Cache = &MuxCache{}
    39  
    40  func (mux *MuxCache) CreateWatch(request *Request, value chan Response) func() {
    41  	key := mux.Classify(request)
    42  	cache, exists := mux.Caches[key]
    43  	if !exists {
    44  		value <- nil
    45  		return nil
    46  	}
    47  	return cache.CreateWatch(request, value)
    48  }
    49  
    50  func (mux *MuxCache) CreateDeltaWatch(request *DeltaRequest, state stream.StreamState, value chan DeltaResponse) func() {
    51  	key := mux.ClassifyDelta(request)
    52  	cache, exists := mux.Caches[key]
    53  	if !exists {
    54  		value <- nil
    55  		return nil
    56  	}
    57  	return cache.CreateDeltaWatch(request, state, value)
    58  }
    59  
    60  func (mux *MuxCache) Fetch(ctx context.Context, request *Request) (Response, error) {
    61  	return nil, errors.New("not implemented")
    62  }