Skip to content

Commit 18dcf5a

Browse files
rantavMstislav Bobakov
authored andcommitted
Add support for more consul params. (#10)
dc - to select a DC allow-stale - to allow stale data at the agent require-consistent - to require consistency from the agent
1 parent 5a2eeec commit 18dcf5a

File tree

6 files changed

+53
-53
lines changed

6 files changed

+53
-53
lines changed

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ For full example see [this section](#example)
1515

1616
*Parameters:*
1717

18-
| Name | Format | Description |
19-
|-------------|--------------------------|-------------------------------------------------------------------------------------------------------------------------------|
20-
| tag | string | Select endpoints only with this tag |
21-
| healthy | true/false | Return only endpoints which pass all health-checks. Default: false |
22-
| wait | as in time.ParseDuration | Wait time for watch changes. Due this time period endpoints will be force refreshed. Default: inherits agent property |
23-
| insecure | true/false | Allow insecure communication with Consul. Default: true |
24-
| near | string | Sort endpoints by response duration. Can be efficient combine with `limit` parameter default: "_agent" |
25-
| limit | int | Limit number of endpoints for the service. Default: no limit |
26-
| timeout | as in time.ParseDuration | Http-client timeout. Default: 60s |
27-
| max-backoff | as in time.ParseDuration | Max backoff time for reconnect to consul. Reconnects will start from 10ms to _max-backoff_ exponentialy with factor 2. Default: 1s |
28-
| token | string | Consul token |
18+
| Name | Format | Description |
19+
|--------------------|--------------------------|-------------------------------------------------------------------------------------------------------------------------------|
20+
| tag | string | Select endpoints only with this tag |
21+
| healthy | true/false | Return only endpoints which pass all health-checks. Default: false |
22+
| wait | as in time.ParseDuration | Wait time for watch changes. Due this time period endpoints will be force refreshed. Default: inherits agent property |
23+
| insecure | true/false | Allow insecure communication with Consul. Default: true |
24+
| near | string | Sort endpoints by response duration. Can be efficient combine with `limit` parameter default: "_agent" |
25+
| limit | int | Limit number of endpoints for the service. Default: no limit |
26+
| timeout | as in time.ParseDuration | Http-client timeout. Default: 60s |
27+
| max-backoff | as in time.ParseDuration | Max backoff time for reconnect to consul. Reconnects will start from 10ms to _max-backoff_ exponentialy with factor 2. Default: 1s |
28+
| token | string | Consul token |
29+
| dc | string | Consul datacenter to choose. Optional |
30+
| allow-stale | true/false | Allow stale results from the agent. https://www.consul.io/api/features/consistency.html#stale |
31+
| require-consistent | true/false | RequireConsistent forces the read to be fully consistent. This is more expensive but prevents ever performing a stale read. |
2932

3033
## Example
3134
```go

consul.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ func watchConsulService(ctx context.Context, s servicer, tgt target, out chan<-
5151
tgt.Tag,
5252
tgt.Healthy,
5353
&api.QueryOptions{
54-
WaitIndex: lastIndex,
55-
Near: tgt.Near,
56-
WaitTime: tgt.Wait,
54+
WaitIndex: lastIndex,
55+
Near: tgt.Near,
56+
WaitTime: tgt.Wait,
57+
Datacenter: tgt.Dc,
58+
AllowStale: tgt.AllowStale,
59+
RequireConsistent: tgt.RequireConsistent,
5760
},
5861
)
5962
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/hashicorp/go-cleanhttp v0.5.0 // indirect
77
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90 // indirect
88
github.com/hashicorp/serf v0.8.1 // indirect
9-
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7 // indirect
9+
github.com/jpillora/backoff v0.0.0-20180909062703-3050d21c67d7
1010
github.com/mitchellh/go-homedir v1.0.0 // indirect
1111
github.com/mitchellh/mapstructure v1.1.2 // indirect
1212
github.com/percolate/charlatan v1.0.5 // indirect

go.sum

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
3-
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
43
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54
github.com/go-playground/form v3.1.3+incompatible h1:GY0v2KFh5AYLVffrkgCFcaN7hcFaX8VXMOAGQ+WtpZs=
65
github.com/go-playground/form v3.1.3+incompatible/go.mod h1:lhcKXfTuhRtIZCIKUeJ0b5F207aeQCPbZU09ScKjwWg=
76
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
87
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
98
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
10-
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
119
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1210
github.com/hashicorp/consul v1.2.3 h1:ekX+fXQ7NYzD2quCCgmDekCCIp0Fsi1NE0ViC2CJm+8=
1311
github.com/hashicorp/consul v1.2.3/go.mod h1:mFrjN1mfidgJfYP1xrJCF+AfRhr6Eaqhb2+sfyn/OOI=
1412
github.com/hashicorp/go-cleanhttp v0.5.0 h1:wvCrVc9TjDls6+YGAF2hAifE1E5U1+b4tH6KdvN3Gig=
1513
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
16-
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90 h1:9HVkPxOpo+yO93Ah4yrO67d/qh0fbLLWbKqhYjyHq9A=
14+
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90 h1:VBj0QYQ0u2MCJzBfeYXGexnAl17GsH1yidnoxCqqD9E=
1715
github.com/hashicorp/go-rootcerts v0.0.0-20160503143440-6bb64b370b90/go.mod h1:o4zcYY1e0GEZI6eSEr+43QDYmuGglw1qSO6qdHUHCgg=
1816
github.com/hashicorp/serf v0.8.1 h1:mYs6SMzu72+90OcPa5wr3nfznA4Dw9UyR791ZFNOIf4=
1917
github.com/hashicorp/serf v0.8.1/go.mod h1:h/Ru6tmZazX7WO/GDmwdpS975F019L4t5ng5IgwbNrE=
@@ -25,32 +23,22 @@ github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnG
2523
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
2624
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
2725
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
28-
github.com/percolate/charlatan v1.0.5 h1:oXiGDWNM/9uHYm0X3upWLevqODAy49vmrKAFya2msog=
2926
github.com/percolate/charlatan v1.0.5/go.mod h1:rbsO+LTKjgLd+sGYdTryJHLJZ2f2fbLhpH3+IfsacWk=
3027
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
3128
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
32-
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3329
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34-
github.com/sirupsen/logrus v1.1.1 h1:VzGj7lhU7KEB9e9gMpAV/v5XT2NVSvLJhJLCWbnkgXg=
3530
github.com/sirupsen/logrus v1.1.1/go.mod h1:zrgwTnHtNr00buQ1vSptGe8m1f/BbgsPukg8qsT7A+A=
36-
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
3731
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
38-
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
3932
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
4033
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
41-
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I=
4234
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
4335
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
4436
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4537
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
46-
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
4738
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
48-
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
4939
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
50-
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52 h1:JG/0uqcGdTNgq7FdU+61l5Pdmb8putNZlXb65bJBROs=
5140
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
5241
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
53-
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
5442
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
5543
google.golang.org/grpc v1.2.1-0.20181011201045-c05280cc73cd h1:v9jzEUwtfbZGqmG5Vx0jvJ33997QiWwOIr3pvZgSGRU=
5644
google.golang.org/grpc v1.2.1-0.20181011201045-c05280cc73cd/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=

target.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ import (
1313
)
1414

1515
type target struct {
16-
Addr string `form:"-"`
17-
User string `form:"-"`
18-
Password string `form:"-"`
19-
Service string `form:"-"`
20-
Wait time.Duration `form:"wait"`
21-
Timeout time.Duration `form:"timeout"`
22-
MaxBackoff time.Duration `form:"max-backoff"`
23-
Tag string `form:"tag"`
24-
Near string `form:"near"`
25-
Limit int `form:"limit"`
26-
Healthy bool `form:"healthy"`
27-
TLSInsecure bool `form:"insecure"`
28-
Token string `form:"token"`
16+
Addr string `form:"-"`
17+
User string `form:"-"`
18+
Password string `form:"-"`
19+
Service string `form:"-"`
20+
Wait time.Duration `form:"wait"`
21+
Timeout time.Duration `form:"timeout"`
22+
MaxBackoff time.Duration `form:"max-backoff"`
23+
Tag string `form:"tag"`
24+
Near string `form:"near"`
25+
Limit int `form:"limit"`
26+
Healthy bool `form:"healthy"`
27+
TLSInsecure bool `form:"insecure"`
28+
Token string `form:"token"`
29+
Dc string `form:"dc"`
30+
AllowStale bool `form:"allow-stale"`
31+
RequireConsistent bool `form:"require-consistent"`
2932
// TODO(mbobakov): custom parameters for the http-transport
3033
// TODO(mbobakov): custom parameters for the TLS subsystem
3134
}

target_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ func Test_parseURL(t *testing.T) {
2424
},
2525
false,
2626
},
27-
{"all-args", "consul://user:[email protected]:8555/my-service?wait=14s&near=host&insecure=true&limit=1&tag=production&token=test_token&max-backoff=2s",
27+
{"all-args", "consul://user:[email protected]:8555/my-service?wait=14s&near=host&insecure=true&limit=1&tag=production&token=test_token&max-backoff=2s&dc=xx&allow-stale=true&require-consistent=true",
2828
target{
29-
Addr: "127.0.0.127:8555",
30-
User: "user",
31-
Password: "password",
32-
Service: "my-service",
33-
Near: "host",
34-
Wait: 14 * time.Second,
35-
TLSInsecure: true,
36-
Limit: 1,
37-
Tag: "production",
38-
Token: "test_token",
39-
MaxBackoff: 2 * time.Second,
29+
Addr: "127.0.0.127:8555",
30+
User: "user",
31+
Password: "password",
32+
Service: "my-service",
33+
Near: "host",
34+
Wait: 14 * time.Second,
35+
TLSInsecure: true,
36+
Limit: 1,
37+
Tag: "production",
38+
Token: "test_token",
39+
MaxBackoff: 2 * time.Second,
40+
Dc: "xx",
41+
AllowStale: true,
42+
RequireConsistent: true,
4043
},
4144
false,
4245
},

0 commit comments

Comments
 (0)