Skip to content

Commit

Permalink
pull out repeated selection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansb committed Jun 19, 2014
1 parent 5e89695 commit f67c167
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (e *EurekaConnection) SelectServiceURL() string {
if e.DNSDiscovery && len(e.discoveryTtl) == 0 {
servers, ttl, err := discoverDNS(e.DiscoveryZone, e.ServicePort)
if err != nil {
return e.ServiceUrls[rand.Int()%len(e.ServiceUrls)]
return choice(e.ServiceUrls)
}
e.discoveryTtl <- struct{}{}
time.AfterFunc(ttl, func() {
Expand All @@ -31,7 +31,11 @@ func (e *EurekaConnection) SelectServiceURL() string {
})
e.ServiceUrls = servers
}
return e.ServiceUrls[rand.Int()%len(e.ServiceUrls)]
return choice(e.ServiceUrls)
}

func choice(options []string) string {
return options[rand.Int()%len(options)]
}

// NewConnFromConfigFile sets up a connection object based on a config in
Expand Down

0 comments on commit f67c167

Please sign in to comment.