github.com/cloudwego/kitex@v0.9.0/pkg/rpctimeout/rpctimeout.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 rpctimeout implements logic for timeout controlling.
    18  package rpctimeout
    19  
    20  import (
    21  	"context"
    22  	"sync/atomic"
    23  	"time"
    24  
    25  	"github.com/cloudwego/kitex/pkg/endpoint"
    26  )
    27  
    28  type timeoutAdjustKeyType int
    29  
    30  // TimeoutAdjustKey is used to adjust the timeout of RPC timeout middleware.
    31  // Deprecated: this value is kept for historical reason and compatibility.
    32  // It should not be used anymore.
    33  const TimeoutAdjustKey timeoutAdjustKeyType = 1
    34  
    35  // MiddlewareBuilder builds timeout middleware.
    36  // Deprecated: this method is kept for historical reason and compatibility.
    37  // It should not be used anymore.
    38  func MiddlewareBuilder(moreTimeout time.Duration) endpoint.MiddlewareBuilder {
    39  	return func(mwCtx context.Context) endpoint.Middleware {
    40  		if p, ok := mwCtx.Value(TimeoutAdjustKey).(*time.Duration); ok {
    41  			*p = moreTimeout
    42  		}
    43  		return endpoint.DummyMiddleware
    44  	}
    45  }
    46  
    47  // Controls whether `rpcTimeoutMW` should separate ErrRPCTimeout into different errors according to causes.
    48  // Since atomic.Bool is available only since go1.19, use int32 instead.
    49  var globalNeedFineGrainedErrCode int32 = 0
    50  
    51  // EnableGlobalNeedFineGrainedErrCode can be used to set global flag, which applies to all clients.
    52  func EnableGlobalNeedFineGrainedErrCode() {
    53  	atomic.StoreInt32(&globalNeedFineGrainedErrCode, 1)
    54  }
    55  
    56  // DisableGlobalNeedFineGrainedErrCode can be used to revert the flag, which is useful in tests.
    57  func DisableGlobalNeedFineGrainedErrCode() {
    58  	atomic.StoreInt32(&globalNeedFineGrainedErrCode, 0)
    59  }
    60  
    61  // LoadGlobalNeedFineGrainedErrCode is used to load the flag, and return a bool value.
    62  func LoadGlobalNeedFineGrainedErrCode() bool {
    63  	return atomic.LoadInt32(&globalNeedFineGrainedErrCode) == 1
    64  }
    65  
    66  // defaultBusinessTimeoutThreshold is used to determine whether a timeout is set by kitex or business.
    67  // If actual DDL + threshold <  Kitex's expected DDL, it's more likely to be set by business code.
    68  var defaultBusinessTimeoutThreshold = time.Millisecond * 50
    69  
    70  // SetBusinessTimeoutThreshold sets the threshold for business timeout.
    71  func SetBusinessTimeoutThreshold(t time.Duration) {
    72  	defaultBusinessTimeoutThreshold = t
    73  }
    74  
    75  // LoadBusinessTimeoutThreshold is used the load the threshold, and keeps compatibility if in the future
    76  // there's need for business code to modify the value.
    77  func LoadBusinessTimeoutThreshold() time.Duration {
    78  	return defaultBusinessTimeoutThreshold
    79  }