From 1219d57f587c9e621b191e124b1c30102efcc22f Mon Sep 17 00:00:00 2001 From: Sergey Galkin Date: Tue, 19 Apr 2022 08:26:14 +0300 Subject: [PATCH] option to disable file watcher (#2485) (#2486) Co-authored-by: Sergey Galkin Co-authored-by: Sergey Galkin --- config/config.go | 2 ++ config/default.go | 11 +++++++++-- config/loader/loader.go | 2 ++ config/loader/memory/memory.go | 12 ++++++++++-- config/loader/memory/options.go | 6 ++++++ config/options.go | 6 ++++++ 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 5715cdd7..023f3736 100644 --- a/config/config.go +++ b/config/config.go @@ -41,6 +41,8 @@ type Options struct { // for alternative data Context context.Context + + WithWatcherDisabled bool } type Option func(o *Options) diff --git a/config/default.go b/config/default.go index 7c3ea134..1ad73413 100644 --- a/config/default.go +++ b/config/default.go @@ -37,7 +37,9 @@ func newConfig(opts ...Option) (Config, error) { if err != nil { return nil, err } - go c.run() + if !c.opts.WithWatcherDisabled { + go c.run() + } return &c, nil } @@ -53,7 +55,12 @@ func (c *config) Init(opts ...Option) error { // default loader uses the configured reader if c.opts.Loader == nil { - c.opts.Loader = memory.NewLoader(memory.WithReader(c.opts.Reader)) + loaderOpts := []loader.Option{memory.WithReader(c.opts.Reader)} + if c.opts.WithWatcherDisabled { + loaderOpts = append(loaderOpts, memory.WithWatcherDisabled()) + } + + c.opts.Loader = memory.NewLoader(loaderOpts...) } err := c.opts.Loader.Load(c.opts.Source...) diff --git a/config/loader/loader.go b/config/loader/loader.go index e46ddc7c..122fb8e5 100644 --- a/config/loader/loader.go +++ b/config/loader/loader.go @@ -48,6 +48,8 @@ type Options struct { // for alternative data Context context.Context + + WithWatcherDisabled bool } type Option func(o *Options) diff --git a/config/loader/memory/memory.go b/config/loader/memory/memory.go index a01d38d2..8d890ac5 100644 --- a/config/loader/memory/memory.go +++ b/config/loader/memory/memory.go @@ -323,7 +323,9 @@ func (m *memory) Load(sources ...source.Source) error { m.sets = append(m.sets, set) idx := len(m.sets) - 1 m.Unlock() - go m.watch(idx, source) + if !m.opts.WithWatcherDisabled { + go m.watch(idx, source) + } } if err := m.reload(); err != nil { @@ -338,6 +340,10 @@ func (m *memory) Load(sources ...source.Source) error { } func (m *memory) Watch(path ...string) (loader.Watcher, error) { + if m.opts.WithWatcherDisabled { + return nil, errors.New("watcher is disabled") + } + value, err := m.Get(path...) if err != nil { return nil, err @@ -449,7 +455,9 @@ func NewLoader(opts ...loader.Option) loader.Loader { for i, s := range options.Source { m.sets[i] = &source.ChangeSet{Source: s.String()} - go m.watch(i, s) + if !options.WithWatcherDisabled { + go m.watch(i, s) + } } return m diff --git a/config/loader/memory/options.go b/config/loader/memory/options.go index fd1bf90f..2174cfa3 100644 --- a/config/loader/memory/options.go +++ b/config/loader/memory/options.go @@ -19,3 +19,9 @@ func WithReader(r reader.Reader) loader.Option { o.Reader = r } } + +func WithWatcherDisabled() loader.Option { + return func(o *loader.Options) { + o.WithWatcherDisabled = true + } +} diff --git a/config/options.go b/config/options.go index 9f382fe0..b979788f 100644 --- a/config/options.go +++ b/config/options.go @@ -26,3 +26,9 @@ func WithReader(r reader.Reader) Option { o.Reader = r } } + +func WithWatcherDisabled() Option { + return func(o *Options) { + o.WithWatcherDisabled = true + } +}