Documentation ¶
Overview ¶
Package etcdvar provides a runtimevar implementation with variables backed by etcd. Use New to construct a *runtimevar.Variable.
As ¶
etcdvar exposes the following types for As:
- Snapshot: *clientv3.GetResponse
- Error: rpctypes.EtcdError
Example ¶
package main import ( "context" "log" "go.etcd.io/etcd/clientv3" "gocloud.dev/runtimevar" "gocloud.dev/runtimevar/etcdvar" ) // MyConfig is a sample configuration struct. type MyConfig struct { Server string Port int } func main() { // Connect to the etcd server. client, err := clientv3.NewFromURL("http://your.etcd.server:9999") if err != nil { log.Fatal(err) } // Create a decoder for decoding JSON strings into MyConfig. decoder := runtimevar.NewDecoder(MyConfig{}, runtimevar.JSONDecode) // Construct a *runtimevar.Variable that watches the variable. // The etcd variable being referenced should have a JSON string that // decodes into MyConfig. v, err := etcdvar.New(client, "cfg-variable-name", decoder, nil) if err != nil { log.Fatal(err) } defer v.Close() // We can now read the current value of the variable from v. snapshot, err := v.Watch(context.Background()) if err != nil { log.Fatal(err) } cfg := snapshot.Value.(MyConfig) _ = cfg }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(cli *clientv3.Client, name string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)
New constructs a *runtimevar.Variable that uses client to watch the variable name on an etcd server. etcd returns raw bytes; provide a decoder to decode the raw bytes into the appropriate type for runtimevar.Snapshot.Value. See the runtimevar package documentation for examples of decoders.
Types ¶
Click to show internal directories.
Click to hide internal directories.