-
Notifications
You must be signed in to change notification settings - Fork 19
/
update.go
70 lines (64 loc) · 1.65 KB
/
update.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
package manager
import (
proto "github.com/Symantec/Dominator/proto/hypervisor"
)
func (m *Manager) closeUpdateChannel(channel <-chan proto.Update) {
m.mutex.Lock()
defer m.mutex.Unlock()
delete(m.notifiers, channel)
}
func (m *Manager) getHealthStatus() string {
m.mutex.RLock()
defer m.mutex.RUnlock()
return m.healthStatus
}
func (m *Manager) makeUpdateChannel() <-chan proto.Update {
channel := make(chan proto.Update, 16)
m.mutex.Lock()
defer m.mutex.Unlock()
m.notifiers[channel] = channel
subnets := make([]proto.Subnet, 0, len(m.subnets))
for id, subnet := range m.subnets {
if id != "hypervisor" {
subnets = append(subnets, subnet)
}
}
vms := make(map[string]*proto.VmInfo, len(m.vms))
for addr, vm := range m.vms {
vms[addr] = &vm.VmInfo
}
numFreeAddresses, err := m.computeNumFreeAddressesMap(m.addressPool)
if err != nil {
m.Logger.Println(err)
}
// Initial update: give everything.
channel <- proto.Update{
HaveAddressPool: true,
AddressPool: m.addressPool.Registered,
NumFreeAddresses: numFreeAddresses,
HealthStatus: m.healthStatus,
HaveSerialNumber: true,
SerialNumber: m.serialNumber,
HaveSubnets: true,
Subnets: subnets,
HaveVMs: true,
VMs: vms,
}
return channel
}
func (m *Manager) sendUpdate(update proto.Update) {
m.mutex.Lock()
defer m.mutex.Unlock()
m.sendUpdateWithLock(update)
}
func (m *Manager) sendUpdateWithLock(update proto.Update) {
update.HealthStatus = m.healthStatus
for readChannel, writeChannel := range m.notifiers {
select {
case writeChannel <- update:
default:
close(writeChannel)
delete(m.notifiers, readChannel)
}
}
}