From d96502fee6ce808d44580bd44f1a168fa633bd66 Mon Sep 17 00:00:00 2001 From: Unnikrishnan Date: Mon, 5 Nov 2018 09:46:09 +0530 Subject: [PATCH] Fixes hystrix timeout (#58) * 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 --- Makefile | 2 +- hystrix/hystrix_client.go | 17 ++++++++++++++++- hystrix/hystrix_client_test.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 020ae7e..91a83d4 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/hystrix/hystrix_client.go b/hystrix/hystrix_client.go index 2e02b45..102b76d 100644 --- a/hystrix/hystrix_client.go +++ b/hystrix/hystrix_client.go @@ -38,6 +38,9 @@ const ( defaultErrorPercentThreshold = 25 defaultSleepWindow = 10 defaultRequestVolumeThreshold = 10 + + maxUint = ^uint(0) + maxInt = int(maxUint >> 1) ) var _ heimdall.Client = (*Client)(nil) @@ -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, @@ -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 diff --git a/hystrix/hystrix_client_test.go b/hystrix/hystrix_client_test.go index ed77a49..48da18e 100644 --- a/hystrix/hystrix_client_test.go +++ b/hystrix/hystrix_client_test.go @@ -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) + }) +}