Documentation
¶
Overview ¶
Package distconf is a distributed configuration framework for go.
Index ¶
- type Bool
- type BoolWatch
- type CommandLine
- type Distconf
- func (c *Distconf) Bool(ctx context.Context, key string, defaultVal bool) *Bool
- func (c *Distconf) Duration(ctx context.Context, key string, defaultVal time.Duration) *Duration
- func (c *Distconf) Float(ctx context.Context, key string, defaultVal float64) *Float
- func (c *Distconf) Info() expvar.Var
- func (c *Distconf) Int(ctx context.Context, key string, defaultVal int64) *Int
- func (c *Distconf) Refresh(ctx context.Context, key string)
- func (c *Distconf) Shutdown(ctx context.Context) error
- func (c *Distconf) Str(ctx context.Context, key string, defaultVal string) *Str
- func (c *Distconf) Var() expvar.Var
- type Duration
- type DurationWatch
- type Environment
- type Float
- type FloatWatch
- type Hooks
- type Int
- type IntWatch
- type Mem
- type Reader
- type Shutdownable
- type Str
- type StrWatch
- type Watcher
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bool ¶
type Bool struct {
// contains filtered or unexported fields
}
Bool is a Boolean type config inside a Config. It uses strconv.ParseBool to parse the conf contents as either true for false
type BoolWatch ¶
BoolWatch is executed if registered on a Bool variable any time the Bool contents change
type CommandLine ¶
type Distconf ¶
type Distconf struct { // Hooks are optional callbacks that let you get information about the internal workings and errors of distconf. Hooks Hooks // Readers are an ordered list of backends for DistConf to get configuration information from. The list // order is important as information from a first backend will be returned before the later ones. Readers []Reader // How long to timeout out of band refresh calls triggered by Watch() callbacks. Defaults to 1 second. RefreshTimeout time.Duration // contains filtered or unexported fields }
Distconf gets configuration data from the first backing that has it. It is a race condition to modify Hooks or Readers after you've started using Distconf. All public functions of Distconf are thread safe.
Example ¶
package main import ( "context" "fmt" "github.com/cep21/distconf" ) func main() { ctx := context.Background() m := distconf.Mem{} if err := m.Write(ctx, "value", []byte("true")); err != nil { panic("never happens") } d := distconf.Distconf{ Readers: []distconf.Reader{&m}, } x := d.Bool(ctx, "value", false) fmt.Println(x.Get()) }
Output: true
Example (Defaults) ¶
package main import ( "context" "fmt" "github.com/cep21/distconf" ) func main() { ctx := context.Background() d := distconf.Distconf{} x := d.Float(ctx, "value", 1.1) fmt.Println(x.Get()) }
Output: 1.1
func (*Distconf) Bool ¶
Bool object that can be referenced to get boolean values from a backing config
Example ¶
package main import ( "context" "fmt" "github.com/cep21/distconf" ) func main() { ctx := context.Background() m := distconf.Mem{} if err := m.Write(ctx, "value", []byte("true")); err != nil { panic("never happens") } d := distconf.Distconf{ Readers: []distconf.Reader{&m}, } x := d.Bool(ctx, "value", false) fmt.Println(x.Get()) }
Output: true
func (*Distconf) Duration ¶
Duration returns a duration object that calls ParseDuration() on the given key
func (*Distconf) Float ¶
Float object that can be referenced to get float values from a backing config
Example ¶
package main import ( "context" "fmt" "github.com/cep21/distconf" ) func main() { ctx := context.Background() m := distconf.Mem{} if err := m.Write(ctx, "value", []byte("3.2")); err != nil { panic("never happens") } d := distconf.Distconf{ Readers: []distconf.Reader{&m}, } x := d.Float(ctx, "value", 1.0) fmt.Println(x.Get()) }
Output: 3.2
func (*Distconf) Info ¶
Info returns an expvar variable that shows the information for all configuration variables. Information consist of file, line, default value and type of variable.
func (*Distconf) Int ¶
Int object that can be referenced to get integer values from a backing config.
func (*Distconf) Refresh ¶
Refresh a single key from readers. This will force a blocking Read from the Readers, in order, until one of them has the key. It will then update the distconf value for that key and trigger any update callbacks. You do not generally need to call this. If your backends implement Watcher, they will trigger this for you.
func (*Distconf) Shutdown ¶
Shutdown this config framework's Readers. Config variable results are undefined after this call. Returns the error of the first reader to return an error. While Watch itself doesn't take a context, shutdown will stop calling future watches if context ends.
type Duration ¶
type Duration struct {
// contains filtered or unexported fields
}
Duration is a duration type config inside a Config.
func (*Duration) Watch ¶
func (s *Duration) Watch(watch DurationWatch)
Watch adds a watch for changes to this structure
type DurationWatch ¶
DurationWatch is executed if registered on a Duration variable any time the contents change
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
type Float ¶
type Float struct {
// contains filtered or unexported fields
}
Float is an float type config inside a Config.
func (*Float) Get ¶
Get the float in this config variable
Example (Inloop) ¶
package main import ( "context" "fmt" "github.com/cep21/distconf" ) func main() { ctx := context.Background() m := distconf.Mem{} if err := m.Write(ctx, "value", []byte("2.0")); err != nil { panic("never happens") } d := distconf.Distconf{ Readers: []distconf.Reader{&m}, } x := d.Float(ctx, "value", 1.0) sum := 0.0 for i := 0; i < 1000; i++ { sum += x.Get() } fmt.Println(sum) }
Output: 2000
func (*Float) Watch ¶
func (c *Float) Watch(watch FloatWatch)
Watch for changes to this variable.
Example ¶
package main import ( "context" "fmt" "github.com/cep21/distconf" ) func main() { ctx := context.Background() m := distconf.Mem{} d := distconf.Distconf{ Readers: []distconf.Reader{&m}, } x := d.Float(ctx, "value", 1.0) x.Watch(func(f *distconf.Float, oldValue float64) { fmt.Println("Change from", oldValue, "to", f.Get()) }) fmt.Println("first", x.Get()) if err := m.Write(ctx, "value", []byte("2.1")); err != nil { panic("never happens") } fmt.Println("second", x.Get()) }
Output: first 1 Change from 1 to 2.1 second 2.1
type FloatWatch ¶
FloatWatch is called on any changes to a register integer config variable
type Hooks ¶
type Hooks struct { // OnError is called whenever there is an error doing something internally to distconf, but the error itself // cannot be directly returned to the caller. // distconfKey is the key that caused the error. OnError func(msg string, distconfKey string, err error) }
Hooks are optional callbacks that let you get information about the internal workings and errors of distconf. All functions of Hooks should be thread safe.
type Int ¶
type Int struct {
// contains filtered or unexported fields
}
Int is an integer type config inside a Config.
type Reader ¶
type Reader interface { // Read should lookup a key inside the configuration source. This function should // be thread safe, but is allowed to be slow or block. That block will only happen // on application startup. An error will skip this source and fall back to another // source in the chain. If the value is not inside this reader, return nil, nil. Read(ctx context.Context, key string) ([]byte, error) }
Reader can get a []byte value for a config key
type Shutdownable ¶
type Shutdownable interface { // Shutdown should signal to a reader it is no longer needed by Distconf. It should expect // to no longer require to return more recent values to distconf. Shutdown(ctx context.Context) error }
Shutdownable is an optional interface of Reader that allows it to be gracefully shutdown.
type Str ¶
type Str struct {
// contains filtered or unexported fields
}
Str is a string type config inside a Config.
type StrWatch ¶
StrWatch is executed if registered on a Str variable any time the Str contents change
type Watcher ¶
type Watcher interface { // Watch a key for a change in value. When the value for that key changes, // execute 'callback'. It is ok to execute callback more times than needed. // Each call to callback will probably trigger future calls to Get(). // Watch may be called multiple times for a single key. Only the latest callback needs to be executed. // It is possible callback may itself call watch. Be careful with locking. // If callback is nil, then we are trying to remove a previously registered callback. Watch(ctx context.Context, key string, callback func()) error }
A Watcher config can change what it thinks a value is over time.