Skip to content

Commit

Permalink
Fixes hystrix timeout (#58)
Browse files Browse the repository at this point in the history
* Fixes hystrix timeout

- Fixes issue where hystrix timeout was being set in nanosecond instead
of millisecond

* Adds test for duration to Int conversion

- Improves implementation of durtationToInt
- Adds test

* Update golint import
  • Loading branch information
ukriish authored and rShetty committed Nov 5, 2018
1 parent fe3abb7 commit d96502f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ALL_PACKAGES=$(shell go list ./... | grep -v "vendor")
setup:
mkdir -p $(GOPATH)/bin
go get -u github.com/golang/dep/cmd/dep
go get -u github.com/golang/lint/golint
go get -u golang.org/x/lint/golint

build-deps:
dep ensure
Expand Down
17 changes: 16 additions & 1 deletion hystrix/hystrix_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
defaultErrorPercentThreshold = 25
defaultSleepWindow = 10
defaultRequestVolumeThreshold = 10

maxUint = ^uint(0)
maxInt = int(maxUint >> 1)
)

var _ heimdall.Client = (*Client)(nil)
Expand Down Expand Up @@ -67,7 +70,7 @@ func NewClient(opts ...Option) *Client {
}

hystrix.ConfigureCommand(client.hystrixCommandName, hystrix.CommandConfig{
Timeout: int(client.hystrixTimeout),
Timeout: durationToInt(client.hystrixTimeout, time.Millisecond),
MaxConcurrentRequests: client.maxConcurrentRequests,
RequestVolumeThreshold: client.requestVolumeThreshold,
SleepWindow: client.sleepWindow,
Expand All @@ -77,6 +80,18 @@ func NewClient(opts ...Option) *Client {
return &client
}

func durationToInt(duration, unit time.Duration) int {
durationAsNumber := duration / unit

if int64(durationAsNumber) > int64(maxInt) {
// Returning max possible value seems like best possible solution here
// the alternative is to panic as there is no way of returning an error
// without changing the NewClient API
return maxInt
}
return int(durationAsNumber)
}

// Get makes a HTTP GET request to provided URL
func (hhc *Client) Get(url string, headers http.Header) (*http.Response, error) {
var response *http.Response
Expand Down
16 changes: 16 additions & 0 deletions hystrix/hystrix_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,19 @@ func respBody(t *testing.T, response *http.Response) string {

return string(respBody)
}

func TestDurationToInt(t *testing.T) {
t.Run("1sec should return 1 when unit is second", func(t *testing.T) {
timeout := 1 * time.Second
timeoutInSec := durationToInt(timeout, time.Second)

assert.Equal(t, 1, timeoutInSec)
})

t.Run("30sec should return 30000 when unit is millisecond", func(t *testing.T) {
timeout := 30 * time.Second
timeoutInMs := durationToInt(timeout, time.Millisecond)

assert.Equal(t, 30000, timeoutInMs)
})
}

0 comments on commit d96502f

Please sign in to comment.