This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
member.go
104 lines (88 loc) · 2 KB
/
member.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gossipCluster
import (
"github.com/ronaksoft/memberlist"
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/errors"
"github.com/ronaksoft/rony/internal/msg"
"google.golang.org/protobuf/proto"
"net"
)
/*
Creation Time: 2021 - Jan - 14
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type Member struct {
idx int
serverID string
replicaSet uint64
hash uint64
gatewayAddr []string
tunnelAddr []string
ClusterAddr net.IP
ClusterPort uint16
}
func (m *Member) ServerID() string {
return m.serverID
}
func (m *Member) ReplicaSet() uint64 {
return m.replicaSet
}
func (m *Member) GatewayAddr() []string {
return m.gatewayAddr
}
func (m *Member) TunnelAddr() []string {
return m.tunnelAddr
}
func (m *Member) Proto(p *rony.Edge) *rony.Edge {
if p == nil {
p = &rony.Edge{}
}
p.ReplicaSet = m.replicaSet
p.ServerID = m.serverID
p.HostPorts = append(p.HostPorts, m.gatewayAddr...)
return p
}
func (m *Member) Merge(en *msg.EdgeNode) {
m.replicaSet = en.GetReplicaSet()
m.gatewayAddr = append(m.gatewayAddr[:0], en.GetGatewayAddr()...)
m.tunnelAddr = append(m.tunnelAddr[:0], en.GetTunnelAddr()...)
}
func (m *Member) Dial() (net.Conn, error) {
if len(m.tunnelAddr) == 0 {
return nil, errors.ErrNoTunnelAddrs
}
idx := m.idx
for {
conn, err := net.Dial("udp", m.tunnelAddr[idx])
if err == nil {
m.idx = idx
return conn, nil
}
idx = (idx + 1) % len(m.tunnelAddr)
if idx == m.idx {
return nil, err
}
}
}
func newMember(sm *memberlist.Node) (*Member, error) {
en := msg.PoolEdgeNode.Get()
defer msg.PoolEdgeNode.Put(en)
if err := extractNode(sm, en); err != nil {
return nil, err
}
m := &Member{
serverID: string(en.ServerID),
hash: en.Hash,
ClusterAddr: sm.Addr,
ClusterPort: sm.Port,
}
m.Merge(en)
return m, nil
}
func extractNode(n *memberlist.Node, en *msg.EdgeNode) error {
return proto.UnmarshalOptions{}.Unmarshal(n.Meta, en)
}