sync

package
v0.19.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateWatch = vterrors.New(vtrpc.Code_FAILED_PRECONDITION, "duplicate watch")

ErrDuplicateWatch is returned when Watch is called on a synced Viper which has already started a watch.

Functions

func AdaptGetter

func AdaptGetter[T any](key string, getter func(v *viper.Viper) func(key string) T, v *Viper) func(key string) T

AdaptGetter wraps a get function (matching the signature of viperutil.Options.GetFunc) to be threadsafe with the passed-in synced Viper.

It must be called prior to starting a watch on the synced Viper; it will panic if a watch has already been established.

This function must be called at most once per key; it will panic if attempting to adapt multiple getters for the same key.

Types

type Viper

type Viper struct {
	// contains filtered or unexported fields
}

Viper is a wrapper around a pair of viper.Viper instances to provide config- reloading in a threadsafe manner.

It maintains one viper, called "disk", which does the actual config watch and reload (via viper's WatchConfig), and a second viper, called "live", which Values (registered via viperutil.Configure with Dynamic=true) access their settings from. The "live" config only updates after blocking all values from reading in order to swap in the most recently-loaded config from the "disk".

func New

func New() *Viper

New returns a new synced Viper.

func (*Viper) AllSettings

func (v *Viper) AllSettings() map[string]any

AllSettings returns the current live settings.

func (*Viper) BindEnv

func (v *Viper) BindEnv(vars ...string) error

func (*Viper) BindPFlag

func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error

func (*Viper) Notify

func (v *Viper) Notify(ch chan<- struct{})

Notify adds a subscription that this synced viper will attempt to notify on config changes, after the updated config has been copied over from disk to live.

Analogous to signal.Notify, notifications are sent non-blocking, so users should account for this when consuming from the channel they've provided.

This function must be called prior to setting up a Watch; it will panic if a a watch has already been established on this synced Viper.

func (*Viper) RegisterAlias

func (v *Viper) RegisterAlias(alias string, key string)

func (*Viper) Set

func (v *Viper) Set(key string, value any)

Set sets the given key to the given value, in both the disk and live vipers.

func (*Viper) SetDefault

func (v *Viper) SetDefault(key string, value any)

func (*Viper) SetFs added in v0.18.0

func (v *Viper) SetFs(fs afero.Fs)

func (*Viper) Watch

func (v *Viper) Watch(ctx context.Context, static *viper.Viper, minWaitInterval time.Duration) (cancel context.CancelFunc, err error)

Watch starts watching the config used by the passed-in Viper. Before starting the watch, the synced viper will perform an initial read and load from disk so that the live config is ready for use without requiring an initial config change.

If the given static viper did not load a config file (and is instead relying purely on defaults, flags, and environment variables), then the settings of that viper are merged over, and this synced Viper may be used to set up an actual watch later. Additionally, this starts a background goroutine to persist changes made in-memory back to disk. It returns a cancel func to stop the persist loop, which the caller is responsible for calling during shutdown (see package servenv for an example).

This does two things — one which is a nice-to-have, and another which is necessary for correctness.

1. Writing in-memory changes (which usually occur through a request to a /debug/env endpoint) ensures they are persisted across process restarts. 2. Writing in-memory changes ensures that subsequent modifications to the config file do not clobber those changes. Because viper loads the entire config on-change, rather than an incremental (diff) load, if a user were to edit an unrelated key (keyA) in the file, and we did not persist the in-memory change (keyB), then future calls to keyB.Get() would return the older value.

If this synced viper is already watching a config file, this function returns an ErrDuplicateWatch. Other errors may be returned via underlying viper code to ensure the config file can be read in properly.

func (*Viper) WriteConfig

func (v *Viper) WriteConfig() error

WriteConfig writes the live viper config back to disk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL