-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
74 lines (61 loc) · 1.4 KB
/
api.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
package election
import (
"errors"
"net"
)
type Subscription struct {
C <-chan Stat
Cancel func()
}
func (e *Election) Term() uint64 {
return e.state.term()
}
func (e *Election) Role() Role {
return e.state.role()
}
func (e *Election) Leader() *net.UDPAddr {
e.mu.Lock()
defer e.mu.Unlock()
return e.leaderAddr
}
func (e *Election) Cluster() []string {
e.mu.Lock()
defer e.mu.Unlock()
members := make([]string, 0, len(e.memberlist))
for m := range e.memberlist {
members = append(members, m)
}
return members
}
// Consistency for the cluster is the role of the upper layer.
// We can add logic to stop working when there is no consistency
// for the cluster.
func (e *Election) AddMember(member string) error {
if _, err := net.ResolveTCPAddr("tcp", member); err != nil {
return err
}
e.mu.Lock()
defer e.mu.Unlock()
if _, ok := e.memberlist[member]; ok {
return errors.New("already exist")
}
e.memberlist[member] = struct{}{}
return nil
}
func (e *Election) DelMember(member string) error {
if _, err := net.ResolveTCPAddr("tcp", member); err != nil {
return err
}
e.mu.Lock()
defer e.mu.Unlock()
if _, ok := e.memberlist[member]; !ok {
return errors.New("not exist")
}
delete(e.memberlist, member)
return nil
}
// - Stat event (notify changed Term or Role)
// - Cluster event (added followers, addMember, delMember)
func (e *Election) Subscribe() *Subscription {
panic("")
}