-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcluster.go
62 lines (47 loc) · 1.13 KB
/
cluster.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
// Copyright 2020 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package cluster
import (
"fmt"
"os"
"github.com/hashicorp/memberlist"
)
// Cluster struct
type Cluster struct {
// https://github.com/hashicorp/memberlist/blob/master/config.go#L350
Config *memberlist.Config
Memlist *memberlist.Memberlist
}
// GetConfig
func (c *Cluster) GetConfig() *memberlist.Config {
return memberlist.DefaultLocalConfig()
}
// SetConfig
func (c *Cluster) SetConfig(config *memberlist.Config) {
c.Config = config
}
// AddLocalNode
func (c *Cluster) AddLocalNode(members []string) (int, error) {
var err error
c.Memlist, err = memberlist.Create(c.Config)
if err != nil {
return 0, err
}
if len(members) > 0 {
count, err := c.Memlist.Join(members)
if err != nil {
return count, err
}
}
return 0, nil
}
// GetNodeName
func (c *Cluster) GetNodeName() string {
hostname, _ := os.Hostname()
return fmt.Sprintf("%s--%s", hostname, GenerateUUID4())
}
// GetLocalNode
func (c *Cluster) GetLocalNode() *memberlist.Node {
return c.Memlist.LocalNode()
}