lnd+cmd: move interrupt into cmd

If the main package is used as a library, we don't want it to
register interrupt signals itself. Rather we want to pass in the
shutdown channel manually. We do this in the cmd now.
This commit is contained in:
Oliver Gugger 2020-04-30 09:42:28 +02:00
parent 7158103d4d
commit 620eaa3199
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 15 additions and 10 deletions

View File

@ -4,8 +4,9 @@ import (
"fmt" "fmt"
"os" "os"
flags "github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"github.com/lightningnetwork/lnd" "github.com/lightningnetwork/lnd"
"github.com/lightningnetwork/lnd/signal"
) )
func main() { func main() {
@ -17,9 +18,15 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Hook interceptor for os signals.
signal.Intercept()
// Call the "real" main in a nested manner so the defers will properly // Call the "real" main in a nested manner so the defers will properly
// be executed in the case of a graceful shutdown. // be executed in the case of a graceful shutdown.
if err := lnd.Main(loadedConfig, lnd.ListenerCfg{}); err != nil { err = lnd.Main(
loadedConfig, lnd.ListenerCfg{}, signal.ShutdownChannel(),
)
if err != nil {
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp { if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
} else { } else {
_, _ = fmt.Fprintln(os.Stderr, err) _, _ = fmt.Fprintln(os.Stderr, err)

14
lnd.go
View File

@ -147,13 +147,11 @@ type ListenerCfg struct {
// listeners. // listeners.
type rpcListeners func() ([]*ListenerWithSignal, func(), error) type rpcListeners func() ([]*ListenerWithSignal, func(), error)
// Main is the true entry point for lnd. This function is required since defers // Main is the true entry point for lnd. It accepts a fully populated and
// created in the top-level scope of a main method aren't executed if os.Exit() // validated main configuration struct and an optional listener config struct.
// is called. // This function starts all main system components then blocks until a signal
func Main(config *Config, lisCfg ListenerCfg) error { // is received on the shutdownChan at which point everything is shut down again.
// Hook interceptor for os signals. func Main(config *Config, lisCfg ListenerCfg, shutdownChan <-chan struct{}) error {
signal.Intercept()
cfg = config cfg = config
defer func() { defer func() {
ltndLog.Info("Shutdown complete") ltndLog.Info("Shutdown complete")
@ -728,7 +726,7 @@ func Main(config *Config, lisCfg ListenerCfg) error {
// Wait for shutdown signal from either a graceful server stop or from // Wait for shutdown signal from either a graceful server stop or from
// the interrupt handler. // the interrupt handler.
<-signal.ShutdownChannel() <-shutdownChan
return nil return nil
} }