-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrolodex.go
161 lines (131 loc) · 3.58 KB
/
rolodex.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package meshboi
import (
"encoding/json"
"net"
"sync"
"time"
log "github.com/sirupsen/logrus"
"inet.af/netaddr"
)
type rolodex struct {
conn *net.UDPConn
networks map[string]*meshNetwork
sendInterval time.Duration
timeOutDuration time.Duration
}
const TimeOutSecs = 30
type meshNetwork struct {
// make of IP address to last seen time
members map[netaddr.IPPort]time.Time
membersLock sync.RWMutex
rollo *rolodex
name string
newMember chan struct{}
}
func (m *meshNetwork) register(addr netaddr.IPPort) {
m.membersLock.Lock()
_, ok := m.members[addr]
m.members[addr] = time.Now()
m.membersLock.Unlock()
if !ok {
log.WithFields(log.Fields{
"address": addr,
"name": m.name,
}).Info("Registering new mesh member")
m.newMember <- struct{}{}
}
}
func (r *rolodex) getNetwork(networkName string) *meshNetwork {
if network, ok := r.networks[networkName]; ok {
return network
}
network := &meshNetwork{}
network.members = make(map[netaddr.IPPort]time.Time)
network.rollo = r
network.newMember = make(chan struct{})
network.name = networkName
r.networks[networkName] = network
go network.Serve()
return network
}
func NewRolodex(conn *net.UDPConn, sendInterval time.Duration, timeOutDuration time.Duration) (*rolodex, error) {
rollo := &rolodex{}
rollo.conn = conn
rollo.sendInterval = sendInterval
rollo.timeOutDuration = timeOutDuration
rollo.networks = make(map[string]*meshNetwork)
return rollo, nil
}
func (r *rolodex) Run() {
buf := make([]byte, 65535)
for {
n, addr, err := r.conn.ReadFromUDP(buf)
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
log.Warn("Temporary error reading data: ", nerr)
continue
}
var message HeartbeatMessage
if err := json.Unmarshal(buf[:n], &message); err != nil {
log.Error("Error unmarshalling ", err)
continue
}
mesh := r.getNetwork(message.NetworkName)
ipPort, ok := netaddr.FromStdAddr(addr.IP, addr.Port, "")
if !ok {
log.Error("Error converting to netaddr ", err)
continue
}
mesh.register(ipPort)
}
}
func (mesh *meshNetwork) timeOutInactiveMembers() {
mesh.membersLock.Lock()
defer mesh.membersLock.Unlock()
now := time.Now()
for member := range mesh.members {
timeSinceLastActive := now.Sub(mesh.members[member])
if timeSinceLastActive > mesh.rollo.timeOutDuration {
log.WithFields(log.Fields{
"address": member.IP,
}).Info("Removing member due to timeout")
delete(mesh.members, member)
}
}
}
// Serve sends out messages to each member so that they're aware of other members they can connect to
// It also serves as a heart beat of sorts from the rolodex to the member
func (mesh *meshNetwork) Serve() {
ticker := time.NewTicker(mesh.rollo.sendInterval)
quit := make(chan int)
for {
// Send out an update both periodically, and on the event of a new member joining
select {
case <-ticker.C:
break
case <-mesh.newMember:
break
case <-quit:
ticker.Stop()
return
}
// reset the ticker in case we're sending an update due to a new member joining
ticker.Reset(mesh.rollo.sendInterval)
mesh.timeOutInactiveMembers()
mesh.membersLock.RLock()
memberIps := make([]netaddr.IPPort, 0, len(mesh.members))
for member := range mesh.members {
memberIps = append(memberIps, member)
}
memberMessage := NetworkMap{Addresses: memberIps}
memberMessage.YourIndex = 0
for _, member := range memberIps {
b, err := json.Marshal(memberMessage)
if err != nil {
panic(err)
}
mesh.rollo.conn.WriteToUDP(b, member.UDPAddr())
memberMessage.YourIndex += 1
}
mesh.membersLock.RUnlock()
}
}