Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ftr: Hystrix filter for circuit break and service downgrade #133

Merged
merged 38 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a828482
hystrix filter added
Jul 17, 2019
431eeb9
Merge pull request #1 from apache/master
YGrylls Jul 17, 2019
b485e8d
config for hystrix filter added
YGrylls Jul 17, 2019
dd05253
fmt the code
YGrylls Jul 17, 2019
0781ce5
delete unused constants
YGrylls Jul 17, 2019
c089a43
change panic in confit initializaiton to error
YGrylls Jul 17, 2019
d2a61dc
fallback config added, see examples/hystrix_example
YGrylls Jul 18, 2019
0ee30d0
Changes in fallback function, now it receives 4 parameters:
YGrylls Jul 18, 2019
ef3b42c
Change comments
YGrylls Jul 18, 2019
7f579b1
Add nil check for fallback func return
YGrylls Jul 18, 2019
08dd542
wrong spell
YGrylls Jul 18, 2019
75aba16
Make the fallback func an interface
YGrylls Jul 18, 2019
79ab52c
Unit tests: invoke and refresh to be done
YGrylls Jul 18, 2019
b7e9c61
Unit tests for hystrix filter done
YGrylls Jul 22, 2019
8e647d4
Merge pull request #2 from apache/master
YGrylls Jul 22, 2019
508d9a0
Update from master
YGrylls Jul 22, 2019
ca9793d
Examples change
YGrylls Jul 22, 2019
08b064a
Merge branch 'develop' into hystrix_filter
YGrylls Jul 22, 2019
b6a964b
Fix example
YGrylls Jul 22, 2019
faa779c
Fix the probelm occurs when filter_conf is nil
YGrylls Jul 22, 2019
3e7042d
Unit test fix
YGrylls Jul 22, 2019
f604d42
fix
YGrylls Jul 22, 2019
40689de
Fix import cycle in protocol_filter_wrapper_test
YGrylls Jul 22, 2019
c50bd5a
fmt
YGrylls Jul 22, 2019
4b52478
Standardize the import format, add lisences infomation for new file
YGrylls Jul 23, 2019
67bae9a
Overhaul: Fix concurrency problems; Add support for server end; Add e…
YGrylls Jul 24, 2019
588a5a8
Example Added for client and fallback
YGrylls Jul 25, 2019
b954e6a
Merge branch 'develop' into hystrix_filter
YGrylls Jul 25, 2019
3c4722a
Add some unit tests
YGrylls Jul 25, 2019
a939b43
fix unit tests
YGrylls Jul 25, 2019
83aad3f
fix unit tests
YGrylls Jul 25, 2019
aedf81b
Add license info for example
YGrylls Jul 26, 2019
515c472
Merge apache/develop into ygrylls/hystrix_filter
YGrylls Jul 31, 2019
4fe6fa3
Change hystrix example directory and modified to follow other new exa…
YGrylls Jul 31, 2019
7e88874
Remove old hessian in go.mod
YGrylls Jul 31, 2019
207fb39
Merge and fix the problems found in review
YGrylls Aug 12, 2019
67bc50d
Merge remote-tracking branch 'upstream/develop' into hystrix_filter
YGrylls Aug 25, 2019
10103db
Merge branch 'develop' into hystrix_filter
YGrylls Aug 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the fallback func an interface
  • Loading branch information
YGrylls committed Jul 18, 2019
commit 75aba16dc2a423b32b1553800ebba25fc3e14fd3
9 changes: 7 additions & 2 deletions examples/hystrix_example/example_hystrix_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import "github.com/afex/hystrix-go/hystrix"

const EXAMPLE_FALLBACK_NAME = "exampleFallback"


type ExampleHystrixFallback struct {
}

//Example for hystrix fallback function
//The 4 parameters:
//The error that caused the fallback;
//The invoker that the filter receives;
//The invocation that should be invoked;
//The copy of circuit breaker for this invocation, for getting its status
func ExampleHystrixFallback(err error, invoker protocol.Invoker, invocation protocol.Invocation, cb hystrix.CircuitBreaker) protocol.Result {
func(f *ExampleHystrixFallback) FallbackFunc (err error, invoker protocol.Invoker, invocation protocol.Invocation, cb hystrix.CircuitBreaker) protocol.Result {
result := &protocol.RPCResult{}
if cb.IsOpen() {
result.SetError(nil)
Expand All @@ -25,8 +29,9 @@ func ExampleHystrixFallback(err error, invoker protocol.Invoker, invocation prot
return result
}


//Add the fallback function to the map
//The name MUST be the same as in your config file
func init() {
impl.SetHystrixFallback(EXAMPLE_FALLBACK_NAME, ExampleHystrixFallback)
impl.SetHystrixFallback(EXAMPLE_FALLBACK_NAME, &ExampleHystrixFallback{})
}
6 changes: 4 additions & 2 deletions filter/impl/default_hystrix_fallback.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"github.com/apache/dubbo-go/protocol"
)

func defaultHystrixFallback(err error, invoker protocol.Invoker, invocation protocol.Invocation, cb hystrix.CircuitBreaker) protocol.Result {
type DefaultHystrixFallback struct {
}
func(d *DefaultHystrixFallback) FallbackFunc(err error, invoker protocol.Invoker, invocation protocol.Invocation, cb hystrix.CircuitBreaker) protocol.Result{
//By default, return nil value and the error occurred
res := &protocol.RPCResult{}
res.SetError(err)
return res
}
}
32 changes: 18 additions & 14 deletions filter/impl/hystrix_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import (
const (
HYSTRIX = "hystrix"
)
type HystrixFallback interface {
FallbackFunc(err error, invoker protocol.Invoker, invocation protocol.Invocation, cb hystrix.CircuitBreaker) protocol.Result
}



type CallBackFunction func(err error, invoker protocol.Invoker, invocation protocol.Invocation, cb hystrix.CircuitBreaker) protocol.Result

var (
isConfigLoaded = false
fallback = make(map[string]CallBackFunction)
fallback = make(map[string]HystrixFallback)
conf = &HystrixFilterConfig{}
//Timeout
//MaxConcurrentRequests
Expand All @@ -34,7 +38,7 @@ func init() {
}

type HystrixFilter struct {
fallbackFunc CallBackFunction
fallback HystrixFallback
}

func (hf *HystrixFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
Expand All @@ -49,7 +53,7 @@ func (hf *HystrixFilter) Invoke(invoker protocol.Invoker, invocation protocol.In

// Do the configuration if the circuit breaker is created for the first time

if ifNew || hf.fallbackFunc == nil {
if ifNew || hf.fallback == nil {
filterConf := getConfig(invoker.GetUrl().Service(), invocation.MethodName())
if ifNew {
hystrix.ConfigureCommand(cmdName, hystrix.CommandConfig{
Expand All @@ -60,8 +64,8 @@ func (hf *HystrixFilter) Invoke(invoker protocol.Invoker, invocation protocol.In
RequestVolumeThreshold: filterConf.RequestVolumeThreshold,
})
}
if hf.fallbackFunc == nil {
hf.fallbackFunc = getHystrixFallback(filterConf.Fallback)
if hf.fallback == nil {
hf.fallback = getHystrixFallback(filterConf.Fallback)
}
}

Expand All @@ -73,7 +77,7 @@ func (hf *HystrixFilter) Invoke(invoker protocol.Invoker, invocation protocol.In
}, func(err error) error {
//failure logic
logger.Debugf("[Hystrix Filter]Invoke failed, circuit breaker open: %v", cb.IsOpen())
result = hf.fallbackFunc(err, invoker, invocation, *cb)
result = hf.fallback.FallbackFunc(err, invoker, invocation, *cb)

//If user try to return nil in the customized fallback func, it will cause panic
//So check here
Expand Down Expand Up @@ -151,17 +155,17 @@ func RefreshHystrix() error {
return initHystrixConfig()
}

func SetHystrixFallback(name string, fallbackFunc CallBackFunction) {
fallback[name] = fallbackFunc
func SetHystrixFallback(name string, fallbackImpl HystrixFallback) {
fallback[name] = fallbackImpl
}

func getHystrixFallback(name string) CallBackFunction {
fallbackFunc := fallback[name]
if fallbackFunc == nil {
func getHystrixFallback(name string) HystrixFallback {
fallbackImpl := fallback[name]
if fallbackImpl == nil {
logger.Warnf("[Hystrix Filter]Fallback func not found: %s", name)
fallbackFunc = defaultHystrixFallback
fallbackImpl =& DefaultHystrixFallback{}
}
return fallbackFunc
return fallbackImpl
}

type CommandConfigWithFallback struct {
Expand Down