Codebase list golang-github-go-kit-kit / 8d6f81f
sd/cache: use atomic.Value Peter Bourgon 7 years ago
1 changed file(s) with 4 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
33 "io"
44 "sort"
55 "sync"
6 "sync/atomic"
67
78 "github.com/go-kit/kit/endpoint"
89 "github.com/go-kit/kit/log"
1718 mtx sync.RWMutex
1819 factory sd.Factory
1920 cache map[string]endpointCloser
20 slice []endpoint.Endpoint
21 slice atomic.Value // []endpoint.Endpoint
2122 logger log.Logger
2223 }
2324
8384 }
8485
8586 // Swap and trigger GC for old copies.
86 c.slice = slice
87 c.slice.Store(slice)
8788 c.cache = cache
8889 }
8990
9091 // Endpoints yields the current set of (presumably identical) endpoints, ordered
9192 // lexicographically by the corresponding instance string.
9293 func (c *Cache) Endpoints() []endpoint.Endpoint {
93 c.mtx.RLock()
94 defer c.mtx.RUnlock()
95 return c.slice
94 return c.slice.Load().([]endpoint.Endpoint)
9695 }