etcd/client/v3
Gyuho Lee e2d67f2e3b
Merge pull request #12956 from gyuho/rename-to-main
*: rename "master" branch references to "main" in source code
2021-05-13 08:26:33 -07: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 *: partial staticcheck fix 2021-03-10 14:13:38 +00:00
leasing client/v3: Rename of imports 2020-10-20 10:13:06 +02:00
mirror client/v3: Rename of imports 2020-10-20 10:13:06 +02:00
mock/mockserver clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
namespace client/v3: Rename of imports 2020-10-20 10:13:06 +02:00
naming fix doc demo bug 2021-05-05 13:57:10 +08:00
ordering clientv3: Bring back ETCD_CLIENT_DEBUG variable interpretation. 2021-03-18 18:43:07 +01:00
snapshot client: Allow setting zap logger in config 2021-05-12 12:15:20 +02:00
yaml Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
auth.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
client_test.go client: Add logger argument to NewCtxClient 2021-05-12 16:40:55 +02:00
client.go client: Add logger argument to NewCtxClient 2021-05-12 16:40:55 +02:00
cluster.go Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
compact_op_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
compact_op.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 client: Allow setting zap logger in config 2021-05-12 12:15:20 +02:00
ctx_test.go *: partial staticcheck fix 2021-03-10 14:13:38 +00:00
ctx.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
doc.go client: rename "master" branch references to "main" 2021-05-12 10:38:50 -07: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 Add initial Tracing with OpenTelemetry 2021-05-10 10:44:40 +02:00
go.sum Add initial Tracing with OpenTelemetry 2021-05-10 10:44:40 +02:00
kv.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
lease.go *: over 20 staticcheck fixes 2021-03-18 15:06:17 +01:00
LICENSE client/v3: Create and update go.mod 2020-10-20 10:09:12 +02:00
logger.go Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
main_test.go Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
maintenance.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
op_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
op.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
options.go *: over 20 staticcheck fixes 2021-03-18 15:06:17 +01:00
README.md client: rename "master" branch references to "main" 2021-05-12 10:38:50 -07:00
retry_interceptor.go Clientv3 (retry) logs should use the configured logger. 2021-04-29 12:57:09 +02:00
retry.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
sort.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
txn_test.go Migrate dependencies pkg/foo -> client/pkg/foo 2021-04-07 00:38:47 +02:00
txn.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
utils.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
watch_test.go clientv3: Move to client/v3 (just file move) 2020-10-20 10:08:35 +02:00
watch.go check resuming len to avoid panic 2021-05-12 09:51:59 +08:00

etcd/clientv3

Docs Godoc

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

Install

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

Warning: As etcd 3.5.0 was not yet released, the command above does not work. After first pre-release of 3.5.0 #12498, etcd can be referenced using:

go get go.etcd.io/etcd/client/v3@v3.5.0-pre

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 := 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.