Skip to content

Commit

Permalink
feat: support multiple tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jialeicui committed Jul 23, 2024
1 parent 3fa479d commit 0f55346
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 44 deletions.
6 changes: 3 additions & 3 deletions consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (r *resolvr) Close() {

//go:generate ./bin/moq -out mocks_test.go . servicer
type servicer interface {
Service(string, string, bool, *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error)
ServiceMultipleTags(string, []string, bool, *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error)
}

func watchConsulService(ctx context.Context, s servicer, tgt target, out chan<- []string) {
Expand All @@ -48,9 +48,9 @@ func watchConsulService(ctx context.Context, s servicer, tgt target, out chan<-
go func() {
var lastIndex uint64
for {
ss, meta, err := s.Service(
ss, meta, err := s.ServiceMultipleTags(
tgt.Service,
tgt.Tag,
tgt.Tags,
tgt.Healthy,
&api.QueryOptions{
WaitIndex: lastIndex,
Expand Down
6 changes: 3 additions & 3 deletions consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestWatchConsulService(t *testing.T) {
errorFromService error
want []string
}{
{"simple", target{Service: "svc", Wait: time.Second},
{"simple", target{Service: "svc", Tags: []string{"foo", "bar"}, Wait: time.Second},
[]*api.ServiceEntry{
{
Service: &api.AgentService{Address: "127.0.0.1", Port: 1024},
Expand Down Expand Up @@ -92,9 +92,9 @@ func TestWatchConsulService(t *testing.T) {
}
}()
fconsul := &servicerMock{
ServiceFunc: func(s1, s2 string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) {
ServiceMultipleTagsFunc: func(s1 string, s2 []string, b bool, queryOptions *api.QueryOptions) ([]*api.ServiceEntry, *api.QueryMeta, error) {
require.Equal(t, tt.tgt.Service, s1)
require.Equal(t, tt.tgt.Tag, s2)
require.Equal(t, tt.tgt.Tags, s2)
require.Equal(t, tt.tgt.Healthy, b)
require.Equal(t, tt.tgt.Near, queryOptions.Near)
require.Equal(t, tt.tgt.Wait, queryOptions.WaitTime)
Expand Down
66 changes: 33 additions & 33 deletions mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type target struct {
Wait time.Duration `form:"wait"`
Timeout time.Duration `form:"timeout"`
MaxBackoff time.Duration `form:"max-backoff"`
Tag string `form:"tag"`
Tags []string `form:"tag"`
Near string `form:"near"`
Limit int `form:"limit"`
Healthy bool `form:"healthy"`
Expand All @@ -34,10 +34,11 @@ type target struct {
}

func (t *target) String() string {
return fmt.Sprintf("service='%s' healthy='%t' tag='%s'", t.Service, t.Healthy, t.Tag)
return fmt.Sprintf("service='%s' healthy='%t' tag='%+v'", t.Service, t.Healthy, t.Tags)
}

// parseURL with parameters
// parseURL with parameters
//
// see README.md for the actual format
// URL schema will stay stable in the future for backward compatibility
func parseURL(u string) (target, error) {
Expand Down
4 changes: 2 additions & 2 deletions target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Test_parseURL(t *testing.T) {
},
false,
},
{"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",
{"all-args", "consul://user:[email protected]:8555/my-service?wait=14s&near=host&insecure=true&limit=1&tag=production&tag=extra_tag&token=test_token&max-backoff=2s&dc=xx&allow-stale=true&require-consistent=true",
target{
Addr: "127.0.0.127:8555",
User: "user",
Expand All @@ -34,7 +34,7 @@ func Test_parseURL(t *testing.T) {
Wait: 14 * time.Second,
TLSInsecure: true,
Limit: 1,
Tag: "production",
Tags: []string{"production", "extra_tag"},
Token: "test_token",
MaxBackoff: 2 * time.Second,
Dc: "xx",
Expand Down

0 comments on commit 0f55346

Please sign in to comment.