File: cluster.go

package info (click to toggle)
golang-etcd 0.2.0~rc1%2Bgit20140717-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-backports, jessie-kfreebsd
  • size: 188 kB
  • ctags: 165
  • sloc: makefile: 5
file content (51 lines) | stat: -rw-r--r-- 1,088 bytes parent folder | download
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
package etcd

import (
	"net/url"
	"strings"
)

type Cluster struct {
	Leader   string   `json:"leader"`
	Machines []string `json:"machines"`
}

func NewCluster(machines []string) *Cluster {
	// if an empty slice was sent in then just assume HTTP 4001 on localhost
	if len(machines) == 0 {
		machines = []string{"http://127.0.0.1:4001"}
	}

	// default leader and machines
	return &Cluster{
		Leader:   machines[0],
		Machines: machines,
	}
}

// switchLeader switch the current leader to machines[num]
func (cl *Cluster) switchLeader(num int) {
	logger.Debugf("switch.leader[from %v to %v]",
		cl.Leader, cl.Machines[num])

	cl.Leader = cl.Machines[num]
}

func (cl *Cluster) updateFromStr(machines string) {
	cl.Machines = strings.Split(machines, ", ")
}

func (cl *Cluster) updateLeader(leader string) {
	logger.Debugf("update.leader[%s,%s]", cl.Leader, leader)
	cl.Leader = leader
}

func (cl *Cluster) updateLeaderFromURL(u *url.URL) {
	var leader string
	if u.Scheme == "" {
		leader = "http://" + u.Host
	} else {
		leader = u.Scheme + "://" + u.Host
	}
	cl.updateLeader(leader)
}