etcd

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package etcd implements the store.Backend interface for the etcd

Index

Examples

Constants

View Source
const (
	DfltSeparator          = '/'
	DfltKeepAliveTTL       = 30 * time.Second
	DfltWatchNotifyTimeout = 5 * time.Second
)

Constants

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend struct {
	RequestTimeout time.Duration
	// contains filtered or unexported fields
}

Backend is uses etcd to store the data.

func New

func New(opts ...Opt) (*Backend, error)

New configures a new etcd backend. At least Withclientv3 or WithEndoints option has to be used.

func (*Backend) AbsKey

func (e *Backend) AbsKey(k string) string

AbsKey will convert a relativ key in a absolute key AbsKey("a/b") with prefix "root" and separator '/' returns "root/a/b" AbsKey does not validate the given key. Given a faulty relative key returns a faulty absolute key.

func (*Backend) Client

func (e *Backend) Client() *clientv3.Client

Client returns the underlying client.Client.

func (*Backend) Close

func (e *Backend) Close() error

Close closes the etcd connection.

func (*Backend) Del

func (e *Backend) Del(key string, ops ...store.DelOption) (int64, error)

Del is used to permanently delete an entry

func (*Backend) Get

func (e *Backend) Get(key string, ops ...store.GetOption) ([]store.Entry, error)

Get is used to fetch an one ore many entries.

func (*Backend) Health

func (e *Backend) Health(key string) []Status

Health returns the status of the store etcd is missing some meaningful documentation

func (*Backend) JoinKey

func (e *Backend) JoinKey(args ...string) string

JoinKey returns a formatted key it joins the given elements with the correct delimiter

func (*Backend) KeyLeaf

func (e *Backend) KeyLeaf(key string) string

KeyLeaf returns the leave (last) element of a key

func (*Backend) Put

func (e *Backend) Put(entry *store.Entry, ops ...store.PutOption) (bool, error)

Put is used to insert or update an entry

func (*Backend) RelKey

func (e *Backend) RelKey(k string) string

RelKey will convert an absolute key in a relativ key RelKey("root/a/b") with prefix "root" and separator '/' returns "a/b" RelKey does not validate the given key. Given a faulty absolute key returns a faulty relativ key.

func (*Backend) SplitKey

func (e *Backend) SplitKey(key string) []string

SplitKey returns the key elements it splits the given key in its elements with the correct delimiter

func (*Backend) Watch

func (e *Backend) Watch(key string, w store.Watcher, ops ...store.WatchOption) error

Watch installs a watcher on key

func (*Backend) WatchChan added in v0.3.0

func (e *Backend) WatchChan(key string, channel interface{}, errChan chan error, ops ...store.WatchOption) (store.WatchStarter, error)

WatchChan creates a watcher for a key or prefix and unmarshals events into channel. The channel elements have to implement the store.KeyOpSetter interface.

type ChannelWatcher added in v0.3.0

type ChannelWatcher struct {
	// contains filtered or unexported fields
}

ChannelWatcher implements the WatchStarter interface.

func (*ChannelWatcher) Start added in v0.3.0

func (c *ChannelWatcher) Start()

Start starts the watcher and blocks until context is canceled.

type Opt

type Opt func(*Backend) error

Opt is a functional option to configure backend.

func WithAutoSyncInterval

func WithAutoSyncInterval(interval time.Duration) Opt

WithAutoSyncInterval is an option to configure the autosync interval.

func WithCA

func WithCA(ca string) Opt

WithCA is an option to configure the tls ca certificate.

func WithCAFile

func WithCAFile(path string) Opt

WithCAFile is an option to configure the tls ca certificate with the path to the ca file.

func WithCert

func WithCert(cert string) Opt

WithCert is an option to configure the tls certificate.

func WithCertFile

func WithCertFile(path string) Opt

WithCertFile is an option to configure the tls certificate with the path to the certificate file.

func WithClient

func WithClient(cli *clientv3.Client) Opt

WithClient is an option to configure the backend with a etcdv3.Client.

func WithContext

func WithContext(ctx context.Context) Opt

WithContext is an option to set the default client context. It can be used to cancel grpc dial out and other operations that do not have an explicit context.

func WithDialKeepAliveTime

func WithDialKeepAliveTime(t time.Duration) Opt

WithDialKeepAliveTime is an option to configure the keepalive dial time.

func WithDialKeepAliveTimeout

func WithDialKeepAliveTimeout(t time.Duration) Opt

WithDialKeepAliveTimeout is an option to configure the keepalive dial timeout.

func WithDialOptions

func WithDialOptions(d ...grpc.DialOption) Opt

WithDialOptions is a function that sets the underlying grpc dial options.

Example
package main

import (
	"log"

	"github.com/postfinance/store/etcd"
	"google.golang.org/grpc"
)

func main() {
	e, err := etcd.New(etcd.WithDialOptions(grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(1024 * 1024 * 8))))
	if err != nil {
		log.Fatal(err)
	}

	_, _ = e.Get("/key")
}
Output:

func WithDialTimeout

func WithDialTimeout(timeout time.Duration) Opt

WithDialTimeout is an option to configure the dial timeout.

func WithEndpoints

func WithEndpoints(endpoints []string) Opt

WithEndpoints is an option to configure etcd endpoints.

func WithErrorHandler

func WithErrorHandler(f store.ErrorFunc) Opt

WithErrorHandler is a function that can be used to handle etcd server errors. The default error handler does nothing.

Example
package main

import (
	"log"

	"github.com/pkg/errors"
	"github.com/postfinance/store/etcd"
	"go.etcd.io/etcd/api/v3/v3rpc/rpctypes"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func main() {
	handleError := func(err error) error {
		fatal := func(err error, msg string) {
			panic(errors.Wrap(err, msg))
		}
		// handle grpc error if code is available
		// https://github.com/grpc/grpc-go/blob/master/codes/codes.go
		switch status.Code(err) {
		case codes.Unauthenticated:
			fatal(err, "grpc Unauthenticated error - terminating")
		case codes.ResourceExhausted:
			fatal(err, "grpc ResourceExhausted error - terminating")
		case codes.Internal:
			fatal(err, "grpc Internal error - terminating")
		case codes.Unavailable:
			fatal(err, "grpc Unavailable error - terminating")
		}
		// handle etcd server-side errors
		// https://github.com/etcd-io/etcd/blob/api/v3.5.0/api/v3rpc/rpctypes/error.go
		switch err {
		case rpctypes.ErrNoSpace,
			rpctypes.ErrTooManyRequests: // codes.ResourceExhausted
			fatal(err, "terminating")
		case rpctypes.ErrInvalidAuthToken: // codes.Unauthenticated
			fatal(err, "terminating")
		case rpctypes.ErrNoLeader,
			rpctypes.ErrNotCapable,
			rpctypes.ErrStopped,
			rpctypes.ErrTimeout,
			rpctypes.ErrTimeoutDueToLeaderFail,
			rpctypes.ErrTimeoutDueToConnectionLost,
			rpctypes.ErrUnhealthy: // codes.Unavailable
			fatal(err, "terminating")
		}

		return err
	}

	e, err := etcd.New(etcd.WithErrorHandler(handleError))
	if err != nil {
		log.Fatal(err)
	}

	_, _ = e.Get("/key")
}
Output:

func WithKey

func WithKey(key string) Opt

WithKey is an option to configure the tls key.

func WithKeyFile

func WithKeyFile(path string) Opt

WithKeyFile is an option to configure the tls key with the path to the key file.

func WithMaxCallRecvMsgSize added in v0.3.2

func WithMaxCallRecvMsgSize(bytes int) Opt

WithMaxCallRecvMsgSize add the default call option grpc.MaxCallRecvMsgSize with the given size

func WithPassword

func WithPassword(password string) Opt

WithPassword is an option to configure etcd password.

func WithPrefix

func WithPrefix(p string) Opt

WithPrefix is an option to set the global prefix used for the backend WithPrefix("global") results in keys prefixed "global" + separator

func WithRequestTimeout

func WithRequestTimeout(timeout time.Duration) Opt

WithRequestTimeout is an option to configure the request timeout.

func WithSeparator

func WithSeparator(s rune) Opt

WithSeparator is an option to overwrite the default separator for keys

func WithUsername

func WithUsername(username string) Opt

WithUsername is an option to configure etcd username.

func WithWatchNotifyTimeout added in v0.3.0

func WithWatchNotifyTimeout(timeout time.Duration) Opt

WithWatchNotifyTimeout determines how long to wait for a watch notify create event. Default is 5s.

type Status

type Status struct {
	Endpoint string `json:"endpoint"`
	Healthy  bool   `json:"healthy"`
	Detail   string `json:"detail"`
	// contains filtered or unexported fields
}

Status represents the status of the store

Jump to

Keyboard shortcuts

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