MCPcopy
hub / github.com/grafana/dskit / setNewPrimaryClient

Method setNewPrimaryClient

kv/multi.go:154–184  ·  view source on GitHub ↗

returns true, if primary client has changed

(store string)

Source from the content-addressed store, hash-verified

152
153// returns true, if primary client has changed
154func (m *MultiClient) setNewPrimaryClient(store string) (bool, error) {
155 newPrimaryIx := -1
156 for ix, c := range m.clients {
157 if c.name == store {
158 newPrimaryIx = ix
159 break
160 }
161 }
162
163 if newPrimaryIx < 0 {
164 return false, fmt.Errorf("KV store not found")
165 }
166
167 prev := int(m.primaryID.Swap(int32(newPrimaryIx)))
168 if prev == newPrimaryIx {
169 return false, nil
170 }
171
172 defer m.updatePrimaryStoreGauge() // do as the last thing, after releasing the lock
173
174 // switching to new primary... cancel clients using previous one
175 m.inProgressMu.Lock()
176 defer m.inProgressMu.Unlock()
177
178 for _, inp := range m.inProgress {
179 if inp.client == prev {
180 inp.cancel()
181 }
182 }
183 return true, nil
184}
185
186func (m *MultiClient) registerMetrics(registerer prometheus.Registerer) {
187 m.primaryStoreGauge = promauto.With(registerer).NewGaugeVec(prometheus.GaugeOpts{

Callers 1

watchConfigChannelMethod · 0.95

Calls 3

ErrorfMethod · 0.80
SwapMethod · 0.45

Tested by

no test coverage detected