etcd/client/v3
James Blair d2af596a98
depdendency: bump github.com/prometheus/common from 0.44.0 to 0.45.0.
Signed-off-by: James Blair <mail@jamesblair.net>
2023-10-24 17:44:01 +13:00
..
clientv3util all: goimports -w . 2022-11-17 19:07:04 +03:00
concurrency feat: enable unparam lint 2023-10-17 21:24:13 +08:00
credentials clientv3: remove the experimental gRPC API grpccredentials.Bundle 2023-08-02 19:35:51 +01:00
experimental/recipes disable staticcheck for DoubleBarrier.Enter 2023-09-21 17:39:44 +08:00
internal endpoints.Interpret returns Host:port as ServerName 2023-07-28 16:19:54 -07:00
leasing *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
mirror cv3/mirror: Fetch the most recent prefix revision 2022-04-11 13:42:03 +00:00
mock/mockserver *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
namespace all: goimports -w . 2022-11-17 19:07:04 +03:00
naming *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
ordering *: fix stylecheck linter 2023-09-25 19:14:01 +08:00
snapshot clientv3: refactor snapshot SaveWithVersion 2023-05-22 09:09:02 +08:00
yaml all: goimports -w . 2022-11-17 19:07:04 +03:00
auth.go Fix goimports in all existing files. Execution of ./scripts/fix.sh 2022-12-29 09:41:31 +01:00
client_test.go *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
client.go feat: enable unparam lint 2023-10-17 21:24:13 +08:00
cluster.go *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
compact_op_test.go
compact_op.go
compare.go Use any instead of interface{} 2023-09-17 17:41:58 +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
config.go docs: fix max recv msg size description 2022-11-17 10:37:55 +08:00
ctx_test.go Goimports: Apply automated fixing to test files as well. 2022-12-29 13:04:45 +01:00
ctx.go Fix goimports in all existing files. Execution of ./scripts/fix.sh 2022-12-29 09:41:31 +01: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
example_cluster_test.go
example_kv_test.go
example_lease_test.go
example_maintenance_test.go
example_metrics_test.go
example_test.go
example_watch_test.go
go.mod depdendency: bump github.com/prometheus/common from 0.44.0 to 0.45.0. 2023-10-24 17:44:01 +13:00
go.sum depdendency: bump github.com/prometheus/common from 0.44.0 to 0.45.0. 2023-10-24 17:44:01 +13:00
kv.go etcd: format import order 2022-09-20 18:41:39 +08:00
lease.go *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
LICENSE
logger.go Fix goimports in all existing files. Execution of ./scripts/fix.sh 2022-12-29 09:41:31 +01:00
main_test.go feat: enable unparam lint 2023-10-17 21:24:13 +08:00
maintenance.go *: fix staticcheck lint 2023-09-21 11:24:26 +08:00
op_test.go expose op.isOptsWithFromKey and op.isOptsWithPrefix 2023-07-11 14:34:51 -05:00
op.go expose op.isOptsWithFromKey and op.isOptsWithPrefix 2023-07-11 14:34:51 -05:00
options.go client/v3: fix comment typo 2023-01-07 14:40:12 +08:00
README.md client: README: update to new go.mod paths 2021-06-15 19:47:22 -07:00
retry_interceptor_test.go clientv3: remove the experimental gRPC API grpccredentials.Bundle 2023-08-02 19:35:51 +01:00
retry_interceptor.go feat: enable unparam lint 2023-10-17 21:24:13 +08:00
retry.go feat: enable unparam lint 2023-10-17 21:24:13 +08:00
sort.go
txn_test.go tests: use separate errc for each case in TestTxnPanics 2023-04-10 09:16:39 +08:00
txn.go *: enable goimports in verify-lint 2023-09-21 21:14:09 +08:00
utils.go fix IsOptsWithFromKey 2021-09-10 00:44:52 +08:00
watch_test.go client: enhance the function shouldRetryWatch and added unit test 2022-12-13 06:05:02 +08:00
watch.go clientv3: correct the nextRev on receving progress notification response 2023-02-10 09:09:19 +08:00

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.