github.com/Cloud-Foundations/Dominator@v0.3.4/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.notifiersMutex.Lock()
     9  	defer m.notifiersMutex.Unlock()
    10  	delete(m.notifiers, channel)
    11  }
    12  
    13  func (m *Manager) getHealthStatus() string {
    14  	m.healthStatusMutex.RLock()
    15  	defer m.healthStatusMutex.RUnlock()
    16  	return m.healthStatus
    17  }
    18  
    19  func (m *Manager) makeUpdateChannel() <-chan proto.Update {
    20  	m.mutex.RLock()
    21  	defer m.mutex.RUnlock()
    22  	subnets := make([]proto.Subnet, 0, len(m.subnets))
    23  	for id, subnet := range m.subnets {
    24  		if id != "hypervisor" {
    25  			subnets = append(subnets, subnet)
    26  		}
    27  	}
    28  	vms := make(map[string]*proto.VmInfo, len(m.vms))
    29  	for addr, vm := range m.vms {
    30  		vms[addr] = &vm.VmInfo
    31  	}
    32  	numFreeAddresses, err := m.computeNumFreeAddressesMap(m.addressPool)
    33  	if err != nil {
    34  		m.Logger.Println(err)
    35  	}
    36  	channel := make(chan proto.Update, 16)
    37  	m.notifiersMutex.Lock()
    38  	defer m.notifiersMutex.Unlock()
    39  	m.notifiers[channel] = channel
    40  	// Initial update: give everything.
    41  	channel <- proto.Update{
    42  		HaveAddressPool:  true,
    43  		AddressPool:      m.addressPool.Registered,
    44  		HaveDisabled:     true,
    45  		Disabled:         m.disabled,
    46  		MemoryInMiB:      &m.memTotalInMiB,
    47  		NumCPUs:          &m.numCPUs,
    48  		NumFreeAddresses: numFreeAddresses,
    49  		HealthStatus:     m.healthStatus,
    50  		HaveSerialNumber: true,
    51  		SerialNumber:     m.serialNumber,
    52  		HaveSubnets:      true,
    53  		Subnets:          subnets,
    54  		TotalVolumeBytes: &m.totalVolumeBytes,
    55  		HaveVMs:          true,
    56  		VMs:              vms,
    57  	}
    58  	return channel
    59  }
    60  
    61  func (m *Manager) sendUpdate(update proto.Update) {
    62  	update.HealthStatus = m.getHealthStatus()
    63  	m.notifiersMutex.Lock()
    64  	defer m.notifiersMutex.Unlock()
    65  	for readChannel, writeChannel := range m.notifiers {
    66  		select {
    67  		case writeChannel <- update:
    68  		default:
    69  			close(writeChannel)
    70  			delete(m.notifiers, readChannel)
    71  		}
    72  	}
    73  }