Skip to content

Commit 33d6f6e

Browse files
terminal: retry to create lnd client on failure
1 parent 33646b4 commit 33d6f6e

File tree

1 file changed

+76
-28
lines changed

1 file changed

+76
-28
lines changed

terminal.go

Lines changed: 76 additions & 28 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"
@@ -634,7 +633,7 @@ func (g *LightningTerminal) start() error {
634633
}
635634

636635
// Set up all the LND clients required by LiT.
637-
err = g.setUpLNDClients()
636+
err = g.setUpLNDClients(lndQuit)
638637
if err != nil {
639638
g.statusMgr.SetErrored(
640639
subservers.LND, "could not set up LND clients: %v", err,
@@ -699,8 +698,9 @@ func (g *LightningTerminal) start() error {
699698
}
700699

701700
// setUpLNDClients sets up the various LND clients required by LiT.
702-
func (g *LightningTerminal) setUpLNDClients() error {
701+
func (g *LightningTerminal) setUpLNDClients(lndQuit chan struct{}) error {
703702
var (
703+
err error
704704
insecure bool
705705
clientOptions []lndclient.BasicClientOption
706706
)
@@ -722,25 +722,57 @@ func (g *LightningTerminal) setUpLNDClients() error {
722722
clientOptions = append(clientOptions, lndclient.Insecure())
723723
}
724724

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

746778
// Now we know that the connection itself is ready. But we also need to
@@ -768,23 +800,39 @@ func (g *LightningTerminal) setUpLNDClients() error {
768800
}()
769801

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

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

0 commit comments

Comments
 (0)