github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/modules/renter/contractor/uptime.go (about)

     1  package contractor
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/Synthesix/Sia/build"
     7  	"github.com/Synthesix/Sia/modules"
     8  	"github.com/Synthesix/Sia/types"
     9  )
    10  
    11  // uptimeMinScans is the minimum number of scans required to judge whether a
    12  // host is offline or not.
    13  const uptimeMinScans = 3
    14  
    15  // uptimeWindow specifies the duration in which host uptime is checked.
    16  var uptimeWindow = func() time.Duration {
    17  	switch build.Release {
    18  	case "dev":
    19  		return 30 * time.Minute
    20  	case "standard":
    21  		return 7 * 24 * time.Hour // 1 week.
    22  	case "testing":
    23  		return 15 * time.Second
    24  	}
    25  	panic("undefined uptimeWindow")
    26  }()
    27  
    28  // IsOffline indicates whether a contract's host should be considered offline,
    29  // based on its scan metrics.
    30  func (c *Contractor) IsOffline(id types.FileContractID) bool {
    31  	contract, ok := c.contracts.View(id)
    32  	if !ok {
    33  		// No contract, assume offline.
    34  		return true
    35  	}
    36  	// See if there is a host that corresponds to this contract.
    37  	host, ok := c.hdb.Host(contract.HostPublicKey)
    38  	if !ok {
    39  		// No host, assume offline.
    40  		return true
    41  	}
    42  	return isOffline(host)
    43  }
    44  
    45  // isOffline indicates whether a host should be considered offline, based on
    46  // its scan metrics.
    47  func isOffline(host modules.HostDBEntry) bool {
    48  	// See if the host has a scan history.
    49  	if len(host.ScanHistory) < 1 {
    50  		// No scan history, assume offline.
    51  		return true
    52  	}
    53  	// Return 'true' if the most recent scan of the host failed, false
    54  	// otherwise.
    55  	return !host.ScanHistory[len(host.ScanHistory)-1].Success
    56  }