forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachines.go
42 lines (34 loc) · 781 Bytes
/
machines.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
package main
// machineNum returns the number of machines in the cluster
func machineNum() int {
response, _ := etcdStore.RawGet("_etcd/machines")
return len(response)
}
// getMachines gets the current machines in the cluster
func getMachines(toURL func(string) (string, bool)) []string {
peers := r.Peers()
machines := make([]string, len(peers)+1)
leader, ok := toURL(r.Leader())
self, _ := toURL(r.Name())
i := 1
if ok {
machines[0] = leader
if leader != self {
machines[1] = self
i = 2
}
} else {
machines[0] = self
}
// Add all peers to the slice
for peerName, _ := range peers {
if machine, ok := toURL(peerName); ok {
// do not add leader twice
if machine != leader {
machines[i] = machine
i++
}
}
}
return machines
}