github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/circuitbreaker/tripfunc.go (about)

     1  // Copyright 2021 ByteDance Inc.
     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  package circuitbreaker
    16  
    17  import "time"
    18  
    19  // TripFunc is a function called by a breaker when error appear and
    20  // determines whether the breaker should trip.
    21  type TripFunc func(Metricer) bool
    22  
    23  // TripFuncWithKey returns a TripFunc according to the key.
    24  type TripFuncWithKey func(string) TripFunc
    25  
    26  // ThresholdTripFunc .
    27  func ThresholdTripFunc(threshold int64) TripFunc {
    28  	return func(m Metricer) bool {
    29  		return m.Failures()+m.Timeouts() >= threshold
    30  	}
    31  }
    32  
    33  // ConsecutiveTripFunc .
    34  func ConsecutiveTripFunc(threshold int64) TripFunc {
    35  	return func(m Metricer) bool {
    36  		return m.ConseErrors() >= threshold
    37  	}
    38  }
    39  
    40  // RateTripFunc .
    41  func RateTripFunc(rate float64, minSamples int64) TripFunc {
    42  	return func(m Metricer) bool {
    43  		samples := m.Samples()
    44  		return samples >= minSamples && m.ErrorRate() >= rate
    45  	}
    46  }
    47  
    48  // ConsecutiveTripFuncV2 uses the following three strategies based on the parameters passed in.
    49  // 1. when the number of samples >= minSamples and the error rate >= rate
    50  // 2. when the number of samples >= durationSamples and the length of consecutive errors >= duration
    51  // 3. When the number of consecutive errors >= conseErrors
    52  // The fuse is opened when any of the above three strategies holds.
    53  func ConsecutiveTripFuncV2(rate float64, minSamples int64, duration time.Duration, durationSamples, conseErrors int64) TripFunc {
    54  	return func(m Metricer) bool {
    55  		samples := m.Samples()
    56  		// based on stat
    57  		if samples >= minSamples && m.ErrorRate() >= rate {
    58  			return true
    59  		}
    60  		// based on continuous time
    61  		if duration > 0 && m.ConseErrors() >= durationSamples && m.ConseTime() >= duration {
    62  			return true
    63  		}
    64  		// base on consecutive errors
    65  		if conseErrors > 0 && m.ConseErrors() >= conseErrors {
    66  			return true
    67  		}
    68  		return false
    69  	}
    70  }