Skip to content

Commit

Permalink
Don't return zero length services
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jan 16, 2019
1 parent 784a89b commit cc5629f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion selector/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ var (
DefaultTTL = time.Minute
)

// isValid checks if the service is valid
func (c *registrySelector) isValid(services []*registry.Service, ttl time.Time) bool {
// no services exist
if len(services) == 0 {
return false
}

// ttl is invalid
if ttl.IsZero() {
return false
}

// time since ttl is longer than timeout
if time.Since(ttl) > c.ttl {
return false
}

// ok
return true
}

func (c *registrySelector) quit() bool {
select {
case <-c.exit:
Expand Down Expand Up @@ -88,7 +109,7 @@ func (c *registrySelector) get(service string) ([]*registry.Service, error) {
ttl, kk := c.ttls[service]

// got services && within ttl so return cache
if ok && kk && time.Since(ttl) < c.ttl {
if ok && kk && c.isValid(services, ttl) {
// make a copy
cp := c.cp(services)
// unlock the read
Expand Down

0 comments on commit cc5629f

Please sign in to comment.