-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflux.go
44 lines (37 loc) · 1.02 KB
/
flux.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
package flux
import (
"time"
"github.com/hashicorp/memberlist"
"github.com/istoican/flux/consistent"
"github.com/istoican/flux/transport"
)
// Initializes a new Node with the provided configuration.
// It sets up the consistent hash ring, initializes peer connections,
// configures the memberlist for managing cluster membership,
// and starts a goroutine for periodically rebalancing local stored keys.
func New(config Config) (*Node, error) {
n := &Node{
addr: config.Addr,
store: config.Store,
ring: consistent.New(config.HashFn),
peers: make(map[string]transport.Peer),
peerFn: config.PeerFn,
metrics: Metrics{},
watchers: make(map[string][]*Watcher),
}
memberlistConfig := memberlist.DefaultLocalConfig()
memberlistConfig.Events = n
memberlist, err := memberlist.Create(memberlistConfig)
if err != nil {
return nil, err
}
n.memberlist = memberlist
// periodically rebalance local stored keys
go func() {
for {
n.rebalance()
time.Sleep(1 * time.Second)
}
}()
return n, nil
}