Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support callback when remote config changed #1750

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions viper.go
Expand Up @@ -224,6 +224,8 @@ type Viper struct {
// TODO: should probably be protected with a mutex
encoderRegistry *encoding.EncoderRegistry
decoderRegistry *encoding.DecoderRegistry

onRemoteConfigChange func()
}

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

func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) }

func (v *Viper) OnRemoteConfigChange(run func()) {
v.onRemoteConfigChange = run
}

// ReadRemoteConfig attempts to get configuration from a remote source
// and read it in the remote configuration registry.
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
Expand Down Expand Up @@ -2024,6 +2032,10 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
b := <-rc
reader := bytes.NewReader(b.Value)
v.unmarshalReader(reader, v.kvstore)

if v.onRemoteConfigChange != nil {
v.onRemoteConfigChange()
}
}
}(respc)
return nil
Expand Down