Skip to content

Commit 940e413

Browse files
committed
multi: track LiT and LND status
With this commit, we now also track the start-up state of LiT and LiT's connection to LND. These differ from the other sub-servers (loop, pool & faraday) because a failure to start LiT or LND is fatal and so should stop the rest of the start-process, however, we still want the webserver to continue serving the new Status server so that the UI can query the start-up status of LND and LiT. So: if any errors occur while starting/connecting to LND or any other errors occur while starting any of LiTs other processes, then we throw an error but we dont kill the main LiT process. The main process is only killed upon receiving a shutdown signal.
1 parent f8d59de commit 940e413

2 files changed

Lines changed: 84 additions & 46 deletions

File tree

rpc_proxy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,10 @@ func (p *rpcProxy) checkSubSystemStarted(requestURI string) error {
421421
system = subServerName
422422

423423
case p.permsMgr.IsLndURI(requestURI):
424-
return nil
424+
system = LNDSubServer
425425

426426
case p.permsMgr.IsLitURI(requestURI):
427-
return nil
427+
system = LitSubServer
428428

429429
default:
430430
return fmt.Errorf("unknown gRPC web request: %v", requestURI)

terminal.go

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,53 @@ func (g *LightningTerminal) Run() error {
222222
return fmt.Errorf("error starting UI HTTP server: %v", err)
223223
}
224224

225+
// Attempt to start Lit and all of its sub-servers. If an error is
226+
// returned, it means that either one of Lit's sub-servers internal could not
227+
// start or LND
228+
startErr := g.start()
229+
if startErr != nil {
230+
g.statusServer.setServerErrored(
231+
LitSubServer, "could not start Lit: %v", err,
232+
)
233+
}
234+
235+
// Now block until we receive an error or the main shutdown
236+
// signal.
237+
<-shutdownInterceptor.ShutdownChannel()
238+
log.Infof("Shutdown signal received")
239+
240+
if g.rpcProxy != nil {
241+
if err := g.rpcProxy.Stop(); err != nil {
242+
log.Errorf("Error stopping rpc proxy: %v", err)
243+
}
244+
}
245+
246+
if g.httpServer != nil {
247+
if err := g.httpServer.Close(); err != nil {
248+
log.Errorf("Error stopping UI server: %v", err)
249+
}
250+
}
251+
252+
if err := g.statusServer.Stop(); err != nil {
253+
log.Errorf("Error stopping status server: %v", err)
254+
}
255+
256+
g.wg.Wait()
257+
258+
return startErr
259+
}
260+
261+
// start attempts to start all the various components of Litd. Only Litd and
262+
// LND errors are considered fatal and will result in an error being returned.
263+
// If any of the sub-servers managed by the subServerMgr error while starting
264+
// up, these are considered non-fatal and will not result in an error being
265+
// returned.
266+
func (g *LightningTerminal) start() error {
225267
// Create the instances of our subservers now so we can hook them up to
226268
// lnd once it's fully running.
227269
g.initSubServers()
228270

271+
var err error
229272
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
230273
basicAuth: g.rpcProxy.basicAuth,
231274
dbDir: filepath.Join(g.cfg.LitDir, g.cfg.Network),
@@ -300,9 +343,7 @@ func (g *LightningTerminal) Run() error {
300343
go func() {
301344
defer g.wg.Done()
302345

303-
err := lnd.Main(
304-
g.cfg.Lnd, lisCfg, implCfg, shutdownInterceptor,
305-
)
346+
err := lnd.Main(g.cfg.Lnd, lisCfg, implCfg, interceptor)
306347
if e, ok := err.(*flags.Error); err != nil &&
307348
(!ok || e.Type != flags.ErrHelp) {
308349

@@ -344,24 +385,30 @@ func (g *LightningTerminal) Run() error {
344385
return err
345386

346387
case <-lndQuit:
347-
return nil
388+
g.statusServer.setServerErrored(
389+
LNDSubServer, "lndQuit channel closed",
390+
)
391+
return fmt.Errorf("LND has stopped")
348392

349-
case <-shutdownInterceptor.ShutdownChannel():
350-
return errors.New("shutting down")
393+
case <-interceptor.ShutdownChannel():
394+
return fmt.Errorf("received the shutdown signal")
351395
}
352396

353397
// We now know that starting lnd was successful. If we now run into an
354398
// error, we must shut down lnd correctly.
355399
defer func() {
356-
err := g.shutdown()
400+
err := g.shutdownSubServers()
357401
if err != nil {
358402
log.Errorf("Error shutting down: %v", err)
359403
}
360404
}()
361405

362406
// Connect to LND.
363407
if err = g.connectLND(bufRpcListener); err != nil {
364-
return fmt.Errorf("could not connect to LND: %v", err)
408+
g.statusServer.setServerErrored(
409+
LNDSubServer, "could not connect to LND: %v", err,
410+
)
411+
return fmt.Errorf("could not connect to LND")
365412
}
366413

367414
// Initialise any connections to sub-servers that we are running in
@@ -389,12 +436,18 @@ func (g *LightningTerminal) Run() error {
389436
return err
390437

391438
case <-lndQuit:
392-
return nil
439+
g.statusServer.setServerErrored(
440+
LNDSubServer, "lndQuit channel closed",
441+
)
442+
return fmt.Errorf("LND is not running")
393443

394-
case <-shutdownInterceptor.ShutdownChannel():
444+
case <-interceptor.ShutdownChannel():
395445
return errors.New("shutting down")
396446
}
397447

448+
// We can now set the status of LND as running.
449+
g.statusServer.setServerRunning(LNDSubServer)
450+
398451
// If we're in integrated mode, we'll need to wait for lnd to send the
399452
// macaroon after unlock before going any further.
400453
if g.cfg.LndMode == ModeIntegrated {
@@ -405,8 +458,10 @@ func (g *LightningTerminal) Run() error {
405458
// Set up all the LND clients required by LiT.
406459
err = g.setUpLNDClients()
407460
if err != nil {
408-
log.Errorf("Could not set up LND clients: %v", err)
409-
return err
461+
g.statusServer.setServerErrored(
462+
LNDSubServer, "could not set up LND clients: %v", err,
463+
)
464+
return fmt.Errorf("could not start LND")
410465
}
411466

412467
// If we're in integrated and stateless init mode, we won't create
@@ -433,22 +488,28 @@ func (g *LightningTerminal) Run() error {
433488
return fmt.Errorf("could not start litd sub-servers: %v", err)
434489
}
435490

491+
// We can now set the status of LiT as running.
492+
g.statusServer.setServerRunning(LitSubServer)
493+
436494
// Now block until we receive an error or the main shutdown signal.
437495
select {
438496
case err := <-g.errQueue.ChanOut():
439497
if err != nil {
440-
log.Errorf("Received critical error from subsystem, "+
441-
"shutting down: %v", err)
498+
return fmt.Errorf("received critical error from "+
499+
"subsystem, shutting down: %v", err,
500+
)
442501
}
443502

444503
case <-lndQuit:
445-
return nil
504+
g.statusServer.setServerErrored(
505+
LNDSubServer, "lndQuit channel closed",
506+
)
507+
return fmt.Errorf("LND is not running")
446508

447-
case <-shutdownInterceptor.ShutdownChannel():
448-
log.Infof("Shutdown signal received")
509+
case <-interceptor.ShutdownChannel():
449510
}
450511

451-
return nil
512+
return fmt.Errorf("received the shutdown signal")
452513
}
453514

454515
// initSubServers registers the faraday, loop and pool sub-servers with the
@@ -899,8 +960,9 @@ func (g *LightningTerminal) BuildWalletConfig(ctx context.Context,
899960
)
900961
}
901962

902-
// shutdown stops all subservers that were started and attached to lnd.
903-
func (g *LightningTerminal) shutdown() error {
963+
// shutdownSubServers stops all subservers that were started and attached to
964+
// lnd.
965+
func (g *LightningTerminal) shutdownSubServers() error {
904966
var returnErr error
905967

906968
err := g.subServerMgr.Stop()
@@ -934,37 +996,13 @@ func (g *LightningTerminal) shutdown() error {
934996
g.restCancel()
935997
}
936998

937-
if g.rpcProxy != nil {
938-
if err := g.rpcProxy.Stop(); err != nil {
939-
log.Errorf("Error stopping lnd proxy: %v", err)
940-
returnErr = err
941-
}
942-
}
943-
944999
if g.lndConn != nil {
9451000
if err := g.lndConn.Close(); err != nil {
9461001
log.Errorf("Error closing lnd connection: %v", err)
9471002
returnErr = err
9481003
}
9491004
}
9501005

951-
if g.httpServer != nil {
952-
if err := g.httpServer.Close(); err != nil {
953-
log.Errorf("Error stopping UI server: %v", err)
954-
returnErr = err
955-
}
956-
}
957-
958-
if err := g.statusServer.Stop(); err != nil {
959-
log.Errorf("Error stopping status server: %v", err)
960-
returnErr = err
961-
}
962-
963-
// In case the error wasn't thrown by lnd, make sure we stop it too.
964-
interceptor.RequestShutdown()
965-
966-
g.wg.Wait()
967-
9681006
// Do we have any last errors to display? We use an anonymous function,
9691007
// so we can use return instead of breaking to a label in the default
9701008
// case.

0 commit comments

Comments
 (0)