Skip to content

Commit aeda052

Browse files
committed
feat: support callback when remote config changed
1 parent 0e82215 commit aeda052

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

viper.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ type Viper struct {
224224
// TODO: should probably be protected with a mutex
225225
encoderRegistry *encoding.EncoderRegistry
226226
decoderRegistry *encoding.DecoderRegistry
227+
228+
onRemoteConfigChange func()
227229
}
228230

229231
// New returns an initialized Viper instance.
@@ -1959,6 +1961,12 @@ func mergeMaps(src, tgt map[string]any, itgt map[any]any) {
19591961
}
19601962
}
19611963

1964+
func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) }
1965+
1966+
func (v *Viper) OnRemoteConfigChange(run func()) {
1967+
v.onRemoteConfigChange = run
1968+
}
1969+
19621970
// ReadRemoteConfig attempts to get configuration from a remote source
19631971
// and read it in the remote configuration registry.
19641972
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
@@ -2024,6 +2032,10 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
20242032
b := <-rc
20252033
reader := bytes.NewReader(b.Value)
20262034
v.unmarshalReader(reader, v.kvstore)
2035+
2036+
if v.onRemoteConfigChange != nil {
2037+
v.onRemoteConfigChange()
2038+
}
20272039
}
20282040
}(respc)
20292041
return nil

0 commit comments

Comments
 (0)