klconsul

package
v0.8.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2020 License: MIT Imports: 11 Imported by: 0

README

Consul loader

Loads config from consul KV.

Usage

Basic usage loading keys and using result as string with watcher

etcdLoader := klconsul.New(&klconsul.Config{
	Client: consulClient, // from github.com/hashicorp/consul/api package
	Keys: []Key{
		{
			Key: "foo",
		},
	},
	Watch: true,
})

Loading keys and JSON parser

consulLoader := klconsul.New(&klconsul.Config{
	Client: consulClient, // from github.com/hashicorp/consul/api package
	Keys: []Key{
		{
			Key: "foo",
			Parser: kpjson.Parser,
		},
	},
	Watch: true,
})

Strict mode

If strict mode is enabled, a key defined in the config but missing in consul will trigger an error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Name is the name of the loader
	Name string
	// Client is the consul KV client
	Client *api.Client
	// StopOnFailure tells whether a load failure(after the retries) leads to closing the config and all registered closers
	StopOnFailure bool
	// Keys is the list of keys to fetch
	Keys []Key
	// Timeout is the timeout duration when fetching a key
	Timeout time.Duration
	// Prefix is a prefix to prepend keys when adding into the konfig.Store
	Prefix string
	// Replacer is a Replacer for the key before adding to the konfig.Store
	Replacer nstrings.Replacer
	// Watch tells if there should be a watcher with the loader
	Watch bool
	// Rater is the rater to pass to the poll watcher
	Rater kwpoll.Rater
	// MaxRetry is the maximum number of times we can retry to load if it fails
	MaxRetry int
	// RetryDelay is the time between each retry when a load fails
	RetryDelay time.Duration
	// Debug sets debug mode on the consulloader
	Debug bool
	// Logger is used across this package to produce logs
	Logger nlogger.Provider
	// StrictMode will raise error if key was not found
	// In false state, konfig will try to reload desired key(s)
	// up until they are not found
	StrictMode bool
	// contains filtered or unexported fields
}

Config is the structure representing the config of a Loader

type ConsulKV

type ConsulKV interface {
	Get(key string, q *api.QueryOptions) (*api.KVPair, *api.QueryMeta, error)
}

ConsulKV is an interface that consul client.KV implements. It is used to retrieve keys.

type Key

type Key struct {
	// Key is the consul key
	Key string
	// Parser is the parser for the key
	// If nil, the value is casted to a string before adding to the config.Store
	Parser parser.Parser
	// QueryOptions is the query options to pass when retrieving the key from consul
	QueryOptions *api.QueryOptions
}

Key is an Consul Key to load

type Loader

type Loader struct {
	*kwpoll.PollWatcher
	// contains filtered or unexported fields
}

Loader is the structure of a loader

func New

func New(cfg *Config) *Loader

New returns a new loader with the given config

func (*Loader) Load

func (l *Loader) Load(s konfig.Values) error

Load implements konfig.Loader, it loads environment variables into the konfig.Store based on config passed to the loader

func (*Loader) MaxRetry

func (l *Loader) MaxRetry() int

MaxRetry is the maximum number of time to retry when a load fails

func (*Loader) Name

func (l *Loader) Name() string

Name returns the name of the loader

func (*Loader) RetryDelay

func (l *Loader) RetryDelay() time.Duration

RetryDelay is the delay between each retry

func (*Loader) StopOnFailure

func (l *Loader) StopOnFailure() bool

StopOnFailure returns whether a load failure should stop the config and the registered closers

Jump to

Keyboard shortcuts

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