Skip to content

Commit b6946d9

Browse files
committed
terminal: retry to create lnd client on failure
1 parent 6c1d9cd commit b6946d9

File tree

1 file changed

+80
-31
lines changed

1 file changed

+80
-31
lines changed

terminal.go

Lines changed: 80 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"
@@ -630,7 +629,7 @@ func (g *LightningTerminal) start() error {
630629
}
631630

632631
// Set up all the LND clients required by LiT.
633-
err = g.setUpLNDClients()
632+
err = g.setUpLNDClients(lndQuit)
634633
if err != nil {
635634
g.statusMgr.SetErrored(
636635
subservers.LND, "could not set up LND clients: %v", err,
@@ -695,8 +694,9 @@ func (g *LightningTerminal) start() error {
695694
}
696695

697696
// setUpLNDClients sets up the various LND clients required by LiT.
698-
func (g *LightningTerminal) setUpLNDClients() error {
697+
func (g *LightningTerminal) setUpLNDClients(lndQuit chan struct{}) error {
699698
var (
699+
err error
700700
insecure bool
701701
clientOptions []lndclient.BasicClientOption
702702
)
@@ -718,25 +718,58 @@ func (g *LightningTerminal) setUpLNDClients() error {
718718
clientOptions = append(clientOptions, lndclient.Insecure())
719719
}
720720

721+
// checkRunning checks if we should continue running for the duration of
722+
// the defaultStartupTimeout, or else returns an error indicating why
723+
// a shut down is needed.
724+
checkRunning := func() error {
725+
select {
726+
case err := <-g.errQueue.ChanOut():
727+
return fmt.Errorf("error from subsystem: %v", err)
728+
729+
case <-lndQuit:
730+
return fmt.Errorf("LND has stopped")
731+
732+
case <-interceptor.ShutdownChannel():
733+
return fmt.Errorf("received the shutdown signal")
734+
735+
case <-time.After(defaultStartupTimeout):
736+
return nil
737+
}
738+
}
739+
721740
// The main RPC listener of lnd might need some time to start, it could
722741
// be that we run into a connection refused a few times. We use the
723742
// basic client connection to find out if the RPC server is started yet
724743
// because that doesn't do anything else than just connect. We'll check
725744
// if lnd is also ready to be used in the next step.
726745
log.Infof("Connecting basic lnd client")
727-
err := wait.NoError(func() error {
728-
// Create an lnd client now that we have the full configuration.
729-
// We'll need a basic client and a full client because not all
730-
// subservers have the same requirements.
731-
var err error
746+
747+
for {
748+
// Create an lnd client now that we have the full
749+
// configuration.
750+
// We'll need a basic client and a full client because
751+
// not all subservers have the same requirements.
732752
g.basicClient, err = lndclient.NewBasicClient(
733-
host, tlsPath, filepath.Dir(macPath), string(network),
734-
clientOptions...,
753+
host, tlsPath, filepath.Dir(macPath),
754+
string(network), clientOptions...,
735755
)
736-
return err
737-
}, defaultStartupTimeout)
738-
if err != nil {
739-
return fmt.Errorf("could not create basic LND Client: %v", err)
756+
if err == nil {
757+
log.Infof("Basic lnd client connected")
758+
759+
break
760+
}
761+
762+
g.statusMgr.SetErrored(
763+
subservers.LIT,
764+
"Error when setting up basic LND Client: %v", err,
765+
)
766+
767+
err = checkRunning()
768+
if err != nil {
769+
return err
770+
}
771+
772+
log.Infof("Retrying to connect basic lnd client")
740773
}
741774

742775
// Now we know that the connection itself is ready. But we also need to
@@ -764,23 +797,39 @@ func (g *LightningTerminal) setUpLNDClients() error {
764797
}()
765798

766799
log.Infof("Connecting full lnd client")
767-
g.lndClient, err = lndclient.NewLndServices(
768-
&lndclient.LndServicesConfig{
769-
LndAddress: host,
770-
Network: network,
771-
TLSPath: tlsPath,
772-
Insecure: insecure,
773-
CustomMacaroonPath: macPath,
774-
CustomMacaroonHex: hex.EncodeToString(macData),
775-
BlockUntilChainSynced: true,
776-
BlockUntilUnlocked: true,
777-
CallerCtx: ctxc,
778-
CheckVersion: minimalCompatibleVersion,
779-
},
780-
)
781-
if err != nil {
782-
return fmt.Errorf("could not create LND Services client: %v",
783-
err)
800+
for {
801+
g.lndClient, err = lndclient.NewLndServices(
802+
&lndclient.LndServicesConfig{
803+
LndAddress: host,
804+
Network: network,
805+
TLSPath: tlsPath,
806+
Insecure: insecure,
807+
CustomMacaroonPath: macPath,
808+
CustomMacaroonHex: hex.EncodeToString(macData),
809+
BlockUntilChainSynced: true,
810+
BlockUntilUnlocked: true,
811+
CallerCtx: ctxc,
812+
CheckVersion: minimalCompatibleVersion,
813+
},
814+
)
815+
if err == nil {
816+
log.Infof("Full lnd client connected")
817+
818+
break
819+
}
820+
821+
g.statusMgr.SetErrored(
822+
subservers.LIT,
823+
"Error when creating LND Services client: %v",
824+
err,
825+
)
826+
827+
err = checkRunning()
828+
if err != nil {
829+
return err
830+
}
831+
832+
log.Infof("Retrying to create LND Services client")
784833
}
785834

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

0 commit comments

Comments
 (0)