etcd: Index | Files

package namespace

import ""

Package namespace is a clientv3 wrapper that translates all keys to begin with a given prefix.

First, create a client:

cli, err := clientv3.New(clientv3.Config{Endpoints: []string{"localhost:2379"}})
if err != nil {
	// handle error!

Next, override the client interfaces:

unprefixedKV := cli.KV
cli.KV = namespace.NewKV(cli.KV, "my-prefix/")
cli.Watcher = namespace.NewWatcher(cli.Watcher, "my-prefix/")
cli.Lease = namespace.NewLease(cli.Lease, "my-prefix/")

Now calls using 'cli' will namespace / prefix all keys with "my-prefix/":

cli.Put(context.TODO(), "abc", "123")
resp, _ := unprefixedKV.Get(context.TODO(), "my-prefix/abc")
fmt.Printf("%s\n", resp.Kvs[0].Value)
// Output: 123
unprefixedKV.Put(context.TODO(), "my-prefix/abc", "456")
resp, _ = cli.Get(context.TODO(), "abc")
fmt.Printf("%s\n", resp.Kvs[0].Value)
// Output: 456


Package Files

doc.go kv.go lease.go util.go watch.go

func NewKV Uses

func NewKV(kv clientv3.KV, prefix string) clientv3.KV

NewKV wraps a KV instance so that all requests are prefixed with a given string.

func NewLease Uses

func NewLease(l clientv3.Lease, prefix string) clientv3.Lease

NewLease wraps a Lease interface to filter for only keys with a prefix and remove that prefix when fetching attached keys through TimeToLive.

func NewWatcher Uses

func NewWatcher(w clientv3.Watcher, prefix string) clientv3.Watcher

NewWatcher wraps a Watcher instance so that all Watch requests are prefixed with a given string and all Watch responses have the prefix removed.

Package namespace imports 6 packages (graph) and is imported by 126 packages. Updated 2020-04-30. Refresh now. Tools for package owners.