dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/resource/locality_id.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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  /*
    19   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  // Package internal contains functions/structs shared by xds
    25  // balancers/resolvers.
    26  package resource
    27  
    28  import (
    29  	"encoding/json"
    30  	"fmt"
    31  )
    32  
    33  import (
    34  	"google.golang.org/grpc/resolver"
    35  )
    36  
    37  // LocalityID is xds.Locality without XXX fields, so it can be used as map
    38  // keys.
    39  //
    40  // xds.Locality cannot be map keys because one of the XXX fields is a slice.
    41  type LocalityID struct {
    42  	Region  string `json:"region,omitempty"`
    43  	Zone    string `json:"zone,omitempty"`
    44  	SubZone string `json:"subZone,omitempty"`
    45  }
    46  
    47  // ToString generates a string representation of LocalityID by marshaling it into
    48  // json. Not calling it String() so printf won't call it.
    49  func (l LocalityID) ToString() (string, error) {
    50  	b, err := json.Marshal(l)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	return string(b), nil
    55  }
    56  
    57  // Equal allows the values to be compared by Attributes.Equal.
    58  func (l LocalityID) Equal(o interface{}) bool {
    59  	ol, ok := o.(LocalityID)
    60  	if !ok {
    61  		return false
    62  	}
    63  	return l.Region == ol.Region && l.Zone == ol.Zone && l.SubZone == ol.SubZone
    64  }
    65  
    66  // LocalityIDFromString converts a json representation of locality, into a
    67  // LocalityID struct.
    68  func LocalityIDFromString(s string) (ret LocalityID, _ error) {
    69  	err := json.Unmarshal([]byte(s), &ret)
    70  	if err != nil {
    71  		return LocalityID{}, fmt.Errorf("%s is not a well formatted locality ID, error: %v", s, err)
    72  	}
    73  	return ret, nil
    74  }
    75  
    76  type localityKeyType string
    77  
    78  const localityKey = localityKeyType("grpc.xds.internal.address.locality")
    79  
    80  // GetLocalityID returns the locality ID of addr.
    81  func GetLocalityID(addr resolver.Address) LocalityID {
    82  	path, _ := addr.BalancerAttributes.Value(localityKey).(LocalityID)
    83  	return path
    84  }
    85  
    86  // SetLocalityID sets locality ID in addr to l.
    87  func SetLocalityID(addr resolver.Address, l LocalityID) resolver.Address {
    88  	addr.BalancerAttributes = addr.BalancerAttributes.WithValue(localityKey, l)
    89  	return addr
    90  }