From ea16f5f8253645c94f347e154bdc60f34b2c6ee2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 4 Aug 2021 16:04:58 +0300 Subject: [PATCH] config/default: not implement watcher as it cant change Signed-off-by: Vasiliy Tolstov --- config/default.go | 13 ++----- config/default_test.go | 50 -------------------------- config/watcher.go | 81 ------------------------------------------ 3 files changed, 2 insertions(+), 142 deletions(-) delete mode 100644 config/watcher.go diff --git a/config/default.go b/config/default.go index ded6d79c..88102821 100644 --- a/config/default.go +++ b/config/default.go @@ -2,6 +2,7 @@ package config import ( "context" + "fmt" "reflect" "strconv" "strings" @@ -270,17 +271,7 @@ func (c *defaultConfig) Name() string { } func (c *defaultConfig) Watch(ctx context.Context, opts ...WatchOption) (Watcher, error) { - w := &defaultWatcher{ - opts: c.opts, - wopts: NewWatchOptions(opts...), - done: make(chan struct{}), - vchan: make(chan map[string]interface{}), - echan: make(chan error), - } - - go w.run() - - return w, nil + return nil, fmt.Errorf("not implemented") } // NewConfig returns new default config source diff --git a/config/default_test.go b/config/default_test.go index 8a64a336..dfb4c5fb 100644 --- a/config/default_test.go +++ b/config/default_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "testing" - "time" "github.com/unistack-org/micro/v3/config" ) @@ -18,55 +17,6 @@ type Cfg struct { IntValue int `default:"99"` } -func TestWatch(t *testing.T) { - ctx := context.Background() - - conf := &Cfg{IntValue: 10} - - cfg := config.NewConfig(config.Struct(conf)) - if err := cfg.Init(); err != nil { - t.Fatal(err) - } - if err := cfg.Load(ctx); err != nil { - t.Fatal(err) - } - - w, err := cfg.Watch(ctx, config.WatchInterval(200*time.Millisecond, 500*time.Millisecond)) - if err != nil { - t.Fatal(err) - } - - defer func() { - _ = w.Stop() - }() - - done := make(chan struct{}) - - go func() { - for { - mp, err := w.Next() - if err != nil { - t.Fatal(err) - } - if len(mp) != 1 { - t.Fatal(fmt.Errorf("default watcher err: %v", mp)) - } - - v, ok := mp["IntValue"] - if !ok { - t.Fatal(fmt.Errorf("default watcher err: %v", v)) - } - if nv, ok := v.(int); !ok || nv != 99 { - t.Fatal(fmt.Errorf("default watcher err: %v", v)) - } - close(done) - return - } - }() - - <-done -} - func TestDefault(t *testing.T) { ctx := context.Background() conf := &Cfg{IntValue: 10} diff --git a/config/watcher.go b/config/watcher.go deleted file mode 100644 index 3e7563ea..00000000 --- a/config/watcher.go +++ /dev/null @@ -1,81 +0,0 @@ -package config - -import ( - "reflect" - - "github.com/unistack-org/micro/v3/util/jitter" - rutil "github.com/unistack-org/micro/v3/util/reflect" -) - -type defaultWatcher struct { - opts Options - wopts WatchOptions - done chan struct{} - vchan chan map[string]interface{} - echan chan error -} - -func (w *defaultWatcher) run() { - ticker := jitter.NewTicker(w.wopts.MinInterval, w.wopts.MaxInterval) - defer ticker.Stop() - - src := w.opts.Struct - if w.wopts.Struct != nil { - src = w.wopts.Struct - } - - for { - select { - case <-w.done: - return - case <-ticker.C: - dst, err := rutil.Zero(src) - if err == nil { - err = fillValues(reflect.ValueOf(dst), w.opts.StructTag) - } - if err != nil { - w.echan <- err - return - } - srcmp, err := rutil.StructFieldsMap(src) - if err != nil { - w.echan <- err - return - } - dstmp, err := rutil.StructFieldsMap(dst) - if err != nil { - w.echan <- err - return - } - for sk, sv := range srcmp { - if reflect.DeepEqual(dstmp[sk], sv) { - delete(dstmp, sk) - } - } - if len(dstmp) > 0 { - w.vchan <- dstmp - src = dst - } - } - } -} - -func (w *defaultWatcher) Next() (map[string]interface{}, error) { - select { - case <-w.done: - break - case err := <-w.echan: - return nil, err - case v, ok := <-w.vchan: - if !ok { - break - } - return v, nil - } - return nil, ErrWatcherStopped -} - -func (w *defaultWatcher) Stop() error { - close(w.done) - return nil -}