mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-12-09 20:33:45 +01:00
healthcheck: monitor access to chain backend
Add a new health check package which will periodically poll health check functions and shutdown if we do not succeed after our set number of attempts. The first check that we add is one for our chain backend, to ensure that we are connected to a bitcoin node.
This commit is contained in:
44
server.go
44
server.go
@@ -37,6 +37,7 @@ import (
|
||||
"github.com/lightningnetwork/lnd/contractcourt"
|
||||
"github.com/lightningnetwork/lnd/discovery"
|
||||
"github.com/lightningnetwork/lnd/feature"
|
||||
"github.com/lightningnetwork/lnd/healthcheck"
|
||||
"github.com/lightningnetwork/lnd/htlcswitch"
|
||||
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
||||
"github.com/lightningnetwork/lnd/input"
|
||||
@@ -276,6 +277,9 @@ type server struct {
|
||||
|
||||
hostAnn *netann.HostAnnouncer
|
||||
|
||||
// livelinessMonitor monitors that lnd has access to critical resources.
|
||||
livelinessMonitor *healthcheck.Monitor
|
||||
|
||||
quit chan struct{}
|
||||
|
||||
wg sync.WaitGroup
|
||||
@@ -1254,6 +1258,32 @@ func newServer(cfg *Config, listenAddrs []net.Addr,
|
||||
})
|
||||
}
|
||||
|
||||
// Create a set of health checks using our configured values. If a
|
||||
// health check has been disabled by setting attempts to 0, our monitor
|
||||
// will not run it.
|
||||
chainHealthCheck := healthcheck.NewObservation(
|
||||
"chain backend",
|
||||
func() error {
|
||||
_, _, err := cc.chainIO.GetBestBlock()
|
||||
return err
|
||||
},
|
||||
cfg.HealthChecks.ChainCheck.Interval,
|
||||
cfg.HealthChecks.ChainCheck.Timeout,
|
||||
cfg.HealthChecks.ChainCheck.Backoff,
|
||||
cfg.HealthChecks.ChainCheck.Attempts,
|
||||
)
|
||||
|
||||
// If we have not disabled all of our health checks, we create a
|
||||
// liveliness monitor with our configured checks.
|
||||
s.livelinessMonitor = healthcheck.NewMonitor(
|
||||
&healthcheck.Config{
|
||||
Checks: []*healthcheck.Observation{
|
||||
chainHealthCheck,
|
||||
},
|
||||
Shutdown: srvrLog.Criticalf,
|
||||
},
|
||||
)
|
||||
|
||||
// Create the connection manager which will be responsible for
|
||||
// maintaining persistent outbound connections and also accepting new
|
||||
// incoming connections
|
||||
@@ -1304,6 +1334,13 @@ func (s *server) Start() error {
|
||||
}
|
||||
}
|
||||
|
||||
if s.livelinessMonitor != nil {
|
||||
if err := s.livelinessMonitor.Start(); err != nil {
|
||||
startErr = err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Start the notification server. This is used so channel
|
||||
// management goroutines can be notified when a funding
|
||||
// transaction reaches a sufficient number of confirmations, or
|
||||
@@ -1535,6 +1572,13 @@ func (s *server) Stop() error {
|
||||
}
|
||||
}
|
||||
|
||||
if s.livelinessMonitor != nil {
|
||||
if err := s.livelinessMonitor.Stop(); err != nil {
|
||||
srvrLog.Warnf("unable to shutdown liveliness "+
|
||||
"monitor: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for all lingering goroutines to quit.
|
||||
s.wg.Wait()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user