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

[feat] fallback func with context #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 4 additions & 3 deletions hystrix/hystrix_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hystrix

import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
Expand All @@ -15,7 +16,7 @@ import (
"github.com/pkg/errors"
)

type fallbackFunc func(error) error
type fallbackFunc func(context.Context, error) error

// Client is the hystrix client implementation
type Client struct {
Expand All @@ -30,7 +31,7 @@ type Client struct {
errorPercentThreshold int
retryCount int
retrier heimdall.Retriable
fallbackFunc func(err error) error
fallbackFunc func(ctx context.Context, err error) error
statsD *plugins.StatsdCollectorConfig
}

Expand Down Expand Up @@ -186,7 +187,7 @@ func (hhc *Client) Do(request *http.Request) (*http.Response, error) {
response.Body.Close()
}

err = hystrix.Do(hhc.hystrixCommandName, func() error {
err = hystrix.DoC(request.Context(), hhc.hystrixCommandName, func(ctx context.Context) error {
response, err = hhc.client.Do(request)
if bodyReader != nil {
// Reset the body reader after the request since at this point it's already read
Expand Down
7 changes: 4 additions & 3 deletions hystrix/hystrix_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hystrix

import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -430,7 +431,7 @@ func TestHystrixHTTPClientReturnsFallbackFailureWithAFallBackFunctionWhichReturn
WithErrorPercentThreshold(10),
WithSleepWindow(100),
WithRequestVolumeThreshold(10),
WithFallbackFunc(func(err error) error {
WithFallbackFunc(func(ctx context.Context, err error) error {
// do something in the fallback function
return err
}),
Expand All @@ -453,7 +454,7 @@ func TestFallBackFunctionIsCalledWithHystrixHTTPClient(t *testing.T) {
WithErrorPercentThreshold(10),
WithSleepWindow(100),
WithRequestVolumeThreshold(10),
WithFallbackFunc(func(err error) error {
WithFallbackFunc(func(ctx context.Context, err error) error {
called = true
return err
}),
Expand All @@ -474,7 +475,7 @@ func TestHystrixHTTPClientReturnsFallbackFailureWithAFallBackFunctionWhichReturn
WithErrorPercentThreshold(10),
WithSleepWindow(100),
WithRequestVolumeThreshold(10),
WithFallbackFunc(func(err error) error {
WithFallbackFunc(func(ctx context.Context, err error) error {
// do something in the fallback function
return nil
}),
Expand Down