@@ -40,6 +40,13 @@ import (
4040// other special cases.
4141const 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.
4451type 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 {
255264func (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 ()
0 commit comments