sd/cache: use atomic.Value
Peter Bourgon
7 years ago
3 | 3 | "io" |
4 | 4 | "sort" |
5 | 5 | "sync" |
6 | "sync/atomic" | |
6 | 7 | |
7 | 8 | "github.com/go-kit/kit/endpoint" |
8 | 9 | "github.com/go-kit/kit/log" |
17 | 18 | mtx sync.RWMutex |
18 | 19 | factory sd.Factory |
19 | 20 | cache map[string]endpointCloser |
20 | slice []endpoint.Endpoint | |
21 | slice atomic.Value // []endpoint.Endpoint | |
21 | 22 | logger log.Logger |
22 | 23 | } |
23 | 24 | |
83 | 84 | } |
84 | 85 | |
85 | 86 | // Swap and trigger GC for old copies. |
86 | c.slice = slice | |
87 | c.slice.Store(slice) | |
87 | 88 | c.cache = cache |
88 | 89 | } |
89 | 90 | |
90 | 91 | // Endpoints yields the current set of (presumably identical) endpoints, ordered |
91 | 92 | // lexicographically by the corresponding instance string. |
92 | 93 | 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) | |
96 | 95 | } |