github.com/NebulousLabs/Sia@v1.3.7/modules/renter/contractor/uptime.go (about)

     1  package contractor
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/NebulousLabs/Sia/build"
     7  	"github.com/NebulousLabs/Sia/modules"
     8  	"github.com/NebulousLabs/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(pk types.SiaPublicKey) bool {
    31  	host, ok := c.hdb.Host(pk)
    32  	if !ok {
    33  		// No host, assume offline.
    34  		return true
    35  	}
    36  	return isOffline(host)
    37  }
    38  
    39  // isOffline indicates whether a host should be considered offline, based on
    40  // its scan metrics.
    41  func isOffline(host modules.HostDBEntry) bool {
    42  	// See if the host has a scan history.
    43  	if len(host.ScanHistory) < 1 {
    44  		// No scan history, assume offline.
    45  		return true
    46  	}
    47  	// If we only have one scan in the history we return false if it was
    48  	// successful.
    49  	if len(host.ScanHistory) == 1 {
    50  		return !host.ScanHistory[0].Success
    51  	}
    52  	// Otherwise we use the last 2 scans. This way a short connectivity problem
    53  	// won't mark the host as offline.
    54  	success1 := host.ScanHistory[len(host.ScanHistory)-1].Success
    55  	success2 := host.ScanHistory[len(host.ScanHistory)-2].Success
    56  	return !(success1 || success2)
    57  }