dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/utils/backoff/backoff.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 2017 gRPC authors.
    21   *
    22   */
    23  
    24  // Package backoff implement the backoff strategy for gRPC.
    25  //
    26  // This is kept in internal until the gRPC project decides whether or not to
    27  // allow alternative backoff strategies.
    28  package backoff
    29  
    30  import (
    31  	"time"
    32  )
    33  
    34  import (
    35  	grpcbackoff "google.golang.org/grpc/backoff"
    36  )
    37  
    38  import (
    39  	"dubbo.apache.org/dubbo-go/v3/xds/utils/grpcrand"
    40  )
    41  
    42  // Strategy defines the methodology for backing off after a grpc connection
    43  // failure.
    44  type Strategy interface {
    45  	// Backoff returns the amount of time to wait before the next retry given
    46  	// the number of consecutive failures.
    47  	Backoff(retries int) time.Duration
    48  }
    49  
    50  // DefaultExponential is an exponential backoff implementation using the
    51  // default values for all the configurable knobs defined in
    52  // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md.
    53  var DefaultExponential = Exponential{Config: grpcbackoff.DefaultConfig}
    54  
    55  // Exponential implements exponential backoff algorithm as defined in
    56  // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md.
    57  type Exponential struct {
    58  	// Config contains all options to configure the backoff algorithm.
    59  	Config grpcbackoff.Config
    60  }
    61  
    62  // Backoff returns the amount of time to wait before the next retry given the
    63  // number of retries.
    64  func (bc Exponential) Backoff(retries int) time.Duration {
    65  	if retries == 0 {
    66  		return bc.Config.BaseDelay
    67  	}
    68  	backoff, max := float64(bc.Config.BaseDelay), float64(bc.Config.MaxDelay)
    69  	for backoff < max && retries > 0 {
    70  		backoff *= bc.Config.Multiplier
    71  		retries--
    72  	}
    73  	if backoff > max {
    74  		backoff = max
    75  	}
    76  	// Randomize backoff delays so that if a cluster of requests start at
    77  	// the same time, they won't operate in lockstep.
    78  	backoff *= 1 + bc.Config.Jitter*(grpcrand.Float64()*2-1)
    79  	if backoff < 0 {
    80  		return 0
    81  	}
    82  	return time.Duration(backoff)
    83  }