mirror of https://github.com/spf13/viper.git
feat: add a cancel function return from WatchConfig() to stop watching manually
This commit is contained in:
parent
097e0d888f
commit
6fb91e06ef
11
viper.go
11
viper.go
|
@ -21,6 +21,7 @@ package viper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -431,10 +432,13 @@ func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WatchConfig starts watching a config file for changes.
|
// WatchConfig starts watching a config file for changes.
|
||||||
func WatchConfig() { v.WatchConfig() }
|
// The function returned for stop watching manually.
|
||||||
|
func WatchConfig() func() { return v.WatchConfig() }
|
||||||
|
|
||||||
// WatchConfig starts watching a config file for changes.
|
// WatchConfig starts watching a config file for changes.
|
||||||
func (v *Viper) WatchConfig() {
|
// The function returned for stop watching manually.
|
||||||
|
func (v *Viper) WatchConfig() func() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
initWG := sync.WaitGroup{}
|
initWG := sync.WaitGroup{}
|
||||||
initWG.Add(1)
|
initWG.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -492,6 +496,8 @@ func (v *Viper) WatchConfig() {
|
||||||
}
|
}
|
||||||
eventsWG.Done()
|
eventsWG.Done()
|
||||||
return
|
return
|
||||||
|
case <-ctx.Done(): // cancel function called
|
||||||
|
watcher.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -500,6 +506,7 @@ func (v *Viper) WatchConfig() {
|
||||||
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
|
eventsWG.Wait() // now, wait for event loop to end in this go-routine...
|
||||||
}()
|
}()
|
||||||
initWG.Wait() // make sure that the go routine above fully ended before returning
|
initWG.Wait() // make sure that the go routine above fully ended before returning
|
||||||
|
return cancel
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetConfigFile explicitly defines the path, name and extension of the config file.
|
// SetConfigFile explicitly defines the path, name and extension of the config file.
|
||||||
|
|
Loading…
Reference in New Issue