github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/xdsclient/pubsub/dump.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package pubsub
    19  
    20  import (
    21  	anypb "github.com/golang/protobuf/ptypes/any"
    22  	"github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/xdsresource"
    23  )
    24  
    25  func rawFromCache(s string, cache interface{}) *anypb.Any {
    26  	switch c := cache.(type) {
    27  	case map[string]xdsresource.ListenerUpdate:
    28  		if v, ok := c[s]; ok {
    29  			return v.Raw
    30  		}
    31  		return nil
    32  	case map[string]xdsresource.RouteConfigUpdate:
    33  		if v, ok := c[s]; ok {
    34  			return v.Raw
    35  		}
    36  		return nil
    37  	case map[string]xdsresource.ClusterUpdate:
    38  		if v, ok := c[s]; ok {
    39  			return v.Raw
    40  		}
    41  		return nil
    42  	case map[string]xdsresource.EndpointsUpdate:
    43  		if v, ok := c[s]; ok {
    44  			return v.Raw
    45  		}
    46  		return nil
    47  	default:
    48  		return nil
    49  	}
    50  }
    51  
    52  // Dump dumps the resource for the given type.
    53  func (pb *Pubsub) Dump(t xdsresource.ResourceType) map[string]xdsresource.UpdateWithMD {
    54  	pb.mu.Lock()
    55  	defer pb.mu.Unlock()
    56  
    57  	var (
    58  		md    map[string]xdsresource.UpdateMetadata
    59  		cache interface{}
    60  	)
    61  	switch t {
    62  	case xdsresource.ListenerResource:
    63  		md = pb.ldsMD
    64  		cache = pb.ldsCache
    65  	case xdsresource.RouteConfigResource:
    66  		md = pb.rdsMD
    67  		cache = pb.rdsCache
    68  	case xdsresource.ClusterResource:
    69  		md = pb.cdsMD
    70  		cache = pb.cdsCache
    71  	case xdsresource.EndpointsResource:
    72  		md = pb.edsMD
    73  		cache = pb.edsCache
    74  	default:
    75  		pb.logger.Errorf("dumping resource of unknown type: %v", t)
    76  		return nil
    77  	}
    78  
    79  	ret := make(map[string]xdsresource.UpdateWithMD, len(md))
    80  	for s, md := range md {
    81  		ret[s] = xdsresource.UpdateWithMD{
    82  			MD:  md,
    83  			Raw: rawFromCache(s, cache),
    84  		}
    85  	}
    86  	return ret
    87  }