Skip to content

Commit

Permalink
deduplicate addresses.
Browse files Browse the repository at this point in the history
Signed-off-by: johncming <[email protected]>
  • Loading branch information
johncming committed Dec 13, 2019
1 parent fa04a1a commit 9db89ea
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/discovery/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ func (c *Cache) Update(tgs []*targetgroup.Group) {

// Addresses returns all the addresses from all target groups present in the Cache.
func (c *Cache) Addresses() []string {
var addresses []string
var unique map[string]struct{}

c.Lock()
defer c.Unlock()
var addresses []string

unique = make(map[string]struct{})
for _, group := range c.tgs {
for _, target := range group.Targets {
addresses = append(addresses, string(target[model.AddressLabel]))
addr := string(target[model.AddressLabel])
if _, ok := unique[addr]; ok {
continue
}
addresses = append(addresses, addr)
unique[addr] = struct{}{}
}
}
return addresses
Expand Down

0 comments on commit 9db89ea

Please sign in to comment.