etcd/client/v3
2025-01-30 08:43:53 -05:00
..
clientv3util all: goimports -w . 2022-11-17 19:07:04 +03:00
concurrency fix: enable gofumpt instead of gofmt linter in client 2024-10-17 22:29:59 +02:00
credentials fix: enable expected-actual rule from testifylint 2024-10-17 09:17:59 +00:00
experimental/recipes fix: enable errorlint in api, client and pkg 2024-10-13 19:43:20 +02:00
internal client: address golangci var-naming issues 2024-03-14 20:47:32 -07:00
kubernetes Fix documentation of List method of kubernetes interface 2025-01-28 09:30:54 +01:00
leasing fix: enable errorlint in api, client and pkg 2024-10-13 19:43:20 +02:00
mirror fix: enable gofumpt instead of gofmt linter in client 2024-10-17 22:29:59 +02:00
mock/mockserver fix: enable errorlint in api, client and pkg 2024-10-13 19:43:20 +02: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 clientv3test: add comments for clientv3test 2023-11-15 08:25:39 -05:00
snapshot Prevent leaking file descriptor during snapshotting and provide better logging of errors 2025-01-17 10:41:26 +01:00
yaml fix: use require.NoError instead of t.Fatal(err) in client package 2024-10-09 08:29:38 +02:00
auth.go Introduce Kubernetes KV interface to etcd client 2024-07-23 14:53:09 +02:00
client_test.go fix: use testify instead of t.Fatal or t.Error in client package (part 1) 2024-12-11 07:58:17 +01:00
client.go Merge pull request #18748 from mmorel-35/golangci-lint/gofumpt 2024-10-25 12:25:33 +01:00
cluster.go Introduce Kubernetes KV interface to etcd client 2024-07-23 14:53:09 +02:00
compact_op_test.go fix: use testify instead of t.Fatal or t.Error in client package (part 1) 2024-12-11 07:58:17 +01:00
compact_op.go
compare.go fix: enable gofumpt instead of gofmt linter in client 2024-10-17 22:29:59 +02:00
config_test.go update epHealthCommandFunc to reuse clientConfigFromCmd 2024-10-15 09:48:12 +01:00
config.go feat(etcdctl): add --max-send-bytes and --max-recv-bytes options 2024-10-16 19:54:41 +08:00
ctx_test.go fix: use testify instead of t.Fatal or t.Error in client package (part 1) 2024-12-11 07:58:17 +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 Switch from grpc-ecosystem/go-grpc-prometheus to grpc-ecosystem/go-grpc-middleware/providers/prometheus 2025-01-30 08:43:53 -05:00
go.sum Switch from grpc-ecosystem/go-grpc-prometheus to grpc-ecosystem/go-grpc-middleware/providers/prometheus 2025-01-30 08:43:53 -05:00
kv.go fix: enable gofumpt instead of gofmt linter in client 2024-10-17 22:29:59 +02:00
lease.go fix runtime error: comparing uncomparable type 2024-11-22 22:13:53 +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: enable errorlint in api, client and pkg 2024-10-13 19:43:20 +02: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
OWNERS Automate labels for clientv3 pull requests. 2024-05-12 22:24:21 +12: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 Merge pull request #18748 from mmorel-35/golangci-lint/gofumpt 2024-10-25 12:25:33 +01:00
retry.go Merge pull request #18748 from mmorel-35/golangci-lint/gofumpt 2024-10-25 12:25:33 +01:00
sort.go fix: enable gofumpt instead of gofmt linter in client 2024-10-17 22:29:59 +02:00
txn_test.go tests: use separate errc for each case in TestTxnPanics 2023-04-10 09:16:39 +08:00
txn.go Introduce Kubernetes KV interface to etcd client 2024-07-23 14:53:09 +02:00
utils.go fix IsOptsWithFromKey 2021-09-10 00:44:52 +08:00
watch_test.go client/v3/watch_test.go: test fmt metadata print 2024-07-10 15:33:54 +03:00
watch.go Merge pull request #18748 from mmorel-35/golangci-lint/gofumpt 2024-10-25 12:25:33 +01: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.