Skip to content

Commit 64f56dc

Browse files
authored
Merge pull request #1126 from ViktorTigerstrom/2025-08-actions-migration-prep
[sql-47] Actions migration prep
2 parents 3ca5e7d + 3700cb4 commit 64f56dc

File tree

3 files changed

+226
-144
lines changed

3 files changed

+226
-144
lines changed

accounts/rpcserver.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package accounts
33
import (
44
"context"
55
"encoding/hex"
6+
"errors"
67
"fmt"
78
"time"
89

@@ -16,6 +17,13 @@ import (
1617
"gopkg.in/macaroon.v2"
1718
)
1819

20+
var (
21+
// ErrServerNotActive indicates that the server has started but hasn't
22+
// fully finished the startup process.
23+
ErrServerNotActive = errors.New("accounts server is still in the " +
24+
"process of starting")
25+
)
26+
1927
// RPCServer is the main server that implements the Accounts gRPC service.
2028
type RPCServer struct {
2129
litrpc.UnimplementedAccountsServer
@@ -26,13 +34,17 @@ type RPCServer struct {
2634
}
2735

2836
// NewRPCServer returns a new RPC server for the given service.
29-
func NewRPCServer(service *InterceptorService,
30-
superMacBaker litmac.Baker) *RPCServer {
37+
func NewRPCServer() *RPCServer {
38+
return &RPCServer{}
39+
}
3140

32-
return &RPCServer{
33-
service: service,
34-
superMacBaker: superMacBaker,
35-
}
41+
// Start adds the necessary dependencies for the RPCServer to be able to process
42+
// requests, and starts the RPCServer.
43+
func (s *RPCServer) Start(service *InterceptorService,
44+
superMacBaker litmac.Baker) {
45+
46+
s.service = service
47+
s.superMacBaker = superMacBaker
3648
}
3749

3850
// CreateAccount adds an entry to the account database. This entry represents

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()

0 commit comments

Comments
 (0)