func storeData(kv *api.KV, key string, value []byte) { pair, _, err := kv.Get(key, nil) if err != nil { Error.Fatal("Failed reading from Consul for key:", key, "Error:", err) } if pair == nil || string(pair.Value) != string(value) { var p *api.KVPair if pair == nil { p = &api.KVPair{Key: key, Value: value} } else { p = &api.KVPair{Key: key, Value: value, ModifyIndex: pair.ModifyIndex} } success, meta, err := kv.CAS(p, nil) if err != nil { Error.Fatal(err) } if !success { storeData(kv, key, value) } else { Trace.Print("Request took: ", meta.RequestTime.Seconds(), " seconds") Info.Print("Updated key '", key, "' to '", string(value), "'") } } else { Trace.Print("not updating key", key) } }
func monitorConsulGoroutine(kv *consulapi.KV, service string, lastIndex uint64) { for { pair, qm, err := kv.Get( consulPrefix+service, &consulapi.QueryOptions{ WaitIndex: lastIndex, RequireConsistent: true, }) if err != nil { if consulapi.IsServerError(err) { // Consul unavailable. Try again. time.Sleep(1 * time.Second) continue } if strings.Contains(err.Error(), "read: connection timed out") { // Try again. time.Sleep(1 * time.Second) continue } log.Fatalf("Error monitoring config in Consul: %v\n", err) } if pair == nil { log.Fatalf("Config in consul has been deleted\n") } updateConsulFlags(pair.Value) lastIndex = qm.LastIndex } }
func keyExists(key string, kv *consul.KV) bool { kp, _, err := kv.Get(key, nil) if err != nil { log.Warnf("Could not get key path %s: %v", key, err) return false } return kp != nil }
func fetchConsul(kv *consul.KV, env string, key string) (value string, conf_err error) { pair, _, _ := kv.Get("go-sms/"+env+"/"+key, nil) if pair == nil { conf_err = errors.New("Failed to fetch \"" + key + "\" key for you environment. Leave default: " + defaults[key]) value = defaults[key] return } value = string(pair.Value) return }
// getSequencer gets the lockindex, session id and the key which constitutes the sequencer for the current lock. func getSequencer(kv *api.KV, key string) (*sequencer, error) { kvPair, _, err := kv.Get(key, nil) if err != nil { log.Println("Can't get the sequencer") return nil, err } if kvPair == nil { return nil, nil } seq := sequencer{} seq.lockIndex = kvPair.LockIndex seq.session = kvPair.Session seq.key = kvPair.Key return &seq, nil }
keysPairs, _, err := kvClient.Keys(joinedKeys, "/", nil) if err != nil { log.Error("Error getting keys ", joinedKeys, err) return nil } keysPairs = fun.Filter(func(key string) bool { if key == joinedKeys { return false } return true }, keysPairs).([]string) return keysPairs }, "Get": func(keys ...string) string { joinedKeys := strings.Join(keys, "") keyPair, _, err := kvClient.Get(joinedKeys, nil) if err != nil { log.Error("Error getting key ", joinedKeys, err) return "" } return string(keyPair.Value) }, "Last": func(key string) string { splittedKey := strings.Split(key, "/") return splittedKey[len(splittedKey)-2] }, } func NewConsulProvider() *ConsulProvider { consulProvider := new(ConsulProvider) // default values
func consulGet(kv *api.KV, key string, lastIndex uint64) (interface{}, *api.QueryMeta, error) { return kv.Get(key, &api.QueryOptions{WaitIndex: lastIndex}) }