github.com/decred/dcrlnd@v0.7.6/lncfg/healthcheck.go (about) 1 package lncfg 2 3 import ( 4 "errors" 5 "fmt" 6 "time" 7 ) 8 9 var ( 10 // MinHealthCheckInterval is the minimum interval we allow between 11 // health checks. 12 MinHealthCheckInterval = time.Minute 13 14 // MinHealthCheckTimeout is the minimum timeout we allow for health 15 // check calls. 16 MinHealthCheckTimeout = time.Second 17 18 // MinHealthCheckBackoff is the minimum back off we allow between health 19 // check retries. 20 MinHealthCheckBackoff = time.Second 21 ) 22 23 // HealthCheckConfig contains the configuration for the different health checks 24 // the lnd runs. 25 type HealthCheckConfig struct { 26 ChainCheck *CheckConfig `group:"chainbackend" namespace:"chainbackend"` 27 28 DiskCheck *DiskCheckConfig `group:"diskspace" namespace:"diskspace"` 29 30 TLSCheck *CheckConfig `group:"tls" namespace:"tls"` 31 32 TorConnection *CheckConfig `group:"torconnection" namespace:"torconnection"` 33 } 34 35 // Validate checks the values configured for our health checks. 36 func (h *HealthCheckConfig) Validate() error { 37 if err := h.ChainCheck.validate("chain backend"); err != nil { 38 return err 39 } 40 41 if err := h.DiskCheck.validate("disk space"); err != nil { 42 return err 43 } 44 45 if err := h.TLSCheck.validate("tls"); err != nil { 46 return err 47 } 48 49 if h.DiskCheck.RequiredRemaining < 0 || 50 h.DiskCheck.RequiredRemaining >= 1 { 51 52 return errors.New("disk required ratio must be in [0:1)") 53 } 54 55 if err := h.TorConnection.validate("tor connection"); err != nil { 56 return err 57 } 58 59 return nil 60 } 61 62 type CheckConfig struct { 63 Interval time.Duration `long:"interval" description:"How often to run a health check."` 64 65 Attempts int `long:"attempts" description:"The number of calls we will make for the check before failing. Set this value to 0 to disable a check."` 66 67 Timeout time.Duration `long:"timeout" description:"The amount of time we allow the health check to take before failing due to timeout."` 68 69 Backoff time.Duration `long:"backoff" description:"The amount of time to back-off between failed health checks."` 70 } 71 72 // validate checks the values in a health check config entry if it is enabled. 73 func (c *CheckConfig) validate(name string) error { 74 if c.Attempts == 0 { 75 return nil 76 } 77 78 if c.Backoff < MinHealthCheckBackoff { 79 return fmt.Errorf("%v backoff: %v below minimum: %v", name, 80 c.Backoff, MinHealthCheckBackoff) 81 } 82 83 if c.Timeout < MinHealthCheckTimeout { 84 return fmt.Errorf("%v timeout: %v below minimum: %v", name, 85 c.Timeout, MinHealthCheckTimeout) 86 } 87 88 if c.Interval < MinHealthCheckInterval { 89 return fmt.Errorf("%v interval: %v below minimum: %v", name, 90 c.Interval, MinHealthCheckInterval) 91 } 92 93 return nil 94 } 95 96 // DiskCheckConfig contains configuration for ensuring that our node has 97 // sufficient disk space. 98 type DiskCheckConfig struct { 99 RequiredRemaining float64 `long:"diskrequired" description:"The minimum ratio of free disk space to total capacity that we allow before shutting lnd down safely."` 100 101 *CheckConfig 102 }