Documentation
¶
Overview ¶
Package consul provides a source.Reader implementation that reads scripts from a HashiCorp Consul KV store (or any Consul-compatible agent such as Nomad's embedded KV, etc.).
Construction:
src, err := consul.New(ctx,
consul.WithAddress("127.0.0.1:8500"),
consul.WithPrefix("scripts/lua/"),
)
Hot-reload detection polls the key's ModifyIndex via Get every 5 seconds and sends a signal when the index changes.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("consul source: key not found")
ErrNotFound is returned (wrapped) by Load when the requested key does not exist in Consul's KV store. Detect with errors.Is(err, ErrNotFound) or the convenience helper IsNotFound.
Functions ¶
func IsNotFound ¶
IsNotFound reports whether err represents a "key not found" response from Consul. Equivalent to errors.Is(err, ErrNotFound).
Types ¶
type Option ¶
type Option func(*configOptions)
Option configures a Reader. Pass to New.
func WithAddress ¶
WithAddress sets the Consul agent address (default "127.0.0.1:8500").
func WithPrefix ¶
WithPrefix sets a key prefix that is transparently prepended to every key before it is resolved against Consul. Useful when all scripts share a common namespace (e.g. WithPrefix("scripts/lua/")).
Leading slashes are stripped; no other normalization is applied.
func WithTimeout ¶
WithTimeout sets the Consul client HTTP timeout (default 5s). This only applies when the Reader constructs its own client (i.e. not in tests).
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads scripts from Consul's KV store.
All exported methods are safe for concurrent use. Reader implements the source.ReadWatcher interface.
func New ¶
New creates a Consul-backed Reader. Address defaults to "127.0.0.1:8500"; override with WithAddress. All other settings are optional.
Authentication can be enabled via WithToken.
func (*Reader) Load ¶
Load fetches the value from Consul's KV store and returns it as a string. Context cancellation propagates to the underlying request via QueryOptions.
A nil KVPair (key not found) is reported as a wrapped ErrNotFound. Other errors are wrapped with the key for easier debugging.
func (*Reader) Watch ¶
Watch returns a channel that signals when the value identified by `key` changes. It polls the key's ModifyIndex via Get every 5 seconds and sends a signal on the channel when the index differs from the one recorded during the last Load.
The returned channel is closed when the context is cancelled. Callers should re-Load the script after receiving from the channel.