1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-18 08:26:38 +02:00

option to disable file watcher (#2485) (#2486)

Co-authored-by: Sergey Galkin <v.sergey.galkin@reddit.com>

Co-authored-by: Sergey Galkin <v.sergey.galkin@reddit.com>
This commit is contained in:
Sergey Galkin 2022-04-19 08:26:14 +03:00 committed by GitHub
parent 5a2f37c718
commit 1219d57f58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 4 deletions

View File

@ -41,6 +41,8 @@ type Options struct {
// for alternative data
Context context.Context
WithWatcherDisabled bool
}
type Option func(o *Options)

View File

@ -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...)

View File

@ -48,6 +48,8 @@ type Options struct {
// for alternative data
Context context.Context
WithWatcherDisabled bool
}
type Option func(o *Options)

View File

@ -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

View File

@ -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
}
}

View File

@ -26,3 +26,9 @@ func WithReader(r reader.Reader) Option {
o.Reader = r
}
}
func WithWatcherDisabled() Option {
return func(o *Options) {
o.WithWatcherDisabled = true
}
}