@@ -40,6 +40,13 @@ import (
40
40
// other special cases.
41
41
const readOnlyAction = "***readonly***"
42
42
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
+
43
50
// sessionRpcServer is the gRPC server for the Session RPC interface.
44
51
type sessionRpcServer struct {
45
52
litrpc.UnimplementedSessionsServer
@@ -70,42 +77,11 @@ type sessionRpcServerConfig struct {
70
77
privMap firewalldb.PrivacyMapper
71
78
}
72
79
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 {
104
82
return & sessionRpcServer {
105
- cfg : cfg ,
106
- sessionServer : server ,
107
- quit : make (chan struct {}),
108
- }, nil
83
+ quit : make (chan struct {}),
84
+ }
109
85
}
110
86
111
87
// wrappedServerStream is a wrapper around the grpc.ServerStream that allows us
@@ -164,9 +140,42 @@ func addSessionIDToUnaryCtx(id session.ID) grpc.ServerOption {
164
140
})
165
141
}
166
142
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
+
170
179
// Delete all sessions in the Reserved state.
171
180
err := s .cfg .db .DeleteReservedSessions (ctx )
172
181
if err != nil {
@@ -255,7 +264,9 @@ func (s *sessionRpcServer) start(ctx context.Context) error {
255
264
func (s * sessionRpcServer ) stop () error {
256
265
var returnErr error
257
266
s .stopOnce .Do (func () {
258
- s .sessionServer .Stop ()
267
+ if s .sessionServer != nil {
268
+ s .sessionServer .Stop ()
269
+ }
259
270
260
271
close (s .quit )
261
272
s .wg .Wait ()
0 commit comments