forked from skynetservices/skydns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
76 lines (70 loc) · 1.98 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2014 The SkyDNS Authors. All rights reserved.
// Use of this source code is governed by The MIT License (MIT) that can be
// found in the LICENSE file.
package main
import (
"log"
"net/url"
"strings"
"github.com/coreos/go-etcd/etcd"
)
func NewClient(machines []string) (client *etcd.Client) {
// set default if not specified in env
if len(machines) == 1 && machines[0] == "" {
machines[0] = "http://127.0.0.1:4001"
}
if strings.HasPrefix(machines[0], "https://") {
var err error
// TODO(miek): machines is local, the rest is global, ugly.
if client, err = etcd.NewTLSClient(machines, tlspem, tlskey, cacert); err != nil {
// TODO(miek): would be nice if this wasn't a fatal error
log.Fatalf("failure to connect: %s\n", err)
}
client.SyncCluster()
} else {
client = etcd.NewClient(machines)
client.SyncCluster()
}
return client
}
// updateClient updates the client with the machines found in v2/_etcd/machines.
func (s *server) UpdateClient(resp *etcd.Response) {
machines := make([]string, 0, 3)
for _, m := range resp.Node.Nodes {
u, e := url.Parse(m.Value)
if e != nil {
continue
}
// etcd=bla&raft=bliep
// TODO(miek): surely there is a better way to do this
ms := strings.Split(u.String(), "&")
if len(ms) == 0 {
continue
}
if len(ms[0]) < 5 {
continue
}
machines = append(machines, ms[0][5:])
}
s.config.log.Infof("setting new etcd cluster to %v", machines)
c := NewClient(machines)
// This is our RCU, switch the pointer, old readers get the old
// one, new reader get the new one.
s.client = c
}
// get is a wrapper for client.Get that uses SingleInflight to suppress multiple
// outstanding queries.
func get(client *etcd.Client, path string, recursive bool) (*etcd.Response, error) {
resp, err, _ := etcdInflight.Do(path, func() (*etcd.Response, error) {
r, e := client.Get(path, false, recursive)
if e != nil {
return nil, e
}
return r, e
})
if err != nil {
return resp, err
}
// shared?
return resp, err
}