github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/lib/filegen/client/api.go (about)

     1  package client
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/Cloud-Foundations/Dominator/lib/hash"
     7  	"github.com/Cloud-Foundations/Dominator/lib/log"
     8  	"github.com/Cloud-Foundations/Dominator/lib/mdb"
     9  	"github.com/Cloud-Foundations/Dominator/lib/objectserver"
    10  	proto "github.com/Cloud-Foundations/Dominator/proto/filegenerator"
    11  )
    12  
    13  type ComputedFile struct {
    14  	Pathname string
    15  	Source   string
    16  }
    17  
    18  type gauge struct {
    19  	sync.Mutex
    20  	value uint64
    21  }
    22  
    23  type Machine struct {
    24  	Machine       mdb.Machine
    25  	ComputedFiles []ComputedFile
    26  }
    27  
    28  type machineType struct {
    29  	machine       mdb.Machine
    30  	updateChannel chan<- []proto.FileInfo
    31  	computedFiles map[string]string   // map[pathname] => source
    32  	sourceToPaths map[string][]string // map[source] => []pathnames
    33  }
    34  
    35  type sourceType struct {
    36  	sendChannel chan<- *proto.ClientRequest
    37  }
    38  
    39  type serverMessageType struct {
    40  	source        string
    41  	serverMessage proto.ServerMessage
    42  }
    43  
    44  type Manager struct {
    45  	sourceMap              map[string]*sourceType
    46  	objectServer           objectserver.ObjectServer
    47  	machineMap             map[string]*machineType
    48  	addMachineChannel      chan *machineType
    49  	removeMachineChannel   chan string
    50  	updateMachineChannel   chan *machineType
    51  	serverMessageChannel   chan *serverMessageType
    52  	sourceReconnectChannel chan<- string
    53  	objectWaiters          map[hash.Hash][]chan<- hash.Hash
    54  	numObjectWaiters       gauge
    55  	logger                 log.Logger
    56  }
    57  
    58  // New creates a new *Manager. Object data will be added to the object server
    59  // objSrv. Only one Manager should be created per application.
    60  // The logger will be used to log problems.
    61  func New(objSrv objectserver.ObjectServer, logger log.Logger) *Manager {
    62  	return newManager(objSrv, logger)
    63  }
    64  
    65  // Add will add a machine to the Manager. Re-adding a machine will result in a
    66  // panic. The length of the returned channel buffer is determined by size.
    67  // A channel is returned from which file information may be read. It is
    68  // guaranteed that corresponding object data are in the object server before
    69  // file information is available.
    70  func (m *Manager) Add(machine Machine, size uint) <-chan []proto.FileInfo {
    71  	updateChannel := make(chan []proto.FileInfo, size)
    72  	mach := buildMachine(machine)
    73  	mach.updateChannel = updateChannel
    74  	m.addMachineChannel <- mach
    75  	return updateChannel
    76  }
    77  
    78  // Remove will remove a machine from the Manager. The corresponding file info
    79  // channel will be closed.
    80  func (m *Manager) Remove(hostname string) {
    81  	m.removeMachineChannel <- hostname
    82  }
    83  
    84  // Update will update the machine data for a machine, which may result in file
    85  // info data being sent to the corresponding channel.
    86  func (m *Manager) Update(machine Machine) {
    87  	m.updateMachineChannel <- buildMachine(machine)
    88  }