dubbo.apache.org/dubbo-go/v3@v3.1.1/cluster/loadbalance/consistenthashing/loadbalance.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  package consistenthashing
    19  
    20  import (
    21  	"encoding/json"
    22  	"hash/crc32"
    23  	"regexp"
    24  )
    25  
    26  import (
    27  	"dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
    28  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    29  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    30  	"dubbo.apache.org/dubbo-go/v3/protocol"
    31  )
    32  
    33  const (
    34  	// HashNodes hash nodes
    35  	HashNodes = "hash.nodes"
    36  	// HashArguments key of hash arguments in url
    37  	HashArguments = "hash.arguments"
    38  )
    39  
    40  var (
    41  	selectors = make(map[string]*selector)
    42  	re        = regexp.MustCompile(constant.CommaSplitPattern)
    43  )
    44  
    45  func init() {
    46  	extension.SetLoadbalance(constant.LoadBalanceKeyConsistentHashing, newConshashLoadBalance)
    47  }
    48  
    49  // conshashLoadBalance implementation of load balancing: using consistent hashing
    50  type conshashLoadBalance struct{}
    51  
    52  // newConshashLoadBalance creates NewConsistentHashLoadBalance
    53  //
    54  // The same parameters of the request is always sent to the same provider.
    55  func newConshashLoadBalance() loadbalance.LoadBalance {
    56  	return &conshashLoadBalance{}
    57  }
    58  
    59  // Select gets invoker based on load balancing strategy
    60  func (lb *conshashLoadBalance) Select(invokers []protocol.Invoker, invocation protocol.Invocation) protocol.Invoker {
    61  	methodName := invocation.MethodName()
    62  	key := invokers[0].GetURL().ServiceKey() + "." + methodName
    63  
    64  	// hash the invokers
    65  	var bs []byte
    66  	for _, invoker := range invokers {
    67  		b, err := json.Marshal(invoker)
    68  		if err != nil {
    69  			return nil
    70  		}
    71  		bs = append(bs, b...)
    72  	}
    73  	hashCode := crc32.ChecksumIEEE(bs)
    74  	selector, ok := selectors[key]
    75  	if !ok || selector.hashCode != hashCode {
    76  		selectors[key] = newSelector(invokers, methodName, hashCode)
    77  		selector = selectors[key]
    78  	}
    79  	return selector.Select(invocation)
    80  }