From c048c5aca60ed901de623f8bc45442198a9c8eb8 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Wed, 6 Jan 2016 11:05:11 -0800 Subject: [PATCH] Makes the API behave better with small wait values. --- api/api.go | 11 +++++++++-- api/api_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/api/api.go b/api/api.go index ad89dc50e6..c5745ab6a4 100644 --- a/api/api.go +++ b/api/api.go @@ -261,9 +261,16 @@ func (r *request) setQueryOptions(q *QueryOptions) { } } -// durToMsec converts a duration to a millisecond specified string +// durToMsec converts a duration to a millisecond specified string. If the +// user selected a positive value that rounds to 0 ms, then we will use 1 ms +// so they get a short delay, otherwise Consul will translate the 0 ms into +// a huge default delay. func durToMsec(dur time.Duration) string { - return fmt.Sprintf("%dms", dur/time.Millisecond) + ms := dur / time.Millisecond + if dur > 0 && ms == 0 { + ms = 1 + } + return fmt.Sprintf("%dms", ms) } // setWriteOptions is used to annotate the request with diff --git a/api/api_test.go b/api/api_test.go index 314a89b146..4d60859f9c 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -254,3 +254,21 @@ func TestAPI_UnixSocket(t *testing.T) { t.Fatalf("bad: %v", info) } } + +func TestAPI_durToMsec(t *testing.T) { + if ms := durToMsec(0); ms != "0ms" { + t.Fatalf("bad: %s", ms) + } + + if ms := durToMsec(time.Millisecond); ms != "1ms" { + t.Fatalf("bad: %s", ms) + } + + if ms := durToMsec(time.Microsecond); ms != "1ms" { + t.Fatalf("bad: %s", ms) + } + + if ms := durToMsec(5 * time.Millisecond); ms != "5ms" { + t.Fatalf("bad: %s", ms) + } +}