• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..16-Jul-2020-

integration/H16-Jul-2020-172117

README.mdH A D16-Jul-20204.7 KiB11890

auth_role.goH A D16-Jul-20205.7 KiB237187

auth_user.goH A D16-Jul-20207.6 KiB320249

cancelreq.goH A D16-Jul-2020405 199

client.goH A D16-Jul-202017.8 KiB711498

client_test.goH A D16-Jul-202031.2 KiB1,075900

cluster_error.goH A D16-Jul-2020981 3819

curl.goH A D16-Jul-20201.5 KiB7141

discover.goH A D16-Jul-20201.2 KiB4118

doc.goH A D16-Jul-20201.8 KiB741

example_keys_test.goH A D16-Jul-20202.2 KiB9458

fake_transport_test.goH A D16-Jul-20201.1 KiB4121

json.goH A D16-Jul-20202 KiB7348

keys.goH A D16-Jul-202018.9 KiB681419

keys_bench_test.goH A D16-Jul-20202.3 KiB8862

keys_test.goH A D16-Jul-202031.1 KiB1,4291,229

main_test.goH A D16-Jul-20202.1 KiB7854

members.goH A D16-Jul-20207.1 KiB304216

members_test.goH A D16-Jul-202014 KiB599497

util.goH A D16-Jul-20201.5 KiB5430

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
7etcd uses `cmd/vendor` directory to store external dependencies, which are
8to be compiled into etcd release binaries. `client` can be imported without
9vendoring. For full compatibility, it is recommended to vendor builds using
10etcd's vendored packages, using tools like godep, as in
11[vendor directories](https://golang.org/cmd/go/#hdr-Vendor_Directories).
12For more detail, please read [Go vendor design](https://golang.org/s/go15vendor).
13
14## Install
15
16```bash
17go get github.com/coreos/etcd/client
18```
19
20## Usage
21
22```go
23package main
24
25import (
26	"log"
27	"time"
28	"context"
29
30	"github.com/coreos/etcd/client"
31)
32
33func main() {
34	cfg := client.Config{
35		Endpoints:               []string{"http://127.0.0.1:2379"},
36		Transport:               client.DefaultTransport,
37		// set timeout per request to fail fast when the target endpoint is unavailable
38		HeaderTimeoutPerRequest: time.Second,
39	}
40	c, err := client.New(cfg)
41	if err != nil {
42		log.Fatal(err)
43	}
44	kapi := client.NewKeysAPI(c)
45	// set "/foo" key with "bar" value
46	log.Print("Setting '/foo' key with 'bar' value")
47	resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
48	if err != nil {
49		log.Fatal(err)
50	} else {
51		// print common key info
52		log.Printf("Set is done. Metadata is %q\n", resp)
53	}
54	// get "/foo" key's value
55	log.Print("Getting '/foo' key value")
56	resp, err = kapi.Get(context.Background(), "/foo", nil)
57	if err != nil {
58		log.Fatal(err)
59	} else {
60		// print common key info
61		log.Printf("Get is done. Metadata is %q\n", resp)
62		// print value
63		log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
64	}
65}
66```
67
68## Error Handling
69
70etcd client might return three types of errors.
71
72- context error
73
74Each 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.
75
76- cluster error
77
78Each 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.
79
80- response error
81
82If the response gets from the cluster is invalid, a plain string error will be returned. For example, it might be a invalid JSON error.
83
84Here is the example code to handle client errors:
85
86```go
87cfg := client.Config{Endpoints: []string{"http://etcd1:2379","http://etcd2:2379","http://etcd3:2379"}}
88c, err := client.New(cfg)
89if err != nil {
90	log.Fatal(err)
91}
92
93kapi := client.NewKeysAPI(c)
94resp, err := kapi.Set(ctx, "test", "bar", nil)
95if err != nil {
96	if err == context.Canceled {
97		// ctx is canceled by another routine
98	} else if err == context.DeadlineExceeded {
99		// ctx is attached with a deadline and it exceeded
100	} else if cerr, ok := err.(*client.ClusterError); ok {
101		// process (cerr.Errors)
102	} else {
103		// bad cluster endpoints, which are not etcd servers
104	}
105}
106```
107
108
109## Caveat
110
1111. 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.
112
1132. 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.
114
1153. 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.
116
1174. etcd/client cannot detect whether a member is healthy with watches and non-quorum read requests. If the member is isolated from the cluster, etcd/client may retrieve outdated data. Instead, users can either issue quorum read requests or monitor the /health endpoint for member health information.
118