Documentation ¶
Overview ¶
Package etcd implements the store.Backend interface for the etcd
Index ¶
- Constants
- type Backend
- func (e *Backend) AbsKey(k string) string
- func (e *Backend) Client() *clientv3.Client
- func (e *Backend) Close() error
- func (e *Backend) Del(key string, ops ...store.DelOption) (int64, error)
- func (e *Backend) Get(key string, ops ...store.GetOption) ([]store.Entry, error)
- func (e *Backend) Health(key string) []Status
- func (e *Backend) JoinKey(args ...string) string
- func (e *Backend) KeyLeaf(key string) string
- func (e *Backend) Put(entry *store.Entry, ops ...store.PutOption) (bool, error)
- func (e *Backend) RelKey(k string) string
- func (e *Backend) SplitKey(key string) []string
- func (e *Backend) Watch(key string, w store.Watcher, ops ...store.WatchOption) error
- func (e *Backend) WatchChan(key string, channel interface{}, errChan chan error, ops ...store.WatchOption) (store.WatchStarter, error)
- type ChannelWatcher
- type Opt
- func WithAutoSyncInterval(interval time.Duration) Opt
- func WithCA(ca string) Opt
- func WithCAFile(path string) Opt
- func WithCert(cert string) Opt
- func WithCertFile(path string) Opt
- func WithClient(cli *clientv3.Client) Opt
- func WithContext(ctx context.Context) Opt
- func WithDialKeepAliveTime(t time.Duration) Opt
- func WithDialKeepAliveTimeout(t time.Duration) Opt
- func WithDialOptions(d ...grpc.DialOption) Opt
- func WithDialTimeout(timeout time.Duration) Opt
- func WithEndpoints(endpoints []string) Opt
- func WithErrorHandler(f store.ErrorFunc) Opt
- func WithKey(key string) Opt
- func WithKeyFile(path string) Opt
- func WithMaxCallRecvMsgSize(bytes int) Opt
- func WithPassword(password string) Opt
- func WithPrefix(p string) Opt
- func WithRequestTimeout(timeout time.Duration) Opt
- func WithSeparator(s rune) Opt
- func WithUsername(username string) Opt
- func WithWatchNotifyTimeout(timeout time.Duration) Opt
- type Status
Examples ¶
Constants ¶
const ( DfltSeparator = '/' DfltKeepAliveTTL = 30 * time.Second DfltWatchNotifyTimeout = 5 * time.Second )
Constants
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
Backend is uses etcd to store the data.
func New ¶
New configures a new etcd backend. At least Withclientv3 or WithEndoints option has to be used.
func (*Backend) AbsKey ¶
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) Health ¶
Health returns the status of the store etcd is missing some meaningful documentation
func (*Backend) JoinKey ¶
JoinKey returns a formatted key it joins the given elements with the correct delimiter
func (*Backend) RelKey ¶
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 ¶
SplitKey returns the key elements it splits the given key in its elements with the correct delimiter
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 ¶
Opt is a functional option to configure backend.
func WithAutoSyncInterval ¶
WithAutoSyncInterval is an option to configure the autosync interval.
func WithCAFile ¶
WithCAFile is an option to configure the tls ca certificate with the path to the ca file.
func WithCertFile ¶
WithCertFile is an option to configure the tls certificate with the path to the certificate file.
func WithClient ¶
WithClient is an option to configure the backend with a etcdv3.Client.
func WithContext ¶
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 ¶
WithDialKeepAliveTime is an option to configure the keepalive dial time.
func WithDialKeepAliveTimeout ¶
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 ¶
WithDialTimeout is an option to configure the dial timeout.
func WithEndpoints ¶
WithEndpoints is an option to configure etcd endpoints.
func WithErrorHandler ¶
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 WithKeyFile ¶
WithKeyFile is an option to configure the tls key with the path to the key file.
func WithMaxCallRecvMsgSize ¶ added in v0.3.2
WithMaxCallRecvMsgSize add the default call option grpc.MaxCallRecvMsgSize with the given size
func WithPassword ¶
WithPassword is an option to configure etcd password.
func WithPrefix ¶
WithPrefix is an option to set the global prefix used for the backend WithPrefix("global") results in keys prefixed "global" + separator
func WithRequestTimeout ¶
WithRequestTimeout is an option to configure the request timeout.
func WithSeparator ¶
WithSeparator is an option to overwrite the default separator for keys
func WithUsername ¶
WithUsername is an option to configure etcd username.
func WithWatchNotifyTimeout ¶ added in v0.3.0
WithWatchNotifyTimeout determines how long to wait for a watch notify create event. Default is 5s.