2019-05-30 23:11:13 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-08-25 15:41:48 +03:00
|
|
|
"fmt"
|
2019-05-30 23:11:13 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config/loader"
|
|
|
|
"github.com/unistack-org/micro/v3/config/reader"
|
|
|
|
"github.com/unistack-org/micro/v3/config/source"
|
2019-05-30 23:11:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
exit chan bool
|
|
|
|
opts Options
|
|
|
|
|
|
|
|
sync.RWMutex
|
|
|
|
// the current snapshot
|
|
|
|
snap *loader.Snapshot
|
|
|
|
// the current values
|
|
|
|
vals reader.Values
|
|
|
|
}
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
lw loader.Watcher
|
|
|
|
rd reader.Reader
|
|
|
|
path []string
|
|
|
|
value reader.Value
|
|
|
|
}
|
|
|
|
|
2020-01-19 16:31:02 +08:00
|
|
|
func newConfig(opts ...Option) (Config, error) {
|
2020-03-12 18:13:03 +00:00
|
|
|
var c config
|
|
|
|
|
2020-07-28 13:54:58 +01:00
|
|
|
if err := c.Init(opts...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-12 18:13:03 +00:00
|
|
|
|
2020-07-28 13:54:58 +01:00
|
|
|
go c.run()
|
2020-03-12 18:13:03 +00:00
|
|
|
return &c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Init(opts ...Option) error {
|
2020-08-25 13:44:41 +03:00
|
|
|
c.opts = Options{}
|
2020-03-19 12:54:59 +03:00
|
|
|
c.exit = make(chan bool)
|
2019-05-30 23:11:13 +01:00
|
|
|
for _, o := range opts {
|
2020-03-12 18:13:03 +00:00
|
|
|
o(&c.opts)
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 18:13:03 +00:00
|
|
|
err := c.opts.Loader.Load(c.opts.Source...)
|
2020-01-19 16:42:05 +08:00
|
|
|
if err != nil {
|
2020-03-12 18:13:03 +00:00
|
|
|
return err
|
2020-01-19 16:42:05 +08:00
|
|
|
}
|
2020-03-12 18:13:03 +00:00
|
|
|
|
|
|
|
c.snap, err = c.opts.Loader.Snapshot()
|
2020-01-19 16:42:05 +08:00
|
|
|
if err != nil {
|
2020-03-12 18:13:03 +00:00
|
|
|
return err
|
2020-01-19 16:42:05 +08:00
|
|
|
}
|
2019-05-30 23:11:13 +01:00
|
|
|
|
2020-03-12 18:13:03 +00:00
|
|
|
c.vals, err = c.opts.Reader.Values(c.snap.ChangeSet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
|
2020-03-12 18:13:03 +00:00
|
|
|
return nil
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 17:13:21 +01:00
|
|
|
func (c *config) Options() Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
func (c *config) run() {
|
|
|
|
watch := func(w loader.Watcher) error {
|
|
|
|
for {
|
|
|
|
// get changeset
|
|
|
|
snap, err := w.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
|
2020-07-14 07:52:45 +01:00
|
|
|
if c.snap != nil && c.snap.Version >= snap.Version {
|
2020-06-17 00:10:52 +08:00
|
|
|
c.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
// save
|
|
|
|
c.snap = snap
|
|
|
|
|
|
|
|
// set values
|
|
|
|
c.vals, _ = c.opts.Reader.Values(snap.ChangeSet)
|
|
|
|
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
w, err := c.opts.Loader.Watch()
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
// the stop watch func
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-c.exit:
|
|
|
|
}
|
|
|
|
w.Stop()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// block watch
|
|
|
|
if err := watch(w); err != nil {
|
|
|
|
// do something better
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close done chan
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
// if the config is closed exit
|
|
|
|
select {
|
|
|
|
case <-c.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Map() map[string]interface{} {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.vals.Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Scan(v interface{}) error {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
return c.vals.Scan(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sync loads all the sources, calls the parser and updates the config
|
|
|
|
func (c *config) Sync() error {
|
|
|
|
if err := c.opts.Loader.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap, err := c.opts.Loader.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.snap = snap
|
|
|
|
vals, err := c.opts.Reader.Values(snap.ChangeSet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.vals = vals
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Close() error {
|
|
|
|
select {
|
|
|
|
case <-c.exit:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(c.exit)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:41:48 +03:00
|
|
|
func (c *config) Get(path ...string) (reader.Value, error) {
|
2019-05-30 23:11:13 +01:00
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
// did sync actually work?
|
|
|
|
if c.vals != nil {
|
|
|
|
return c.vals.Get(path...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// no value
|
2020-08-25 15:41:48 +03:00
|
|
|
return nil, fmt.Errorf("no value")
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:41:48 +03:00
|
|
|
func (c *config) Set(val interface{}, path ...string) error {
|
2020-01-12 04:50:09 +08:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.vals != nil {
|
|
|
|
c.vals.Set(val, path...)
|
|
|
|
}
|
2020-08-25 15:41:48 +03:00
|
|
|
return nil
|
2020-01-12 04:50:09 +08:00
|
|
|
}
|
|
|
|
|
2020-08-25 15:41:48 +03:00
|
|
|
func (c *config) Del(path ...string) error {
|
2020-01-12 04:50:09 +08:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
if c.vals != nil {
|
|
|
|
c.vals.Del(path...)
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:41:48 +03:00
|
|
|
return nil
|
2020-01-12 04:50:09 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 23:11:13 +01:00
|
|
|
func (c *config) Bytes() []byte {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
if c.vals == nil {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.vals.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Load(sources ...source.Source) error {
|
|
|
|
if err := c.opts.Loader.Load(sources...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap, err := c.opts.Loader.Snapshot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.snap = snap
|
|
|
|
vals, err := c.opts.Reader.Values(snap.ChangeSet)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.vals = vals
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) Watch(path ...string) (Watcher, error) {
|
2020-08-25 15:41:48 +03:00
|
|
|
value, err := c.Get(path...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-30 23:11:13 +01:00
|
|
|
|
|
|
|
w, err := c.opts.Loader.Watch(path...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &watcher{
|
|
|
|
lw: w,
|
|
|
|
rd: c.opts.Reader,
|
|
|
|
path: path,
|
|
|
|
value: value,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *config) String() string {
|
|
|
|
return "config"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Next() (reader.Value, error) {
|
|
|
|
for {
|
|
|
|
s, err := w.lw.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// only process changes
|
|
|
|
if bytes.Equal(w.value.Bytes(), s.ChangeSet.Data) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := w.rd.Values(s.ChangeSet)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:41:48 +03:00
|
|
|
return v.Get()
|
2019-05-30 23:11:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Stop() error {
|
|
|
|
return w.lw.Stop()
|
|
|
|
}
|