google.golang.org/grpc@v1.72.2/resolver/ringhash/attr.go (about)

     1  /*
     2   *
     3   * Copyright 2025 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  
    19  // Package ringhash implements resolver related functions for the ring_hash
    20  // load balancing policy.
    21  package ringhash
    22  
    23  import (
    24  	"google.golang.org/grpc/resolver"
    25  )
    26  
    27  type hashKeyType string
    28  
    29  // hashKeyKey is the key to store the ring hash key attribute in
    30  // a resolver.Endpoint attribute.
    31  const hashKeyKey = hashKeyType("grpc.resolver.ringhash.hash_key")
    32  
    33  // SetHashKey sets the hash key for this endpoint. Combined with the ring_hash
    34  // load balancing policy, it allows placing the endpoint on the ring based on an
    35  // arbitrary string instead of the IP address. If hashKey is empty, the endpoint
    36  // is returned unmodified.
    37  //
    38  // # Experimental
    39  //
    40  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    41  // later release.
    42  func SetHashKey(endpoint resolver.Endpoint, hashKey string) resolver.Endpoint {
    43  	if hashKey == "" {
    44  		return endpoint
    45  	}
    46  	endpoint.Attributes = endpoint.Attributes.WithValue(hashKeyKey, hashKey)
    47  	return endpoint
    48  }
    49  
    50  // HashKey returns the hash key attribute of endpoint. If this attribute is
    51  // not set, it returns the empty string.
    52  //
    53  // # Experimental
    54  //
    55  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    56  // later release.
    57  func HashKey(endpoint resolver.Endpoint) string {
    58  	hashKey, _ := endpoint.Attributes.Value(hashKeyKey).(string)
    59  	return hashKey
    60  }