From b129bc8184dcd2eb829fa017703f055458abdc71 Mon Sep 17 00:00:00 2001 From: chuckyz Date: Thu, 30 Jan 2020 09:24:51 -0800 Subject: [PATCH 1/5] allow consul stale --- controller/consul.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/controller/consul.go b/controller/consul.go index 85a277f..ac54e50 100644 --- a/controller/consul.go +++ b/controller/consul.go @@ -13,6 +13,7 @@ import ( const ( consulNodeEnv = "CONSUL_NODE" + allowStale = "CONSUL_STALE" matchTag = "enable_gocast" nodeURL = "/catalog/node" remoteHealthCheckurl = "/health/checks" @@ -52,7 +53,11 @@ func NewConsulMon(addr string) (*ConsulMon, error) { func (c *ConsulMon) queryServices() ([]*App, error) { var apps []*App - addr := c.addr + fmt.Sprintf("%s/%s", nodeURL, c.node) + stale := "" + if os.Getenv(allowStale) { + stale = "?stale" + } + addr := c.addr + fmt.Sprintf("%s/%s%s", nodeURL, c.node, stale) resp, err := c.client.Get(addr) if err != nil { return apps, err From 52a05e0317663613a48ce43a46642172dfc0f26d Mon Sep 17 00:00:00 2001 From: chuckyz Date: Thu, 30 Jan 2020 09:26:32 -0800 Subject: [PATCH 2/5] os.Getenv returns a string --- controller/consul.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/consul.go b/controller/consul.go index ac54e50..3d23cff 100644 --- a/controller/consul.go +++ b/controller/consul.go @@ -54,7 +54,7 @@ func NewConsulMon(addr string) (*ConsulMon, error) { func (c *ConsulMon) queryServices() ([]*App, error) { var apps []*App stale := "" - if os.Getenv(allowStale) { + if os.Getenv(allowStale) == "true" { stale = "?stale" } addr := c.addr + fmt.Sprintf("%s/%s%s", nodeURL, c.node, stale) From 1001600a68fa769b3431fd95556fe36d4b64cae3 Mon Sep 17 00:00:00 2001 From: chuckyz Date: Thu, 30 Jan 2020 09:42:57 -0800 Subject: [PATCH 3/5] use url.Values and .Encode --- controller/consul.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controller/consul.go b/controller/consul.go index 3d23cff..84c3617 100644 --- a/controller/consul.go +++ b/controller/consul.go @@ -53,11 +53,11 @@ func NewConsulMon(addr string) (*ConsulMon, error) { func (c *ConsulMon) queryServices() ([]*App, error) { var apps []*App - stale := "" + params := url.Values{} if os.Getenv(allowStale) == "true" { - stale = "?stale" + params.Add("stale") } - addr := c.addr + fmt.Sprintf("%s/%s%s", nodeURL, c.node, stale) + addr := c.addr + fmt.Sprintf("%s/%s?%s", nodeURL, c.node, params.Encode()) resp, err := c.client.Get(addr) if err != nil { return apps, err From 6e0002841060f980c10fb87835249f55a9a30560 Mon Sep 17 00:00:00 2001 From: chuckyz Date: Thu, 30 Jan 2020 10:20:44 -0800 Subject: [PATCH 4/5] revert using url.Values as url.Add wont work --- controller/consul.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/controller/consul.go b/controller/consul.go index 84c3617..5208e94 100644 --- a/controller/consul.go +++ b/controller/consul.go @@ -53,11 +53,11 @@ func NewConsulMon(addr string) (*ConsulMon, error) { func (c *ConsulMon) queryServices() ([]*App, error) { var apps []*App - params := url.Values{} + stale := "" if os.Getenv(allowStale) == "true" { - params.Add("stale") + stale = "stale" } - addr := c.addr + fmt.Sprintf("%s/%s?%s", nodeURL, c.node, params.Encode()) + addr := c.addr + fmt.Sprintf("%s/%s?%s", nodeURL, c.node, stale) resp, err := c.client.Get(addr) if err != nil { return apps, err From 4a9df038fba7248a3bab02ae6de675a762697eb2 Mon Sep 17 00:00:00 2001 From: chuckyz Date: Thu, 30 Jan 2020 13:23:13 -0800 Subject: [PATCH 5/5] switch from implicit to explicit string --- controller/consul.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/consul.go b/controller/consul.go index 5208e94..ac9749f 100644 --- a/controller/consul.go +++ b/controller/consul.go @@ -53,7 +53,7 @@ func NewConsulMon(addr string) (*ConsulMon, error) { func (c *ConsulMon) queryServices() ([]*App, error) { var apps []*App - stale := "" + var stale string if os.Getenv(allowStale) == "true" { stale = "stale" }