Skip to content

Commit 713843f

Browse files
authored
[feat] Add WithFallbackCtxFunc hystrix.Option (#129)
1 parent 0b6fc64 commit 713843f

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

hystrix/hystrix_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
type fallbackFunc func(error) error
20+
type fallbackCtxFunc func(context.Context, error) error
2021

2122
// Client is the hystrix client implementation
2223
type Client struct {

hystrix/hystrix_client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ func TestFallBackFunctionIsCalledWithHystrixHTTPClient(t *testing.T) {
508508
WithErrorPercentThreshold(10),
509509
WithSleepWindow(100),
510510
WithRequestVolumeThreshold(10),
511-
WithFallbackFunc(func(err error) error {
511+
WithFallbackCtxFunc(func(ctx context.Context, err error) error {
512512
called = true
513513
return err
514514
}),
@@ -529,7 +529,7 @@ func TestHystrixHTTPClientReturnsFallbackFailureWithAFallBackFunctionWhichReturn
529529
WithErrorPercentThreshold(10),
530530
WithSleepWindow(100),
531531
WithRequestVolumeThreshold(10),
532-
WithFallbackFunc(func(err error) error {
532+
WithFallbackCtxFunc(func(ctx context.Context, err error) error {
533533
// do something in the fallback function
534534
return nil
535535
}),

hystrix/options.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,18 @@ func WithErrorPercentThreshold(errorPercentThreshold int) Option {
6363

6464
// WithFallbackFunc sets the fallback function
6565
func WithFallbackFunc(fn fallbackFunc) Option {
66+
if fn == nil {
67+
return func(_ *Client) {}
68+
}
69+
return WithFallbackCtxFunc(func(_ context.Context, err error) error {
70+
return fn(err)
71+
})
72+
}
73+
74+
// WithFallbackCtxFunc sets the fallback function with context support
75+
func WithFallbackCtxFunc(fn fallbackCtxFunc) Option {
6676
return func(c *Client) {
67-
if fn == nil {
68-
return
69-
}
70-
c.fallbackFunc = func(_ context.Context, err error) error {
71-
return fn(err)
72-
}
77+
c.fallbackFunc = fn
7378
}
7479
}
7580

0 commit comments

Comments
 (0)