Skip to content

Commit

Permalink
sd/cache: use atomic.Value
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbourgon committed Jun 6, 2016
1 parent 111d8a1 commit 8d6f81f
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions sd/cache/cache.go
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"sort"
"sync"
"sync/atomic"

"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
Expand All @@ -18,7 +19,7 @@ type Cache struct {
mtx sync.RWMutex
factory sd.Factory
cache map[string]endpointCloser
slice []endpoint.Endpoint
slice atomic.Value // []endpoint.Endpoint
logger log.Logger
}

Expand Down Expand Up @@ -84,14 +85,12 @@ func (c *Cache) Update(instances []string) {
}

// Swap and trigger GC for old copies.
c.slice = slice
c.slice.Store(slice)
c.cache = cache
}

// Endpoints yields the current set of (presumably identical) endpoints, ordered
// lexicographically by the corresponding instance string.
func (c *Cache) Endpoints() []endpoint.Endpoint {
c.mtx.RLock()
defer c.mtx.RUnlock()
return c.slice
return c.slice.Load().([]endpoint.Endpoint)
}

0 comments on commit 8d6f81f

Please sign in to comment.