etcdvar

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2019 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package etcdvar provides a runtimevar implementation with variables backed by etcd. Use OpenVariable to construct a *runtimevar.Variable.

URLs

For runtimevar.OpenVariable, etcdvar registers for the scheme "etcd". The default URL opener will dial an etcd server based on the environment variable "ETCD_SERVER_URL". To customize the URL opener, or for more details on the URL format, see URLOpener. See https://godoc.org/gocloud.dev#hdr-URLs for background information.

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.OpenVariable(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.Latest(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	cfg := snapshot.Value.(MyConfig)
	_ = cfg
}
Output:

Example (OpenVariable)
package main

import (
	"context"
	"log"

	"gocloud.dev/runtimevar"
)

func main() {
	// OpenVariable creates a *runtimevar.Variable from a URL.
	// The default opener connects to an etcd server based on the environment
	// variable ETCD_SERVER_URL.
	ctx := context.Background()
	v, err := runtimevar.OpenVariable(ctx, "etcd://myvarname")
	if err != nil {
		log.Fatal(err)
	}

	snapshot, err := v.Latest(ctx)
	_, _ = snapshot, err
}
Output:

Index

Examples

Constants

View Source
const Scheme = "etcd"

Scheme is the URL scheme etcdvar registers its URLOpener under on runtimevar.DefaultMux.

Variables

This section is empty.

Functions

func OpenVariable added in v0.13.0

func OpenVariable(cli *clientv3.Client, name string, decoder *runtimevar.Decoder, opts *Options) (*runtimevar.Variable, error)

OpenVariable 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

type Options

type Options struct {
	// Timeout controls the timeout on RPCs to etcd; timeouts will result in
	// errors being returned from Watch. Defaults to 30 seconds.
	Timeout time.Duration
}

Options sets options.

type URLOpener added in v0.12.0

type URLOpener struct {
	// The Client to use; required.
	Client *clientv3.Client

	// Decoder specifies the decoder to use if one is not specified in the URL.
	// Defaults to runtimevar.BytesDecoder.
	Decoder *runtimevar.Decoder

	// Options specifies the options to pass to OpenVariable.
	Options Options
}

URLOpener opens etcd URLs like "etcd://mykey?decoder=string".

The host+path is used as the variable name.

The following URL parameters are supported:

  • decoder: The decoder to use. Defaults to runtimevar.BytesDecoder. See runtimevar.DecoderByName for supported values.

func (*URLOpener) OpenVariableURL added in v0.12.0

func (o *URLOpener) OpenVariableURL(ctx context.Context, u *url.URL) (*runtimevar.Variable, error)

OpenVariableURL opens a etcdvar Variable for u.

Jump to

Keyboard shortcuts

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