viper/remote/consul/consul.go

120 lines
2.6 KiB
Go
Raw Normal View History

package consul
import (
"bytes"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/watch"
"github.com/spf13/viper"
"io"
2019-04-10 11:28:10 +03:00
"sync"
)
2019-04-10 11:28:10 +03:00
type consulConfigProvider struct {
mu sync.Mutex
idxMap map[string]uint64
}
2019-04-10 11:28:10 +03:00
func (rc *consulConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
config := api.DefaultConfig()
config.Address = rp.Endpoint()
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
2019-04-10 11:28:10 +03:00
kv, meta, err := client.KV().Get(rp.Path(), nil)
if err != nil {
return nil, err
}
2019-04-10 11:28:10 +03:00
rc.updateIndex(rp, meta.LastIndex)
return bytes.NewReader(kv.Value), nil
}
2019-04-10 11:28:10 +03:00
func (rc *consulConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
return rc.watch(rp)
}
2019-04-10 11:28:10 +03:00
func (rc *consulConfigProvider) watch(rp viper.RemoteProvider) (r io.Reader, err error) {
p, err := watch.Parse(map[string]interface{}{"type": "key", "key": rp.Path()})
if err != nil {
return nil, err
}
// handler
p.Handler = func(index uint64, data interface{}) {
if data == nil {
return
}
kv, ok := data.(*api.KVPair)
if !ok {
return
}
if !rc.updateIndex(rp, index) {
return
}
r = bytes.NewReader(kv.Value)
p.Stop()
}
// start watch
p.Run(rp.Endpoint())
return
}
func (rc *consulConfigProvider) WatchChannel(
rp viper.RemoteProvider) (resp <-chan *viper.RemoteResponse, quit chan bool) {
2019-04-10 11:28:10 +03:00
return rc.watchChannel(rp)
}
2019-04-10 11:28:10 +03:00
func (rc *consulConfigProvider) watchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
p, err := watch.Parse(map[string]interface{}{"type": "key", "key": rp.Path()})
if err != nil {
2019-04-10 11:28:10 +03:00
// this should not happen
return nil, nil
}
quit := make(chan bool)
viperResponseCh := make(chan *viper.RemoteResponse)
2019-04-10 11:28:10 +03:00
// handler
p.Handler = func(index uint64, data interface{}) {
if data == nil {
return
}
kv, ok := data.(*api.KVPair)
if !ok {
return
}
2019-04-10 11:28:10 +03:00
if !rc.updateIndex(rp, index) {
return
}
select {
case viperResponseCh <- &viper.RemoteResponse{Value: kv.Value}:
case <-quit:
}
}
2019-04-10 11:28:10 +03:00
// start watcher
go p.Run(rp.Endpoint())
// wait quit
go func() {
<-quit
p.Stop()
}()
return viperResponseCh, quit
}
2019-04-10 11:28:10 +03:00
func makeIndexKey(rp viper.RemoteProvider) string {
return rp.Endpoint() + "_" + rp.Path()
}
func (rc *consulConfigProvider) updateIndex(
rp viper.RemoteProvider, lastIndex uint64) (updated bool) {
rc.mu.Lock()
oldLastIndex := rc.idxMap[makeIndexKey(rp)]
if oldLastIndex < lastIndex {
rc.idxMap[makeIndexKey(rp)] = lastIndex
updated = true
}
rc.mu.Unlock()
return
}
func init() {
2019-04-10 11:28:10 +03:00
viper.RemoteConfig = &consulConfigProvider{idxMap: make(map[string]uint64)}
}