mirror of
https://github.com/go-micro/go-micro.git
synced 2025-07-12 22:41:07 +02:00
option to disable file watcher (#2485)
Co-authored-by: Sergey Galkin <v.sergey.galkin@reddit.com>
This commit is contained in:
@ -41,6 +41,8 @@ type Options struct {
|
|||||||
|
|
||||||
// for alternative data
|
// for alternative data
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
|
||||||
|
WithWatcherDisabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(o *Options)
|
type Option func(o *Options)
|
||||||
|
@ -37,7 +37,9 @@ func newConfig(opts ...Option) (Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if !c.opts.WithWatcherDisabled {
|
||||||
go c.run()
|
go c.run()
|
||||||
|
}
|
||||||
|
|
||||||
return &c, nil
|
return &c, nil
|
||||||
}
|
}
|
||||||
@ -53,7 +55,12 @@ func (c *config) Init(opts ...Option) error {
|
|||||||
|
|
||||||
// default loader uses the configured reader
|
// default loader uses the configured reader
|
||||||
if c.opts.Loader == nil {
|
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...)
|
err := c.opts.Loader.Load(c.opts.Source...)
|
||||||
|
@ -48,6 +48,8 @@ type Options struct {
|
|||||||
|
|
||||||
// for alternative data
|
// for alternative data
|
||||||
Context context.Context
|
Context context.Context
|
||||||
|
|
||||||
|
WithWatcherDisabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(o *Options)
|
type Option func(o *Options)
|
||||||
|
@ -323,8 +323,10 @@ func (m *memory) Load(sources ...source.Source) error {
|
|||||||
m.sets = append(m.sets, set)
|
m.sets = append(m.sets, set)
|
||||||
idx := len(m.sets) - 1
|
idx := len(m.sets) - 1
|
||||||
m.Unlock()
|
m.Unlock()
|
||||||
|
if !m.opts.WithWatcherDisabled {
|
||||||
go m.watch(idx, source)
|
go m.watch(idx, source)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := m.reload(); err != nil {
|
if err := m.reload(); err != nil {
|
||||||
gerrors = append(gerrors, err.Error())
|
gerrors = append(gerrors, err.Error())
|
||||||
@ -338,6 +340,10 @@ func (m *memory) Load(sources ...source.Source) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *memory) Watch(path ...string) (loader.Watcher, 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...)
|
value, err := m.Get(path...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -449,8 +455,10 @@ func NewLoader(opts ...loader.Option) loader.Loader {
|
|||||||
|
|
||||||
for i, s := range options.Source {
|
for i, s := range options.Source {
|
||||||
m.sets[i] = &source.ChangeSet{Source: s.String()}
|
m.sets[i] = &source.ChangeSet{Source: s.String()}
|
||||||
|
if !options.WithWatcherDisabled {
|
||||||
go m.watch(i, s)
|
go m.watch(i, s)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
@ -19,3 +19,9 @@ func WithReader(r reader.Reader) loader.Option {
|
|||||||
o.Reader = r
|
o.Reader = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithWatcherDisabled() loader.Option {
|
||||||
|
return func(o *loader.Options) {
|
||||||
|
o.WithWatcherDisabled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -26,3 +26,9 @@ func WithReader(r reader.Reader) Option {
|
|||||||
o.Reader = r
|
o.Reader = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithWatcherDisabled() Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
o.WithWatcherDisabled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user