mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
remote: Avoid the start of go-routines which are never get stopped
This commit is contained in:
parent
7538d73b4e
commit
11ca61e888
2 changed files with 35 additions and 3 deletions
|
@ -33,14 +33,23 @@ func (rc remoteConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp := <-cm.Watch(rp.Path(), nil)
|
resp,err := cm.Get(rp.Path())
|
||||||
err = resp.Error
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return bytes.NewReader(resp.Value), nil
|
return bytes.NewReader(resp), nil
|
||||||
}
|
}
|
||||||
|
func (rc remoteConfigProvider) WatchChannel(rp viper.RemoteProvider) (<-chan *crypt.Response, chan bool) {
|
||||||
|
cm, err := getConfigManager(rp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
quit := make(chan bool)
|
||||||
|
return cm.Watch(rp.Path(), quit) ,quit
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) {
|
func getConfigManager(rp viper.RemoteProvider) (crypt.ConfigManager, error) {
|
||||||
|
|
||||||
|
|
23
viper.go
23
viper.go
|
@ -36,6 +36,7 @@ import (
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
|
crypt "github.com/xordataexchange/crypt/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var v *Viper
|
var v *Viper
|
||||||
|
@ -47,6 +48,7 @@ func init() {
|
||||||
type remoteConfigFactory interface {
|
type remoteConfigFactory interface {
|
||||||
Get(rp RemoteProvider) (io.Reader, error)
|
Get(rp RemoteProvider) (io.Reader, error)
|
||||||
Watch(rp RemoteProvider) (io.Reader, error)
|
Watch(rp RemoteProvider) (io.Reader, error)
|
||||||
|
WatchChannel(rp RemoteProvider)(<-chan *crypt.Response, chan bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoteConfig is optional, see the remote package
|
// RemoteConfig is optional, see the remote package
|
||||||
|
@ -1255,6 +1257,10 @@ func (v *Viper) WatchRemoteConfig() error {
|
||||||
return v.watchKeyValueConfig()
|
return v.watchKeyValueConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Viper) WatchRemoteConfigOnChannel() error {
|
||||||
|
return v.watchKeyValueConfigOnChannel()
|
||||||
|
}
|
||||||
|
|
||||||
// Unmarshall a Reader into a map.
|
// Unmarshall a Reader into a map.
|
||||||
// Should probably be an unexported function.
|
// Should probably be an unexported function.
|
||||||
func unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
func unmarshalReader(in io.Reader, c map[string]interface{}) error {
|
||||||
|
@ -1298,6 +1304,23 @@ func (v *Viper) getRemoteConfig(provider RemoteProvider) (map[string]interface{}
|
||||||
return v.kvstore, err
|
return v.kvstore, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Retrieve the first found remote configuration.
|
||||||
|
func (v *Viper) watchKeyValueConfigOnChannel() error {
|
||||||
|
for _, rp := range v.remoteProviders {
|
||||||
|
respc, _ := RemoteConfig.WatchChannel(rp)
|
||||||
|
//Todo: Add quit channel
|
||||||
|
go func(rc <-chan *crypt.Response) {
|
||||||
|
for {
|
||||||
|
b := <-rc
|
||||||
|
reader := bytes.NewReader(b.Value)
|
||||||
|
v.unmarshalReader(reader, v.kvstore)
|
||||||
|
}
|
||||||
|
}(respc)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return RemoteConfigError("No Files Found")
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve the first found remote configuration.
|
// Retrieve the first found remote configuration.
|
||||||
func (v *Viper) watchKeyValueConfig() error {
|
func (v *Viper) watchKeyValueConfig() error {
|
||||||
for _, rp := range v.remoteProviders {
|
for _, rp := range v.remoteProviders {
|
||||||
|
|
Loading…
Reference in a new issue