etcd/clientv3
Xiang Li 22065fe334 clientv3: fix mirror and add integration test 2016-03-13 18:27:45 -07:00
..
concurrency clientv3: include a context in Client 2016-03-04 09:20:44 -08:00
integration clientv3: fix mirror and add integration test 2016-03-13 18:27:45 -07:00
mirror clientv3: fix mirror and add integration test 2016-03-13 18:27:45 -07:00
README.md clientv3: document error handling in README 2016-02-29 15:41:52 -08:00
auth.go clientv3: a new interface Auth for auth related RPCs 2016-03-02 15:17:59 +09:00
client.go *: add client.defrag and defrag cmd for etcdctl 2016-03-10 09:30:41 -08:00
client_test.go clientv3: do not reconnect on request context cancellation 2016-03-03 13:43:16 -08:00
cluster.go clientv3: do not reconnect on request context cancellation 2016-03-03 13:43:16 -08:00
compare.go clientv3: make compare compliant with proposed txn usage 2016-02-08 13:48:29 -08:00
doc.go clientv3: document error handling 2016-02-29 13:54:35 -08:00
example_cluster_test.go clientv3: use default client cluster 2016-02-25 18:13:26 -08:00
example_kv_test.go clientv3: use default client kv 2016-02-25 18:13:26 -08:00
example_lease_test.go clientv3: use default client lease api 2016-02-25 18:13:26 -08:00
example_test.go clientv3: add IsProgressNotify with example 2016-03-04 12:16:43 -08:00
example_watch_test.go clientv3: add IsProgressNotify with example 2016-03-04 12:16:43 -08:00
kv.go clientv3: do not reconnect on request context cancellation 2016-03-03 13:43:16 -08:00
lease.go clientv3: do not reconnect on request context cancellation 2016-03-03 13:43:16 -08:00
maintenance.go *: add client.defrag and defrag cmd for etcdctl 2016-03-10 09:30:41 -08:00
op.go *: fix watch full key range 2016-03-05 14:45:43 -08:00
sort.go clientv3: fill in kv ops 2016-01-28 08:17:53 -08:00
txn.go clientv3: do not reconnect on request context cancellation 2016-03-03 13:43:16 -08:00
txn_test.go clientv3: optionize put and delete 2016-02-10 15:03:11 -08:00
watch.go Merge pull request #4689 from hongchaodeng/master 2016-03-04 16:09:16 -08:00

README.md

etcd/clientv3

Godoc

etcd/clientv3 is the official Go etcd client for v3.

Install

go get github.com/coreos/etcd/clientv3

Get started

Create client using clientv3.New:

cli, err := clientv3.New(clientv3.Config{
	Endpoints:   []string{"localhost:12378", "localhost:22378", "localhost:32378"},
	DialTimeout: 5 * time.Second,
})
if err != nil {
	// handle error!
}
defer cli.Close()

etcd v3 uses gRPC for remote procedure calls. And clientv3 uses grpc-go to connect to etcd. Make sure to close the client after using it. If the client is not closed, the connection will have leaky goroutines. To specify client request timeout, pass context.WithTimeout to APIs:

ctx, cancel := context.WithTimeout(context.Background(), timeout)
resp, err := kvc.Put(ctx, "sample_key", "sample_value")
cancel()
if err != nil {
    // handle error!
}
// use the response

Error Handling

etcd client returns 2 types of errors:

  1. context error: canceled or deadline exceeded.
  2. gRPC error: see v3rpc/error.

Here is the example code to handle client errors:

resp, err := kvc.Put(ctx, "", "")
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 verr, ok := err.(*v3rpc.ErrEmptyKey); ok {
		// process (verr.Errors)
	} else {
		// bad cluster endpoints, which are not etcd servers
	}
}

Examples

More code examples can be found at GoDoc.