README.md
1# etcd/client
2
3etcd/client is the Go client library for etcd.
4
5[![GoDoc](https://godoc.org/github.com/coreos/etcd/client?status.png)](https://godoc.org/github.com/coreos/etcd/client)
6
7## Install
8
9```bash
10go get github.com/coreos/etcd/client
11```
12
13## Usage
14
15```go
16package main
17
18import (
19 "log"
20 "time"
21
22 "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
23 "github.com/coreos/etcd/client"
24)
25
26func main() {
27 cfg := client.Config{
28 Endpoints: []string{"http://127.0.0.1:2379"},
29 Transport: client.DefaultTransport,
30 // set timeout per request to fail fast when the target endpoint is unavailable
31 HeaderTimeoutPerRequest: time.Second,
32 }
33 c, err := client.New(cfg)
34 if err != nil {
35 log.Fatal(err)
36 }
37 kapi := client.NewKeysAPI(c)
38 // set "/foo" key with "bar" value
39 log.Print("Setting '/foo' key with 'bar' value")
40 resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
41 if err != nil {
42 log.Fatal(err)
43 } else {
44 // print common key info
45 log.Printf("Set is done. Metadata is %q\n", resp)
46 }
47 // get "/foo" key's value
48 log.Print("Getting '/foo' key value")
49 resp, err = kapi.Get(context.Background(), "/foo", nil)
50 if err != nil {
51 log.Fatal(err)
52 } else {
53 // print common key info
54 log.Printf("Get is done. Metadata is %q\n", resp)
55 // print value
56 log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
57 }
58}
59```
60
61## Error Handling
62
63etcd client might return three types of errors.
64
65- context error
66
67Each API call has its first parameter as `context`. A context can be canceled or have an attached deadline. If the context is canceled or reaches its deadline, the responding context error will be returned no matter what internal errors the API call has already encountered.
68
69- cluster error
70
71Each API call tries to send request to the cluster endpoints one by one until it successfully gets a response. If a requests to an endpoint fails, due to exceeding per request timeout or connection issues, the error will be added into a list of errors. If all possible endpoints fail, a cluster error that includes all encountered errors will be returned.
72
73- response error
74
75If the response gets from the cluster is invalid, a plain string error will be returned. For example, it might be a invalid JSON error.
76
77Here is the example code to handle client errors:
78
79```go
80cfg := client.Config{Endpoints: []string{"http://etcd1:2379","http://etcd2:2379","http://etcd3:2379"}}
81c, err := client.New(cfg)
82if err != nil {
83 log.Fatal(err)
84}
85
86kapi := client.NewKeysAPI(c)
87resp, err := kapi.Set(ctx, "test", "bar", nil)
88if err != nil {
89 if err == context.Canceled {
90 // ctx is canceled by another routine
91 } else if err == context.DeadlineExceeded {
92 // ctx is attached with a deadline and it exceeded
93 } else if cerr, ok := err.(*client.ClusterError); ok {
94 // process (cerr.Errors)
95 } else {
96 // bad cluster endpoints, which are not etcd servers
97 }
98}
99```
100
101
102## Caveat
103
1041. etcd/client prefers to use the same endpoint as long as the endpoint continues to work well. This saves socket resources, and improves efficiency for both client and server side. This preference doesn't remove consistency from the data consumed by the client because data replicated to each etcd member has already passed through the consensus process.
105
1062. etcd/client does round-robin rotation on other available endpoints if the preferred endpoint isn't functioning properly. For example, if the member that etcd/client connects to is hard killed, etcd/client will fail on the first attempt with the killed member, and succeed on the second attempt with another member. If it fails to talk to all available endpoints, it will return all errors happened.
107
1083. Default etcd/client cannot handle the case that the remote server is SIGSTOPed now. TCP keepalive mechanism doesn't help in this scenario because operating system may still send TCP keep-alive packets. Over time we'd like to improve this functionality, but solving this issue isn't high priority because a real-life case in which a server is stopped, but the connection is kept alive, hasn't been brought to our attention.
109
1104. etcd/client cannot detect whether the member in use is healthy when doing read requests. If the member is isolated from the cluster, etcd/client may retrieve outdated data. As a workaround, users could monitor experimental /health endpoint for member healthy information. We are improving it at [#3265](https://github.com/coreos/etcd/issues/3265).
111