etcd/client/v3
Benjamin Wang 7f10dccbaf Bump go 1.19: update all the dependencies and go.sum files
1. run ./scripts/fix.sh;
2. cd tools/mod; gofmt -w . & go mod tidy;

Signed-off-by: Benjamin Wang <wachao@vmware.com>
2022-09-22 08:47:46 +08:00
..
clientv3util client/v3: Rename of imports 2020-10-20 10:13:06 +02:00
concurrency Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
credentials clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
experimental/recipes Make test.sh scripts OSX/BSD compatible: 2021-01-12 16:29:00 +01:00
internal Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
leasing Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
mirror cv3/mirror: Fetch the most recent prefix revision 2022-04-11 13:42:03 +00:00
mock/mockserver Mockserver can handle requests to the leases API 2022-09-06 18:48:51 +03:00
namespace Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
naming Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
ordering Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
snapshot Update client/v3/snapshot/v3_snapshot.go 2022-04-08 18:44:50 +01:00
yaml *: move from io/ioutil to io and os packages 2021-10-28 00:05:28 +08:00
LICENSE client/v3: Create and update go.mod 2020-10-20 10:09:12 +02:00
README.md client: README: update to new go.mod paths 2021-06-15 19:47:22 -07:00
auth.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
client.go more logging around auto sync 2022-08-10 18:00:06 -07:00
client_test.go fix #14161 2022-06-25 10:09:05 +00:00
cluster.go Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
compact_op.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
compact_op_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
compare.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
config.go clientv3: fix --insecure-skip-tls-verify not working when not specify --cert-path and --key-path 2022-08-08 16:24:12 +08:00
config_test.go clientv3: fix --insecure-skip-tls-verify not working when not specify --cert-path and --key-path 2022-08-08 16:24:12 +08:00
ctx.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
ctx_test.go *: partial staticcheck fix 2021-03-10 14:13:38 +00:00
doc.go Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
example_auth_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_cluster_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_kv_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_lease_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_maintenance_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_metrics_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
example_watch_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
go.mod Bump go 1.19: upgrade go version to 1.19 in all go.mod files 2022-09-22 08:47:46 +08:00
go.sum Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
kv.go etcd: format import order 2022-09-20 18:41:39 +08:00
lease.go clientv3: close streams after use in lessor keepAliveOnce method 2022-08-17 18:02:13 -07:00
logger.go Improve error message for incorrect values of ETCD_CLIENT_DEBUG 2022-07-13 18:55:51 +02:00
main_test.go Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
maintenance.go client: Alias downgrade action enum 2022-02-21 17:58:20 +01:00
op.go Client: fix check for WithPrefix op 2022-06-29 17:52:05 -04:00
op_test.go Client: fix check for WithPrefix op 2022-06-29 17:52:05 -04:00
options.go *: over 20 staticcheck fixes 2021-03-18 15:06:17 +01:00
retry.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
retry_interceptor.go doc: Fix Markdown code block 2022-04-25 15:06:38 +08:00
retry_interceptor_test.go etcd: format import order 2022-09-20 18:41:39 +08:00
sort.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
txn.go Bump go 1.19: update all the dependencies and go.sum files 2022-09-22 08:47:46 +08:00
txn_test.go fix potential goroutine leaks in TestTxnPanics 2021-12-15 01:22:56 -05:00
utils.go fix IsOptsWithFromKey 2021-09-10 00:44:52 +08:00
watch.go *: handle auth invalid token and old revision errors in watch 2022-09-17 21:51:36 +09:00
watch_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00

README.md

etcd/client/v3

Docs Godoc

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

Install

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

Get started

Create client using clientv3.New:

import clientv3 "go.etcd.io/etcd/client/v3"

func main() {
	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 := cli.Put(ctx, "sample_key", "sample_value")
cancel()
if err != nil {
    // handle error!
}
// use the response

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

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 := cli.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)
	}
}

Metrics

The etcd client optionally exposes RPC metrics through go-grpc-prometheus. See the examples.

Namespacing

The namespace package provides clientv3 interface wrappers to transparently isolate client requests to a user-defined prefix.

Request size limit

Client request size limit is configurable via clientv3.Config.MaxCallSendMsgSize and MaxCallRecvMsgSize in bytes. If none given, client request send limit defaults to 2 MiB including gRPC overhead bytes. And receive limit defaults to math.MaxInt32.

Examples

More code examples can be found at GoDoc.