Skip to content

Golang variables with channel-based change notifications

License

Notifications You must be signed in to change notification settings

bobvawter/go-notify

Repository files navigation

Golang Channel-Based Data Notifications

Go Reference

go get vawter.tech/notify

This package provides a storage variable type with channel-based notifications of changes. It is useful for values that need to be observed at runtime, such as configuration types. The notify.Var type can be thought of as a cross between an atomic.Value coupled to a sync.Cond, albeit using channels for control-flow composition.

v := notify.VarOf(&ConfigurationType{})

// Somewhere in your run loop to respond to changes.
for cfg, cfgUpdated := v.Get(); ; {
    // Do something with the new configuration.
    select {
    case <-cfgUpdated:
        // The channel will be closed when the contents of the
        // variable have changed. This composes easily with multiple
        // channels.
        cfg, cfgUpdated = v.Get()

    case <-ctx.Done():
        return
    }
}

// Elsewhere to update the value.
v.Set(&Configuration{Updated: true})

Project History

This repository was extracted from github.com/cockroachdb/field-eng-powertools using the command git filter-repo --path stopvar --subdirectory-filter notify --path LICENSE by the code's original author.

About

Golang variables with channel-based change notifications

Topics

Resources

License

Stars

Watchers

Forks