github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/sub/rpcd/poll.go (about)

     1  package rpcd
     2  
     3  import (
     4  	"syscall"
     5  	"time"
     6  
     7  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
     8  	"github.com/Cloud-Foundations/Dominator/proto/sub"
     9  )
    10  
    11  var startTime time.Time = time.Now()
    12  
    13  func (t *rpcType) Poll(conn *srpc.Conn) error {
    14  	defer conn.Flush()
    15  	var request sub.PollRequest
    16  	var response sub.PollResponse
    17  	if err := conn.Decode(&request); err != nil {
    18  		_, err = conn.WriteString(err.Error() + "\n")
    19  		return err
    20  	}
    21  	if _, err := conn.WriteString("\n"); err != nil {
    22  		return err
    23  	}
    24  	if !request.ShortPollOnly && !conn.GetAuthInformation().HaveMethodAccess {
    25  		return srpc.ErrorAccessToMethodDenied
    26  	}
    27  	response.NetworkSpeed = t.networkReaderContext.MaximumSpeed()
    28  	response.CurrentConfiguration = t.getConfiguration()
    29  	t.rwLock.RLock()
    30  	response.FetchInProgress = t.fetchInProgress
    31  	response.UpdateInProgress = t.updateInProgress
    32  	if t.lastFetchError != nil {
    33  		response.LastFetchError = t.lastFetchError.Error()
    34  	}
    35  	if !t.updateInProgress {
    36  		if t.lastUpdateError != nil {
    37  			response.LastUpdateError = t.lastUpdateError.Error()
    38  		}
    39  		response.LastUpdateHadTriggerFailures = t.lastUpdateHadTriggerFailures
    40  	}
    41  	response.LastSuccessfulImageName = t.lastSuccessfulImageName
    42  	response.FreeSpace = t.getFreeSpace()
    43  	t.rwLock.RUnlock()
    44  	response.StartTime = startTime
    45  	response.PollTime = time.Now()
    46  	response.ScanCount = t.fileSystemHistory.ScanCount()
    47  	response.DurationOfLastScan = t.fileSystemHistory.DurationOfLastScan()
    48  	response.GenerationCount = t.fileSystemHistory.GenerationCount()
    49  	fs := t.fileSystemHistory.FileSystem()
    50  	if fs != nil &&
    51  		!request.ShortPollOnly &&
    52  		request.HaveGeneration != t.fileSystemHistory.GenerationCount() {
    53  		response.FileSystemFollows = true
    54  	}
    55  	if err := conn.Encode(response); err != nil {
    56  		return err
    57  	}
    58  	if response.FileSystemFollows {
    59  		if err := fs.FileSystem.Encode(conn); err != nil {
    60  			return err
    61  		}
    62  		if err := fs.ObjectCache.Encode(conn); err != nil {
    63  			return err
    64  		}
    65  	}
    66  	return nil
    67  }
    68  
    69  func (t *rpcType) getFreeSpace() *uint64 {
    70  	if fd, err := syscall.Open(t.rootDir, syscall.O_RDONLY, 0); err != nil {
    71  		t.logger.Printf("error opening: %s: %s", t.rootDir, err)
    72  		return nil
    73  	} else {
    74  		defer syscall.Close(fd)
    75  		var statbuf syscall.Statfs_t
    76  		if err := syscall.Fstatfs(fd, &statbuf); err != nil {
    77  			t.logger.Printf("error getting file-system stats: %s\n", err)
    78  			return nil
    79  		}
    80  		retval := uint64(statbuf.Bfree * uint64(statbuf.Bsize))
    81  		return &retval
    82  	}
    83  }