Пример #1
0
func putKV(client *api.Client, key, value string, index uint64) (bool, error) {
	p := &api.KVPair{Key: key[1:], Value: []byte(value), ModifyIndex: index}
	ok, _, err := client.KV().CAS(p, nil)
	if err != nil {
		return false, err
	}
	return ok, nil
}
Пример #2
0
func getKV(client *api.Client, key string, waitIndex uint64) (string, uint64, error) {
	q := &api.QueryOptions{RequireConsistent: true, WaitIndex: waitIndex}
	kvpair, meta, err := client.KV().Get(key, q)
	if err != nil {
		return "", 0, err
	}
	if kvpair == nil {
		return "", meta.LastIndex, nil
	}
	return strings.TrimSpace(string(kvpair.Value)), meta.LastIndex, nil
}
Пример #3
0
func nextValue(client *api.Client, path string, lastIndex uint64) (string, uint64) {
	for {
		q := &api.QueryOptions{RequireConsistent: true, WaitIndex: lastIndex}
		kvpair, meta, err := client.KV().Get(path, q)
		if err != nil {
			log.Printf("[WARN] Error fetching config from %s. %v", path, err)
			time.Sleep(time.Second)
			continue
		}

		if kvpair == nil {
			return "", meta.LastIndex
		}

		return strings.TrimSpace(string(kvpair.Value)), meta.LastIndex
	}
}