# README
etcd/clientv3
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:12378", "localhost:22378", "localhost:32378"},
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
Error Handling
etcd client returns 2 types of errors:
- context error: canceled or deadline exceeded.
- gRPC error: see v3rpc/error.
Here is the example code to handle client errors:
resp, err := kvc.Put(ctx, "", "")
if err != nil {
if err == context.Canceled {
// ctx is canceled by another routine
} else if err == context.DeadlineExceeded {
// ctx is attached with a deadline and it exceeded
} else if verr, ok := err.(*v3rpc.ErrEmptyKey); ok {
// process (verr.Errors)
} else {
// bad cluster endpoints, which are not etcd servers
}
}
Examples
More code examples can be found at GoDoc.
# Packages
Copyright 2016 CoreOS, Inc.
Package integration implements tests built upon embedded etcd, and focuses on correctness of etcd client.
# Functions
New creates a new etcdv3 client from a given configuration.
NewFromURL creates a new etcdv3 client from a URL.
WithFirstCreate gets the key with the oldest creation revision in the request range.
WithFirstKey gets the lexically first key in the request range.
WithFirstRev gets the key with the oldest modification revision in the request range.
WithFromKey specifies the range of 'Get' or 'Delete' requests to be equal or greater than they key in the argument.
WithLastCreate gets the key with the latest creation revision in the request range.
WithLastKey gets the lexically last key in the request range.
WithLastRev gets the key with the latest modification revision in the request range.
WithLease attaches a lease ID to a key in 'Put' request.
WithLimit limits the number of results to return from 'Get' request.
WithPrefix enables 'Get', 'Delete', or 'Watch' requests to operate on the keys with matching prefix.
WithProgressNotify makes watch server send periodic progress updates.
WithRange specifies the range of 'Get' or 'Delete' requests.
WithRev specifies the store revision for 'Get' request.
WithSerializable makes 'Get' request serializable.
WithSort specifies the ordering in 'Get' request.
# Constants
NoLease is a lease ID for the absence of a lease.
# Structs
Client provides and manages an etcd v3 client session.
Op represents an Operation that kv can execute.
# Type aliases
EndpointDialer is a policy for choosing which endpoint to dial next.
OpOption configures Operations like Get, Put, Delete.