github.com/cloudwego/kitex@v0.9.0/pkg/retry/backup.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package retry
    18  
    19  import (
    20  	"fmt"
    21  )
    22  
    23  const maxBackupRetryTimes = 2
    24  
    25  // NewBackupPolicy init backup request policy
    26  // the param delayMS is suggested to set as TP99
    27  func NewBackupPolicy(delayMS uint32) *BackupPolicy {
    28  	if delayMS == 0 {
    29  		panic("invalid backup request delay duration")
    30  	}
    31  	p := &BackupPolicy{
    32  		RetryDelayMS: delayMS,
    33  		StopPolicy: StopPolicy{
    34  			MaxRetryTimes:    1,
    35  			DisableChainStop: false,
    36  			CBPolicy: CBPolicy{
    37  				ErrorRate: defaultCBErrRate,
    38  			},
    39  		},
    40  	}
    41  	return p
    42  }
    43  
    44  // WithMaxRetryTimes default is 1, not include first call
    45  func (p *BackupPolicy) WithMaxRetryTimes(retryTimes int) {
    46  	if retryTimes < 0 || retryTimes > maxBackupRetryTimes {
    47  		panic(fmt.Errorf("maxBackupRetryTimes=%d", maxBackupRetryTimes))
    48  	}
    49  	p.StopPolicy.MaxRetryTimes = retryTimes
    50  }
    51  
    52  // DisableChainRetryStop disables chain stop
    53  func (p *BackupPolicy) DisableChainRetryStop() {
    54  	p.StopPolicy.DisableChainStop = true
    55  }
    56  
    57  // WithRetryBreaker sets error rate.
    58  func (p *BackupPolicy) WithRetryBreaker(errRate float64) {
    59  	p.StopPolicy.CBPolicy.ErrorRate = errRate
    60  	if err := checkCBErrorRate(&p.StopPolicy.CBPolicy); err != nil {
    61  		panic(err)
    62  	}
    63  }
    64  
    65  // WithRetrySameNode set retry to use the same node.
    66  func (p *BackupPolicy) WithRetrySameNode() {
    67  	p.RetrySameNode = true
    68  }
    69  
    70  // String is used to print human readable debug info.
    71  func (p BackupPolicy) String() string {
    72  	return fmt.Sprintf("{RetryDelayMS:%+v StopPolicy:%+v RetrySameNode:%+v}", p.RetryDelayMS, p.StopPolicy, p.RetrySameNode)
    73  }