github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/hypervisor/manager/update.go (about) 1 package manager 2 3 import ( 4 proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor" 5 ) 6 7 func (m *Manager) closeUpdateChannel(channel <-chan proto.Update) { 8 m.mutex.Lock() 9 defer m.mutex.Unlock() 10 delete(m.notifiers, channel) 11 } 12 13 func (m *Manager) getHealthStatus() string { 14 m.mutex.RLock() 15 defer m.mutex.RUnlock() 16 return m.healthStatus 17 } 18 19 func (m *Manager) makeUpdateChannel() <-chan proto.Update { 20 channel := make(chan proto.Update, 16) 21 m.mutex.Lock() 22 defer m.mutex.Unlock() 23 m.notifiers[channel] = channel 24 subnets := make([]proto.Subnet, 0, len(m.subnets)) 25 for id, subnet := range m.subnets { 26 if id != "hypervisor" { 27 subnets = append(subnets, subnet) 28 } 29 } 30 vms := make(map[string]*proto.VmInfo, len(m.vms)) 31 for addr, vm := range m.vms { 32 vms[addr] = &vm.VmInfo 33 } 34 numFreeAddresses, err := m.computeNumFreeAddressesMap(m.addressPool) 35 if err != nil { 36 m.Logger.Println(err) 37 } 38 // Initial update: give everything. 39 channel <- proto.Update{ 40 HaveAddressPool: true, 41 AddressPool: m.addressPool.Registered, 42 NumFreeAddresses: numFreeAddresses, 43 HealthStatus: m.healthStatus, 44 HaveSerialNumber: true, 45 SerialNumber: m.serialNumber, 46 HaveSubnets: true, 47 Subnets: subnets, 48 HaveVMs: true, 49 VMs: vms, 50 } 51 return channel 52 } 53 54 func (m *Manager) sendUpdate(update proto.Update) { 55 m.mutex.Lock() 56 defer m.mutex.Unlock() 57 m.sendUpdateWithLock(update) 58 } 59 60 func (m *Manager) sendUpdateWithLock(update proto.Update) { 61 update.HealthStatus = m.healthStatus 62 for readChannel, writeChannel := range m.notifiers { 63 select { 64 case writeChannel <- update: 65 default: 66 close(writeChannel) 67 delete(m.notifiers, readChannel) 68 } 69 } 70 }