github.com/cloudwego/kitex@v0.9.0/pkg/circuitbreak/default.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 circuitbreak 18 19 import ( 20 "context" 21 "errors" 22 23 "github.com/cloudwego/kitex/pkg/kerrors" 24 ) 25 26 // some types of error won't trigger circuit breaker 27 var ignoreErrTypes = map[error]ErrorType{ 28 kerrors.ErrInternalException: TypeIgnorable, 29 kerrors.ErrServiceDiscovery: TypeIgnorable, 30 kerrors.ErrACL: TypeIgnorable, 31 kerrors.ErrLoadbalance: TypeIgnorable, 32 kerrors.ErrRPCFinish: TypeIgnorable, 33 } 34 35 // ErrorTypeOnServiceLevel determines the error type with a service level criteria. 36 func ErrorTypeOnServiceLevel(ctx context.Context, request, response interface{}, err error) ErrorType { 37 if err != nil { 38 for e, t := range ignoreErrTypes { 39 if errors.Is(err, e) { 40 return t 41 } 42 } 43 var we *errorWrapperWithType 44 if ok := errors.As(err, &we); ok { 45 return we.errType 46 } 47 if kerrors.IsTimeoutError(err) { 48 return TypeTimeout 49 } 50 return TypeFailure 51 } 52 return TypeSuccess 53 } 54 55 // ErrorTypeOnInstanceLevel determines the error type with an instance level criteria. 56 // Basically, it treats only the connection error as failure. 57 func ErrorTypeOnInstanceLevel(ctx context.Context, request, response interface{}, err error) ErrorType { 58 if errors.Is(err, kerrors.ErrGetConnection) { 59 return TypeFailure 60 } 61 return TypeSuccess 62 } 63 64 // FailIfError return TypeFailure if err is not nil, otherwise TypeSuccess. 65 func FailIfError(ctx context.Context, request, response interface{}, err error) ErrorType { 66 if err != nil { 67 return TypeFailure 68 } 69 return TypeSuccess 70 } 71 72 // NoDecoration returns the original err. 73 func NoDecoration(ctx context.Context, request interface{}, err error) error { 74 return err 75 }