etcd/clientv3
Gyu-Ho Lee 0c5bcd5d80 clientv3: fix README, add error handling example 2016-04-29 13:34:16 -07:00
..
concurrency clientv3/concurrency: use session lease id for mutex keys 2016-04-26 17:37:26 -07:00
integration clientv3/integration: tests with new errors 2016-04-29 12:00:26 -07:00
mirror *: migrate Godeps to vendor/ 2016-03-22 17:10:28 -07:00
README.md clientv3: fix README, add error handling example 2016-04-29 13:34:16 -07:00
auth.go clientv3: auth with rpctypes.Error 2016-04-29 12:00:26 -07:00
client.go clientv3: convert errors to rpctypes on returning 2016-04-28 15:39:37 -07:00
client_test.go clientv3: convert errors to rpctypes on returning 2016-04-28 15:39:37 -07:00
cluster.go clientv3: convert errors to rpctypes on returning 2016-04-28 15:39:37 -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 clientv3: fix README, add error handling example 2016-04-29 13:34:16 -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: better serialization for kv and txn connection retry 2016-04-29 09:26:32 -07:00
lease.go clientv3: convert errors to rpctypes on returning 2016-04-28 15:39:37 -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 clientv3: convert errors to rpctypes on returning 2016-04-28 15:39:37 -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 clientv3: better serialization for kv and txn connection retry 2016-04-29 09:26:32 -07:00
txn_test.go test, clientv3: run examples as integration tests 2016-04-15 11:51:30 -07:00
watch.go clientv3: convert errors to rpctypes on returning 2016-04-28 15:39:37 -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 api/v3rpc/rpctypes.

Here is the example code to handle client errors:

resp, err := kvc.Put(ctx, "", "")
if err != nil {
	switch err {
	case context.Canceled:
		log.Fatalf("ctx is canceled by another routine: %v", err)
	case context.DeadlineExceeded:
		log.Fatalf("ctx is attached with a deadline is exceeded: %v", err)
	case rpctypes.ErrEmptyKey:
		log.Fatalf("client-side error: %v", err)
	default:
		log.Fatalf("bad cluster endpoints, which are not etcd servers: %v", err)
	}
}

Examples

More code examples can be found at GoDoc.