Skip to content

Commit 7dafaed

Browse files
terminal: separate session RPC server init & start
Similar to how the previous commit separated the initialization and starting of the account RPC server, this commit separates the initialization and starting of the sessions RPC server.
1 parent 6e96c61 commit 7dafaed

File tree

2 files changed

+87
-77
lines changed

2 files changed

+87
-77
lines changed

session_rpcserver.go

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ import (
4040
// other special cases.
4141
const readOnlyAction = "***readonly***"
4242

43+
var (
44+
// ErrServerNotActive indicates that the server has started but hasn't
45+
// fully finished the startup process.
46+
ErrServerNotActive = errors.New("session server is still in the " +
47+
"process of starting")
48+
)
49+
4350
// sessionRpcServer is the gRPC server for the Session RPC interface.
4451
type sessionRpcServer struct {
4552
litrpc.UnimplementedSessionsServer
@@ -70,42 +77,11 @@ type sessionRpcServerConfig struct {
7077
privMap firewalldb.PrivacyMapper
7178
}
7279

73-
// newSessionRPCServer creates a new sessionRpcServer using the passed config.
74-
func newSessionRPCServer(cfg *sessionRpcServerConfig) (*sessionRpcServer,
75-
error) {
76-
77-
// Create the gRPC server that handles adding/removing sessions and the
78-
// actual mailbox server that spins up the Terminal Connect server
79-
// interface.
80-
server := session.NewServer(
81-
func(id session.ID, opts ...grpc.ServerOption) *grpc.Server {
82-
// Add the session ID injector interceptors first so
83-
// that the session ID is available in the context of
84-
// all interceptors that come after.
85-
allOpts := []grpc.ServerOption{
86-
addSessionIDToStreamCtx(id),
87-
addSessionIDToUnaryCtx(id),
88-
}
89-
90-
allOpts = append(allOpts, cfg.grpcOptions...)
91-
allOpts = append(allOpts, opts...)
92-
93-
// Construct the gRPC server with the options.
94-
grpcServer := grpc.NewServer(allOpts...)
95-
96-
// Register various grpc servers with the LNC session
97-
// server.
98-
cfg.registerGrpcServers(grpcServer)
99-
100-
return grpcServer
101-
},
102-
)
103-
80+
// newSessionRPCServer creates a new sessionRpcServer.
81+
func newSessionRPCServer() *sessionRpcServer {
10482
return &sessionRpcServer{
105-
cfg: cfg,
106-
sessionServer: server,
107-
quit: make(chan struct{}),
108-
}, nil
83+
quit: make(chan struct{}),
84+
}
10985
}
11086

11187
// wrappedServerStream is a wrapper around the grpc.ServerStream that allows us
@@ -164,9 +140,42 @@ func addSessionIDToUnaryCtx(id session.ID) grpc.ServerOption {
164140
})
165141
}
166142

167-
// start all the components necessary for the sessionRpcServer to start serving
168-
// requests. This includes resuming all non-revoked sessions.
169-
func (s *sessionRpcServer) start(ctx context.Context) error {
143+
// start starts a new sessionRpcServer using the passed config, and adds all
144+
// components necessary for the sessionRpcServer to start serving requests. This
145+
// includes resuming all non-revoked sessions.
146+
func (s *sessionRpcServer) start(ctx context.Context,
147+
cfg *sessionRpcServerConfig) error {
148+
149+
// Create the gRPC server that handles adding/removing sessions and the
150+
// actual mailbox server that spins up the Terminal Connect server
151+
// interface.
152+
server := session.NewServer(
153+
func(id session.ID, opts ...grpc.ServerOption) *grpc.Server {
154+
// Add the session ID injector interceptors first so
155+
// that the session ID is available in the context of
156+
// all interceptors that come after.
157+
allOpts := []grpc.ServerOption{
158+
addSessionIDToStreamCtx(id),
159+
addSessionIDToUnaryCtx(id),
160+
}
161+
162+
allOpts = append(allOpts, cfg.grpcOptions...)
163+
allOpts = append(allOpts, opts...)
164+
165+
// Construct the gRPC server with the options.
166+
grpcServer := grpc.NewServer(allOpts...)
167+
168+
// Register various grpc servers with the LNC session
169+
// server.
170+
cfg.registerGrpcServers(grpcServer)
171+
172+
return grpcServer
173+
},
174+
)
175+
176+
s.cfg = cfg
177+
s.sessionServer = server
178+
170179
// Delete all sessions in the Reserved state.
171180
err := s.cfg.db.DeleteReservedSessions(ctx)
172181
if err != nil {
@@ -255,7 +264,9 @@ func (s *sessionRpcServer) start(ctx context.Context) error {
255264
func (s *sessionRpcServer) stop() error {
256265
var returnErr error
257266
s.stopOnce.Do(func() {
258-
s.sessionServer.Stop()
267+
if s.sessionServer != nil {
268+
s.sessionServer.Stop()
269+
}
259270

260271
close(s.quit)
261272
s.wg.Wait()

terminal.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,6 @@ func (g *LightningTerminal) start(ctx context.Context) error {
463463
return fmt.Errorf("error creating account service: %v", err)
464464
}
465465

466-
superMacBaker := func(ctx context.Context, rootKeyID uint64,
467-
perms []bakery.Op, caveats []macaroon.Caveat) (string, error) {
468-
469-
return litmac.BakeSuperMacaroon(
470-
ctx, g.basicClient, rootKeyID, perms, caveats,
471-
)
472-
}
473-
474466
// We create a reference to the `accountRpcServer` here before starting
475467
// it and prior to setting up the LND connection. This is because when
476468
// the LND connection is set up for an integrated LND instance, LND will
@@ -512,35 +504,11 @@ func (g *LightningTerminal) start(ctx context.Context) error {
512504
}
513505
}
514506

515-
g.sessionRpcServer, err = newSessionRPCServer(&sessionRpcServerConfig{
516-
db: g.stores.sessions,
517-
basicAuth: g.rpcProxy.basicAuth,
518-
grpcOptions: []grpc.ServerOption{
519-
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
520-
grpc.ChainStreamInterceptor(
521-
g.rpcProxy.StreamServerInterceptor,
522-
),
523-
grpc.ChainUnaryInterceptor(
524-
g.rpcProxy.UnaryServerInterceptor,
525-
),
526-
grpc.UnknownServiceHandler(
527-
grpcProxy.TransparentHandler(
528-
// Don't allow calls to litrpc.
529-
g.rpcProxy.makeDirector(false),
530-
),
531-
),
532-
},
533-
registerGrpcServers: func(server *grpc.Server) {
534-
g.registerSubDaemonGrpcServers(server, true)
535-
},
536-
superMacBaker: superMacBaker,
537-
firstConnectionDeadline: g.cfg.FirstLNCConnDeadline,
538-
permMgr: g.permsMgr,
539-
actionsDB: g.stores.firewall,
540-
autopilot: g.autopilotClient,
541-
ruleMgrs: g.ruleMgrs,
542-
privMap: g.stores.firewall,
543-
})
507+
// Similar to the init of the `accountRpcServer` reference above, we
508+
// create a reference to the `sessionRpcServer` here before setting up
509+
// the LND connection. See the comment above for the `accountRpcServer`
510+
// to understand why this is necessary.
511+
g.sessionRpcServer, err = newSessionRPCServer()
544512
if err != nil {
545513
return fmt.Errorf("could not create new session rpc "+
546514
"server: %v", err)
@@ -1066,7 +1034,38 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context,
10661034
}
10671035

10681036
log.Infof("Starting LiT session server")
1069-
if err = g.sessionRpcServer.start(ctx); err != nil {
1037+
1038+
sessionCfg := &sessionRpcServerConfig{
1039+
db: g.stores.sessions,
1040+
basicAuth: g.rpcProxy.basicAuth,
1041+
grpcOptions: []grpc.ServerOption{
1042+
grpc.CustomCodec(grpcProxy.Codec()), // nolint: staticcheck,
1043+
grpc.ChainStreamInterceptor(
1044+
g.rpcProxy.StreamServerInterceptor,
1045+
),
1046+
grpc.ChainUnaryInterceptor(
1047+
g.rpcProxy.UnaryServerInterceptor,
1048+
),
1049+
grpc.UnknownServiceHandler(
1050+
grpcProxy.TransparentHandler(
1051+
// Don't allow calls to litrpc.
1052+
g.rpcProxy.makeDirector(false),
1053+
),
1054+
),
1055+
},
1056+
registerGrpcServers: func(server *grpc.Server) {
1057+
g.registerSubDaemonGrpcServers(server, true)
1058+
},
1059+
superMacBaker: superMacBaker,
1060+
firstConnectionDeadline: g.cfg.FirstLNCConnDeadline,
1061+
permMgr: g.permsMgr,
1062+
actionsDB: g.stores.firewall,
1063+
autopilot: g.autopilotClient,
1064+
ruleMgrs: g.ruleMgrs,
1065+
privMap: g.stores.firewall,
1066+
}
1067+
1068+
if err = g.sessionRpcServer.start(ctx, sessionCfg); err != nil {
10701069
return err
10711070
}
10721071
g.sessionRpcServerStarted = true

0 commit comments

Comments
 (0)