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

Method Get

kv/consul/mock.go:152–201  ·  view source on GitHub ↗
(key string, q *consul.QueryOptions)

Source from the content-addressed store, hash-verified

150}
151
152func (m *mockKV) Get(key string, q *consul.QueryOptions) (*consul.KVPair, *consul.QueryMeta, error) {
153 level.Debug(m.logger).Log("msg", "Get", "key", key, "wait_index", q.WaitIndex)
154
155 m.mtx.Lock()
156 defer m.mtx.Unlock()
157
158 value := m.kvps[key]
159 if value == nil && q.WaitIndex == 0 {
160 level.Debug(m.logger).Log("msg", "Get - not found", "key", key)
161 return nil, &consul.QueryMeta{LastIndex: m.current}, nil
162 }
163
164 var valueModifyIndex uint64
165 if value != nil {
166 valueModifyIndex = value.ModifyIndex
167 } else {
168 valueModifyIndex = m.current
169 }
170
171 if q.WaitIndex >= valueModifyIndex && q.WaitTime > 0 {
172 deadline := time.Now().Add(mockedMaxWaitTime(q.WaitTime))
173 if ctxDeadline, ok := q.Context().Deadline(); ok && ctxDeadline.Before(deadline) {
174 // respect deadline from context, if set.
175 deadline = ctxDeadline
176 }
177
178 // simply wait until value.ModifyIndex changes. This allows us to test reporting old index values by resetting them.
179 startModify := valueModifyIndex
180 for startModify == valueModifyIndex && time.Now().Before(deadline) {
181 m.cond.Wait()
182 value = m.kvps[key]
183
184 if value != nil {
185 valueModifyIndex = value.ModifyIndex
186 }
187 }
188 if time.Now().After(deadline) {
189 level.Debug(m.logger).Log("msg", "Get - deadline exceeded", "key", key)
190 return nil, &consul.QueryMeta{LastIndex: q.WaitIndex}, nil
191 }
192 }
193
194 if value == nil {
195 level.Debug(m.logger).Log("msg", "Get - not found", "key", key)
196 return nil, &consul.QueryMeta{LastIndex: m.current}, nil
197 }
198
199 level.Debug(m.logger).Log("msg", "Get", "key", key, "modify_index", value.ModifyIndex, "value", fmt.Sprintf("%.40q", value.Value))
200 return copyKVPair(value), &consul.QueryMeta{LastIndex: value.ModifyIndex}, nil
201}
202
203func (m *mockKV) List(prefix string, q *consul.QueryOptions) (consul.KVPairs, *consul.QueryMeta, error) {
204 m.mtx.Lock()

Callers

nothing calls this directly

Calls 8

mockedMaxWaitTimeFunction · 0.85
copyKVPairFunction · 0.85
AddMethod · 0.65
BeforeMethod · 0.65
AfterMethod · 0.65
LogMethod · 0.45
ContextMethod · 0.45
WaitMethod · 0.45

Tested by

no test coverage detected