etcd/client
Piotr Tabor de55bb6331 pkg: Rename imports after making 'pkg' a module
find -name '*.go' | xargs sed --follow-symlinks -i 's|go.etcd.io/etcd/v3/pkg/|go.etcd.io/etcd/pkg/v3/|g'
go fmt ./...
2020-10-13 00:09:27 +02:00
..
mock/mockserver Mechanical: Move mock packages out of ./pkg 2020-10-12 23:58:09 +02:00
README.md vendor: Get rid of ./vendor cleanup 2020-09-15 14:38:54 +02:00
auth_role.go *: replace 'golang.org/x/net/context' with 'context' 2017-09-07 13:39:42 -07:00
auth_user.go *: replace 'golang.org/x/net/context' with 'context' 2017-09-07 13:39:42 -07:00
cancelreq.go client: drop go1.4 tests 2016-03-18 18:44:56 -07:00
client.go *: Update references to code moved to the api/ dir. 2020-10-06 11:56:16 +02:00
client_test.go pkg: Rename imports after making 'pkg' a module 2020-10-13 00:09:27 +02:00
cluster_error.go client: improve error message for ClusterError 2016-06-22 13:13:12 -07:00
curl.go *: update LICENSE header 2016-05-12 20:51:48 -07:00
discover.go pkg: Rename imports after making 'pkg' a module 2020-10-13 00:09:27 +02:00
doc.go go.mod: use go.etcd.io/etcd/v3 versioning 2020-04-28 00:57:35 +00:00
example_keys_test.go tests: Make examples (for not client) to be both: documentation and integration-runnable 2020-10-07 15:38:40 +02:00
fake_transport_test.go *: update LICENSE header 2016-05-12 20:51:48 -07:00
json.go client: Switch to case sensitive unmarshalling to be compatible with ugorji 2019-04-23 16:54:44 -04:00
keys.go pkg: Rename imports after making 'pkg' a module 2020-10-13 00:09:27 +02:00
keys_bench_test.go client/keys_bench_test.go: Fix some misspells 2019-02-28 14:36:06 -05:00
keys_test.go client: Fix tests for latest golang 2019-04-22 08:14:10 -05:00
main_test.go pkg: Rename imports after making 'pkg' a module 2020-10-13 00:09:27 +02:00
members.go pkg: Rename imports after making 'pkg' a module 2020-10-13 00:09:27 +02:00
members_test.go pkg: Rename imports after making 'pkg' a module 2020-10-13 00:09:27 +02:00
util.go client: utility functions for getting detail of v2 auth errors 2016-07-31 21:23:58 +09:00

README.md

etcd/client

etcd/client is the Go client library for etcd.

GoDoc

For full compatibility, it is recommended to install released versions of clients using go modules.

Install

go get go.etcd.io/etcd/v3/client

Usage

package main

import (
	"log"
	"time"
	"context"

	"go.etcd.io/etcd/v3/client"
)

func main() {
	cfg := client.Config{
		Endpoints:               []string{"http://127.0.0.1:2379"},
		Transport:               client.DefaultTransport,
		// set timeout per request to fail fast when the target endpoint is unavailable
		HeaderTimeoutPerRequest: time.Second,
	}
	c, err := client.New(cfg)
	if err != nil {
		log.Fatal(err)
	}
	kapi := client.NewKeysAPI(c)
	// set "/foo" key with "bar" value
	log.Print("Setting '/foo' key with 'bar' value")
	resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
	if err != nil {
		log.Fatal(err)
	} else {
		// print common key info
		log.Printf("Set is done. Metadata is %q\n", resp)
	}
	// get "/foo" key's value
	log.Print("Getting '/foo' key value")
	resp, err = kapi.Get(context.Background(), "/foo", nil)
	if err != nil {
		log.Fatal(err)
	} else {
		// print common key info
		log.Printf("Get is done. Metadata is %q\n", resp)
		// print value
		log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
	}
}

Error Handling

etcd client might return three types of errors.

  • context error

Each API call has its first parameter as context. A context can be canceled or have an attached deadline. If the context is canceled or reaches its deadline, the responding context error will be returned no matter what internal errors the API call has already encountered.

  • cluster error

Each API call tries to send request to the cluster endpoints one by one until it successfully gets a response. If a requests to an endpoint fails, due to exceeding per request timeout or connection issues, the error will be added into a list of errors. If all possible endpoints fail, a cluster error that includes all encountered errors will be returned.

  • response error

If the response gets from the cluster is invalid, a plain string error will be returned. For example, it might be a invalid JSON error.

Here is the example code to handle client errors:

cfg := client.Config{Endpoints: []string{"http://etcd1:2379","http://etcd2:2379","http://etcd3:2379"}}
c, err := client.New(cfg)
if err != nil {
	log.Fatal(err)
}

kapi := client.NewKeysAPI(c)
resp, err := kapi.Set(ctx, "test", "bar", nil)
if err != nil {
	if err == context.Canceled {
		// ctx is canceled by another routine
	} else if err == context.DeadlineExceeded {
		// ctx is attached with a deadline and it exceeded
	} else if cerr, ok := err.(*client.ClusterError); ok {
		// process (cerr.Errors)
	} else {
		// bad cluster endpoints, which are not etcd servers
	}
}

Caveat

  1. etcd/client prefers to use the same endpoint as long as the endpoint continues to work well. This saves socket resources, and improves efficiency for both client and server side. This preference doesn't remove consistency from the data consumed by the client because data replicated to each etcd member has already passed through the consensus process.

  2. etcd/client does round-robin rotation on other available endpoints if the preferred endpoint isn't functioning properly. For example, if the member that etcd/client connects to is hard killed, etcd/client will fail on the first attempt with the killed member, and succeed on the second attempt with another member. If it fails to talk to all available endpoints, it will return all errors happened.

  3. Default etcd/client cannot handle the case that the remote server is SIGSTOPed now. TCP keepalive mechanism doesn't help in this scenario because operating system may still send TCP keep-alive packets. Over time we'd like to improve this functionality, but solving this issue isn't high priority because a real-life case in which a server is stopped, but the connection is kept alive, hasn't been brought to our attention.

  4. etcd/client cannot detect whether a member is healthy with watches and non-quorum read requests. If the member is isolated from the cluster, etcd/client may retrieve outdated data. Instead, users can either issue quorum read requests or monitor the /health endpoint for member health information.