etcd/clientv3
Anthony Romano 08f6c0775a Merge pull request #5199 from heyitsanthony/safe-lock-retry
clientv3/concurrency: use session lease id for mutex keys
2016-04-27 11:10:46 -07:00
..
concurrency clientv3/concurrency: use session lease id for mutex keys 2016-04-26 17:37:26 -07:00
integration etcdserver: respond with ttl=0 for revoked lease keep alive 2016-04-26 13:53:20 -07:00
mirror *: migrate Godeps to vendor/ 2016-03-22 17:10:28 -07:00
README.md *: client vendoring README 2016-04-08 19:48:17 -07:00
auth.go *: support authenticate in v3 auth 2016-04-21 12:32:19 +09:00
client.go clientv3: support read conf from file 2016-04-01 09:36:11 -07:00
client_test.go *: migrate Godeps to vendor/ 2016-03-22 17:10:28 -07:00
cluster.go *: remove MemberLeader API in client side (fix examples) 2016-04-13 16:23:57 -07:00
compare.go clientv3: rename comparison from CreatedRevision to CreateRevision 2016-03-23 09:50:46 -07:00
config.go clientv3: make YamlConfig struct private 2016-04-14 12:26:01 -07:00
config_test.go clientv3: make YamlConfig struct private 2016-04-14 12:26:01 -07:00
doc.go client: doc that client is thread-safe 2016-03-29 09:28:53 -07:00
example_cluster_test.go test, clientv3: run examples as integration tests 2016-04-15 11:51:30 -07:00
example_kv_test.go test, clientv3: run examples as integration tests 2016-04-15 11:51:30 -07:00
example_lease_test.go test, clientv3: run examples as integration tests 2016-04-15 11:51:30 -07:00
example_maintenence_test.go *: add more examples to clientv3, pkg/adt 2016-04-14 23:46:50 -07:00
example_test.go *: remove MemberLeader API in client side (fix examples) 2016-04-13 16:23:57 -07:00
example_watch_test.go *: add more examples to clientv3, pkg/adt 2016-04-14 23:46:50 -07:00
kv.go clientv3: fix race in KV reconnection logic 2016-03-28 16:08:18 -07:00
lease.go Merge pull request #5203 from heyitsanthony/fix-lease-leak 2016-04-26 20:42:31 -07:00
logger.go clientv3: fix race when setting grpc Logger 2016-03-28 23:30:03 -07:00
main_test.go test, clientv3: run examples as integration tests 2016-04-15 11:51:30 -07:00
maintenance.go etcdctl: move endpoint-heath and status into endpoint command 2016-04-12 16:30:26 -07:00
op.go *: clean up if, bool comparison 2016-04-02 12:55:11 -07:00
sort.go clientv3: Renaming SortByCreatedRev to maintain consistency. 2016-03-22 17:56:24 -07:00
txn.go *: clean up from go vet, misspell 2016-04-10 23:16:56 -07:00
txn_test.go test, clientv3: run examples as integration tests 2016-04-15 11:51:30 -07:00
watch.go *: rename storage package to mvcc 2016-04-25 15:25:51 -07:00
watch_test.go *: rename storage package to mvcc 2016-04-25 15:25:51 -07: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:2379", "localhost:22379", "localhost:32379"},
	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

etcd uses cmd/vendor directory to store external dependencies, which are to be compiled into etcd release binaries. client can be imported without vendoring. For full compatibility, it is recommended to vendor builds using etcd's vendored packages, using tools like godep, as in vendor directories. For more detail, please read Go vendor design.

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.