-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb_membership.go
72 lines (59 loc) · 1.75 KB
/
b_membership.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
package main
import (
"github.com/charmbracelet/log"
"github.com/hashicorp/memberlist"
"os"
)
type Membership interface {
Join(existing []string) error
MembershipChangeCh() chan memberlist.NodeEvent
}
// GossipMembership is a membership implementation using hashicorp/memberlist
// It could be EtcdMembership as well as done in JunoDB
type GossipMembership struct {
membershipList *memberlist.Memberlist
hostName string
gossipPort int
membershipChangeCh chan memberlist.NodeEvent
}
func NewGossipMembership(gossipPort int, httpAddress string) (Membership, error) {
config := memberlist.DefaultLocalConfig()
config.Name = httpAddress
config.BindAddr = GetLocalIP()
config.BindPort = gossipPort
config.LogOutput = NewMemberlistLogger()
membershipChangeCh := make(chan memberlist.NodeEvent, 16)
config.Events = &memberlist.ChannelEventDelegate{
Ch: membershipChangeCh,
}
membershipList, err := memberlist.Create(config)
if err != nil {
return nil, err
}
return &GossipMembership{
membershipList: membershipList,
membershipChangeCh: membershipChangeCh,
}, nil
}
func (c *GossipMembership) Join(existing []string) error {
_, err := c.membershipList.Join(existing)
return err
}
func (c *GossipMembership) MembershipChangeCh() chan memberlist.NodeEvent {
return c.membershipChangeCh
}
// -----------------------Logger----------------------------------
type MemberlistLogger struct {
Logger *log.Logger
}
func NewMemberlistLogger() MemberlistLogger {
return MemberlistLogger{
Logger: log.NewWithOptions(os.Stderr, log.Options{
Prefix: "memberlist",
}),
}
}
func (l MemberlistLogger) Write(p []byte) (n int, err error) {
l.Logger.Debug(string(p)) // change it to `Info` to see the memberlist logs
return len(p), nil
}