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

     1  /*
     2   *
     3   * Copyright 2019 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 internal contains functions/structs shared by xds
    19  // balancers/resolvers.
    20  package internal
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  
    26  	"github.com/hxx258456/ccgo/grpc/resolver"
    27  )
    28  
    29  // LocalityID is xds.Locality without XXX fields, so it can be used as map
    30  // keys.
    31  //
    32  // xds.Locality cannot be map keys because one of the XXX fields is a slice.
    33  type LocalityID struct {
    34  	Region  string `json:"region,omitempty"`
    35  	Zone    string `json:"zone,omitempty"`
    36  	SubZone string `json:"subZone,omitempty"`
    37  }
    38  
    39  // ToString generates a string representation of LocalityID by marshalling it into
    40  // json. Not calling it String() so printf won't call it.
    41  func (l LocalityID) ToString() (string, error) {
    42  	b, err := json.Marshal(l)
    43  	if err != nil {
    44  		return "", err
    45  	}
    46  	return string(b), nil
    47  }
    48  
    49  // Equal allows the values to be compared by Attributes.Equal.
    50  func (l LocalityID) Equal(o interface{}) bool {
    51  	ol, ok := o.(LocalityID)
    52  	if !ok {
    53  		return false
    54  	}
    55  	return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone
    56  }
    57  
    58  // LocalityIDFromString converts a json representation of locality, into a
    59  // LocalityID struct.
    60  func LocalityIDFromString(s string) (ret LocalityID, _ error) {
    61  	err := json.Unmarshal([]byte(s), &ret)
    62  	if err != nil {
    63  		return LocalityID{}, fmt.Errorf("%s is not a well formatted locality ID, error: %v", s, err)
    64  	}
    65  	return ret, nil
    66  }
    67  
    68  type localityKeyType string
    69  
    70  const localityKey = localityKeyType("grpc.xds.internal.address.locality")
    71  
    72  // GetLocalityID returns the locality ID of addr.
    73  func GetLocalityID(addr resolver.Address) LocalityID {
    74  	path, _ := addr.BalancerAttributes.Value(localityKey).(LocalityID)
    75  	return path
    76  }
    77  
    78  // SetLocalityID sets locality ID in addr to l.
    79  func SetLocalityID(addr resolver.Address, l LocalityID) resolver.Address {
    80  	addr.BalancerAttributes = addr.BalancerAttributes.WithValue(localityKey, l)
    81  	return addr
    82  }