github.com/KinWaiYuen/client-go/v2@v2.5.4/tikv/backoff.go (about)

     1  // Copyright 2021 TiKV Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/backoff.go
    19  //
    20  
    21  // Copyright 2021 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package tikv
    36  
    37  import (
    38  	"context"
    39  
    40  	"github.com/KinWaiYuen/client-go/v2/internal/retry"
    41  	"github.com/KinWaiYuen/client-go/v2/kv"
    42  )
    43  
    44  // Backoffer is a utility for retrying queries.
    45  type Backoffer = retry.Backoffer
    46  
    47  // BackoffConfig defines the backoff configuration.
    48  type BackoffConfig = retry.Config
    49  
    50  // Maximum total sleep time(in ms) for kv/cop commands.
    51  const (
    52  	gcResolveLockMaxBackoff = 100000
    53  )
    54  
    55  // NewBackofferWithVars creates a Backoffer with maximum sleep time(in ms) and kv.Variables.
    56  func NewBackofferWithVars(ctx context.Context, maxSleep int, vars *kv.Variables) *Backoffer {
    57  	return retry.NewBackofferWithVars(ctx, maxSleep, vars)
    58  }
    59  
    60  // NewBackoffer creates a Backoffer with maximum sleep time(in ms).
    61  func NewBackoffer(ctx context.Context, maxSleep int) *Backoffer {
    62  	return retry.NewBackoffer(ctx, maxSleep)
    63  }
    64  
    65  // TxnStartKey is a key for transaction start_ts info in context.Context.
    66  func TxnStartKey() interface{} {
    67  	return retry.TxnStartKey
    68  }
    69  
    70  // BoRegionMiss returns the default backoff config for RegionMiss.
    71  func BoRegionMiss() *BackoffConfig {
    72  	return retry.BoRegionMiss
    73  }
    74  
    75  // BoTiFlashRPC returns the default backoff config for TiFlashRPC.
    76  func BoTiFlashRPC() *BackoffConfig {
    77  	return retry.BoTiFlashRPC
    78  }
    79  
    80  // BoTxnLock returns the default backoff config for TxnLock.
    81  func BoTxnLock() *BackoffConfig {
    82  	return retry.BoTxnLock
    83  }
    84  
    85  // BoPDRPC returns the default backoff config for PDRPC.
    86  func BoPDRPC() *BackoffConfig {
    87  	return retry.BoPDRPC
    88  }
    89  
    90  // BoTiKVRPC returns the default backoff config for TiKVRPC.
    91  func BoTiKVRPC() *BackoffConfig {
    92  	return retry.BoTiKVRPC
    93  }
    94  
    95  // NewGcResolveLockMaxBackoffer creates a Backoffer for Gc to resolve lock.
    96  func NewGcResolveLockMaxBackoffer(ctx context.Context) *Backoffer {
    97  	return retry.NewBackofferWithVars(ctx, gcResolveLockMaxBackoff, nil)
    98  }
    99  
   100  // NewNoopBackoff create a Backoffer do nothing just return error directly
   101  var NewNoopBackoff = retry.NewNoopBackoff