Skip to content

Commit 3c7a539

Browse files
committed
terminal: retry to create lnd client on failure
1 parent 5036dec commit 3c7a539

File tree

1 file changed

+77
-31
lines changed

1 file changed

+77
-31
lines changed

terminal.go

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import (
4848
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
4949
"github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc"
5050
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
51-
"github.com/lightningnetwork/lnd/lntest/wait"
5251
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
5352
"github.com/lightningnetwork/lnd/macaroons"
5453
"github.com/lightningnetwork/lnd/rpcperms"
@@ -622,7 +621,7 @@ func (g *LightningTerminal) start() error {
622621
}
623622

624623
// Set up all the LND clients required by LiT.
625-
err = g.setUpLNDClients()
624+
err = g.setUpLNDClients(lndQuit)
626625
if err != nil {
627626
g.statusMgr.SetErrored(
628627
subservers.LND, "could not set up LND clients: %v", err,
@@ -687,8 +686,9 @@ func (g *LightningTerminal) start() error {
687686
}
688687

689688
// setUpLNDClients sets up the various LND clients required by LiT.
690-
func (g *LightningTerminal) setUpLNDClients() error {
689+
func (g *LightningTerminal) setUpLNDClients(lndQuit chan struct{}) error {
691690
var (
691+
err error
692692
insecure bool
693693
clientOptions []lndclient.BasicClientOption
694694
)
@@ -710,25 +710,55 @@ func (g *LightningTerminal) setUpLNDClients() error {
710710
clientOptions = append(clientOptions, lndclient.Insecure())
711711
}
712712

713+
checkRunning := func() error {
714+
select {
715+
case err := <-g.errQueue.ChanOut():
716+
return fmt.Errorf("Error from subsystem: %v", err)
717+
718+
case <-lndQuit:
719+
return fmt.Errorf("LND has stopped")
720+
721+
case <-interceptor.ShutdownChannel():
722+
return fmt.Errorf("received the shutdown signal")
723+
724+
default:
725+
return nil
726+
}
727+
}
728+
713729
// The main RPC listener of lnd might need some time to start, it could
714730
// be that we run into a connection refused a few times. We use the
715731
// basic client connection to find out if the RPC server is started yet
716732
// because that doesn't do anything else than just connect. We'll check
717733
// if lnd is also ready to be used in the next step.
718734
log.Infof("Connecting basic lnd client")
719-
err := wait.NoError(func() error {
720-
// Create an lnd client now that we have the full configuration.
721-
// We'll need a basic client and a full client because not all
722-
// subservers have the same requirements.
723-
var err error
735+
736+
for {
737+
err = checkRunning()
738+
if err != nil {
739+
return err
740+
}
741+
742+
// Create an lnd client now that we have the full
743+
// configuration.
744+
// We'll need a basic client and a full client because
745+
// not all subservers have the same requirements.
724746
g.basicClient, err = lndclient.NewBasicClient(
725-
host, tlsPath, filepath.Dir(macPath), string(network),
726-
clientOptions...,
747+
host, tlsPath, filepath.Dir(macPath),
748+
string(network), clientOptions...,
727749
)
728-
return err
729-
}, defaultStartupTimeout)
730-
if err != nil {
731-
return fmt.Errorf("could not create basic LND Client: %v", err)
750+
if err == nil {
751+
log.Infof("Basic lnd client connected")
752+
753+
break
754+
}
755+
756+
g.statusMgr.SetErrored(
757+
subservers.LIT,
758+
"Error when setting up basic LND Client: %v", err,
759+
)
760+
761+
log.Infof("Retrying to connect basic lnd client")
732762
}
733763

734764
// Now we know that the connection itself is ready. But we also need to
@@ -756,23 +786,39 @@ func (g *LightningTerminal) setUpLNDClients() error {
756786
}()
757787

758788
log.Infof("Connecting full lnd client")
759-
g.lndClient, err = lndclient.NewLndServices(
760-
&lndclient.LndServicesConfig{
761-
LndAddress: host,
762-
Network: network,
763-
TLSPath: tlsPath,
764-
Insecure: insecure,
765-
CustomMacaroonPath: macPath,
766-
CustomMacaroonHex: hex.EncodeToString(macData),
767-
BlockUntilChainSynced: true,
768-
BlockUntilUnlocked: true,
769-
CallerCtx: ctxc,
770-
CheckVersion: minimalCompatibleVersion,
771-
},
772-
)
773-
if err != nil {
774-
return fmt.Errorf("could not create LND Services client: %v",
775-
err)
789+
for {
790+
err = checkRunning()
791+
if err != nil {
792+
return err
793+
}
794+
795+
g.lndClient, err = lndclient.NewLndServices(
796+
&lndclient.LndServicesConfig{
797+
LndAddress: host,
798+
Network: network,
799+
TLSPath: tlsPath,
800+
Insecure: insecure,
801+
CustomMacaroonPath: macPath,
802+
CustomMacaroonHex: hex.EncodeToString(macData),
803+
BlockUntilChainSynced: true,
804+
BlockUntilUnlocked: true,
805+
CallerCtx: ctxc,
806+
CheckVersion: minimalCompatibleVersion,
807+
},
808+
)
809+
if err == nil {
810+
log.Infof("Full lnd client connected")
811+
812+
break
813+
}
814+
815+
g.statusMgr.SetErrored(
816+
subservers.LIT,
817+
"Error when creating LND Services client: %v",
818+
err,
819+
)
820+
821+
log.Infof("Retrying to create LND Services client")
776822
}
777823

778824
// Pass LND's build tags to the permission manager so that it can

0 commit comments

Comments
 (0)