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

..15-Feb-2020-

README.mdH A D15-Feb-20204.4 KiB11385

auth_role.goH A D15-Feb-20205.7 KiB237187

auth_user.goH A D15-Feb-20207.6 KiB320249

cancelreq.goH A D15-Feb-2020405 199

client.goH A D15-Feb-202017.8 KiB711498

cluster_error.goH A D15-Feb-2020981 3819

curl.goH A D15-Feb-20201.5 KiB7141

discover.goH A D15-Feb-20201.2 KiB4118

doc.goH A D15-Feb-20201.8 KiB741

json.goH A D15-Feb-20202 KiB7348

keys.goH A D15-Feb-202018.9 KiB680419

members.goH A D15-Feb-20207.1 KiB304216

util.goH A D15-Feb-20201.5 KiB5430

README.md

1# etcd/client
2
3etcd/client is the Go client library for etcd.
4
5[![GoDoc](https://godoc.org/go.etcd.io/etcd/client?status.png)](https://godoc.org/go.etcd.io/etcd/client)
6
7For full compatibility, it is recommended to vendor builds using etcd's vendored packages, using tools like `golang/dep`, as in [vendor directories](https://golang.org/cmd/go/#hdr-Vendor_Directories).
8
9## Install
10
11```bash
12go get go.etcd.io/etcd/client
13```
14
15## Usage
16
17```go
18package main
19
20import (
21	"log"
22	"time"
23	"context"
24
25	"go.etcd.io/etcd/client"
26)
27
28func main() {
29	cfg := client.Config{
30		Endpoints:               []string{"http://127.0.0.1:2379"},
31		Transport:               client.DefaultTransport,
32		// set timeout per request to fail fast when the target endpoint is unavailable
33		HeaderTimeoutPerRequest: time.Second,
34	}
35	c, err := client.New(cfg)
36	if err != nil {
37		log.Fatal(err)
38	}
39	kapi := client.NewKeysAPI(c)
40	// set "/foo" key with "bar" value
41	log.Print("Setting '/foo' key with 'bar' value")
42	resp, err := kapi.Set(context.Background(), "/foo", "bar", nil)
43	if err != nil {
44		log.Fatal(err)
45	} else {
46		// print common key info
47		log.Printf("Set is done. Metadata is %q\n", resp)
48	}
49	// get "/foo" key's value
50	log.Print("Getting '/foo' key value")
51	resp, err = kapi.Get(context.Background(), "/foo", nil)
52	if err != nil {
53		log.Fatal(err)
54	} else {
55		// print common key info
56		log.Printf("Get is done. Metadata is %q\n", resp)
57		// print value
58		log.Printf("%q key has %q value\n", resp.Node.Key, resp.Node.Value)
59	}
60}
61```
62
63## Error Handling
64
65etcd client might return three types of errors.
66
67- context error
68
69Each 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.
70
71- cluster error
72
73Each 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.
74
75- response error
76
77If the response gets from the cluster is invalid, a plain string error will be returned. For example, it might be a invalid JSON error.
78
79Here is the example code to handle client errors:
80
81```go
82cfg := client.Config{Endpoints: []string{"http://etcd1:2379","http://etcd2:2379","http://etcd3:2379"}}
83c, err := client.New(cfg)
84if err != nil {
85	log.Fatal(err)
86}
87
88kapi := client.NewKeysAPI(c)
89resp, err := kapi.Set(ctx, "test", "bar", nil)
90if err != nil {
91	if err == context.Canceled {
92		// ctx is canceled by another routine
93	} else if err == context.DeadlineExceeded {
94		// ctx is attached with a deadline and it exceeded
95	} else if cerr, ok := err.(*client.ClusterError); ok {
96		// process (cerr.Errors)
97	} else {
98		// bad cluster endpoints, which are not etcd servers
99	}
100}
101```
102
103
104## Caveat
105
1061. 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.
107
1082. 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.
109
1103. 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.
111
1124. 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.
113