Skip to content

Commit

Permalink
terminal: retry to create lnd client on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorTigerstrom committed Dec 14, 2023
1 parent a41e8f8 commit 6f98adf
Showing 1 changed file with 77 additions and 31 deletions.
108 changes: 77 additions & 31 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import (
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
"github.com/lightningnetwork/lnd/lnrpc/watchtowerrpc"
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/lightningnetwork/lnd/rpcperms"
Expand Down Expand Up @@ -622,7 +621,7 @@ func (g *LightningTerminal) start() error {
}

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

// setUpLNDClients sets up the various LND clients required by LiT.
func (g *LightningTerminal) setUpLNDClients() error {
func (g *LightningTerminal) setUpLNDClients(lndQuit chan struct{}) error {
var (
err error
insecure bool
clientOptions []lndclient.BasicClientOption
)
Expand All @@ -710,25 +710,55 @@ func (g *LightningTerminal) setUpLNDClients() error {
clientOptions = append(clientOptions, lndclient.Insecure())
}

checkRunning := func() error {
select {
case err := <-g.errQueue.ChanOut():
return fmt.Errorf("Error from subsystem: %v", err)

case <-lndQuit:
return fmt.Errorf("LND has stopped")

case <-interceptor.ShutdownChannel():
return fmt.Errorf("received the shutdown signal")

default:
return nil
}
}

// The main RPC listener of lnd might need some time to start, it could
// be that we run into a connection refused a few times. We use the
// basic client connection to find out if the RPC server is started yet
// because that doesn't do anything else than just connect. We'll check
// if lnd is also ready to be used in the next step.
log.Infof("Connecting basic lnd client")
err := wait.NoError(func() error {
// Create an lnd client now that we have the full configuration.
// We'll need a basic client and a full client because not all
// subservers have the same requirements.
var err error

for {
err = checkRunning()
if err != nil {
return err
}

// Create an lnd client now that we have the full
// configuration.
// We'll need a basic client and a full client because
// not all subservers have the same requirements.
g.basicClient, err = lndclient.NewBasicClient(
host, tlsPath, filepath.Dir(macPath), string(network),
clientOptions...,
host, tlsPath, filepath.Dir(macPath),
string(network), clientOptions...,
)
return err
}, defaultStartupTimeout)
if err != nil {
return fmt.Errorf("could not create basic LND Client: %v", err)
if err == nil {
log.Infof("Basic lnd client connected")

break
}

g.statusMgr.SetErrored(
subservers.LIT,
"Error when setting up basic LND Client: %v", err,
)

log.Infof("Retrying to connect basic lnd client")
}

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

log.Infof("Connecting full lnd client")
g.lndClient, err = lndclient.NewLndServices(
&lndclient.LndServicesConfig{
LndAddress: host,
Network: network,
TLSPath: tlsPath,
Insecure: insecure,
CustomMacaroonPath: macPath,
CustomMacaroonHex: hex.EncodeToString(macData),
BlockUntilChainSynced: true,
BlockUntilUnlocked: true,
CallerCtx: ctxc,
CheckVersion: minimalCompatibleVersion,
},
)
if err != nil {
return fmt.Errorf("could not create LND Services client: %v",
err)
for {
err = checkRunning()
if err != nil {
return err
}

g.lndClient, err = lndclient.NewLndServices(
&lndclient.LndServicesConfig{
LndAddress: host,
Network: network,
TLSPath: tlsPath,
Insecure: insecure,
CustomMacaroonPath: macPath,
CustomMacaroonHex: hex.EncodeToString(macData),
BlockUntilChainSynced: true,
BlockUntilUnlocked: true,
CallerCtx: ctxc,
CheckVersion: minimalCompatibleVersion,
},
)
if err == nil {
log.Infof("Full lnd client connected")

break
}

g.statusMgr.SetErrored(
subservers.LIT,
"Error when creating LND Services client: %v",
err,
)

log.Infof("Retrying to create LND Services client")
}

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

0 comments on commit 6f98adf

Please sign in to comment.