github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/cloud/circuitbreaker/interface.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 // Panel is what users should use. 20 type Panel interface { 21 Succeed(key string) 22 Fail(key string) 23 FailWithTrip(key string, f TripFunc) 24 Timeout(key string) 25 TimeoutWithTrip(key string, f TripFunc) 26 IsAllowed(key string) bool 27 RemoveBreaker(key string) 28 DumpBreakers() map[string]Breaker 29 // Close should be called if Panel is not used anymore. Or may lead to resource leak. 30 // If Panel is used after Close is called, behavior is undefined. 31 Close() 32 GetMetricer(key string) Metricer 33 } 34 35 // Breaker is the base of a circuit breaker. 36 type Breaker interface { 37 Succeed() 38 Fail() 39 FailWithTrip(TripFunc) 40 Timeout() 41 TimeoutWithTrip(TripFunc) 42 IsAllowed() bool 43 State() State 44 Metricer() Metricer 45 Reset() 46 } 47 48 // Metricer metrics errors, timeouts and successes 49 type Metricer interface { 50 Failures() int64 // return the number of failures 51 Successes() int64 // return the number of successes 52 Timeouts() int64 // return the number of timeouts 53 ConseErrors() int64 // return the consecutive errors recently 54 ConseTime() time.Duration // return the consecutive error time 55 ErrorRate() float64 // rate = (timeouts + failures) / (timeouts + failures + successes) 56 Samples() int64 // (timeouts + failures + successes) 57 Counts() (successes, failures, timeouts int64) 58 } 59 60 // mutable Metricer 61 type metricer interface { 62 Metricer 63 64 Fail() // records a failure 65 Succeed() // records a success 66 Timeout() // records a timeout 67 68 Reset() 69 tick() 70 }