github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/go-control-plane/pkg/cache/v3/delta.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  
    20  	"github.com/hxx258456/ccgo/go-control-plane/pkg/cache/types"
    21  	"github.com/hxx258456/ccgo/go-control-plane/pkg/server/stream/v3"
    22  )
    23  
    24  // groups together resource-related arguments for the createDeltaResponse function
    25  type resourceContainer struct {
    26  	resourceMap   map[string]types.Resource
    27  	versionMap    map[string]string
    28  	systemVersion string
    29  }
    30  
    31  func createDeltaResponse(ctx context.Context, req *DeltaRequest, state stream.StreamState, resources resourceContainer) *RawDeltaResponse {
    32  	// variables to build our response with
    33  	nextVersionMap := make(map[string]string)
    34  	filtered := make([]types.Resource, 0, len(resources.resourceMap))
    35  	toRemove := make([]string, 0)
    36  
    37  	// If we are handling a wildcard request, we want to respond with all resources
    38  	switch {
    39  	case state.IsWildcard():
    40  		for name, r := range resources.resourceMap {
    41  			// Since we've already precomputed the version hashes of the new snapshot,
    42  			// we can just set it here to be used for comparison later
    43  			version := resources.versionMap[name]
    44  			nextVersionMap[name] = version
    45  			prevVersion, found := state.GetResourceVersions()[name]
    46  			if !found || (prevVersion != nextVersionMap[name]) {
    47  				filtered = append(filtered, r)
    48  			}
    49  		}
    50  	default:
    51  		// Reply only with the requested resources
    52  		for name, prevVersion := range state.GetResourceVersions() {
    53  			if r, ok := resources.resourceMap[name]; ok {
    54  				nextVersion := resources.versionMap[name]
    55  				if prevVersion != nextVersion {
    56  					filtered = append(filtered, r)
    57  				}
    58  				nextVersionMap[name] = nextVersion
    59  			}
    60  		}
    61  	}
    62  
    63  	// Compute resources for removal regardless of the request type
    64  	for name := range state.GetResourceVersions() {
    65  		if _, ok := resources.resourceMap[name]; !ok {
    66  			toRemove = append(toRemove, name)
    67  		}
    68  	}
    69  
    70  	return &RawDeltaResponse{
    71  		DeltaRequest:      req,
    72  		Resources:         filtered,
    73  		RemovedResources:  toRemove,
    74  		NextVersionMap:    nextVersionMap,
    75  		SystemVersionInfo: resources.systemVersion,
    76  		Ctx:               ctx,
    77  	}
    78  }