Skip to content

Commit 0ab5cb7

Browse files
committed
subservers: embed Subserver in subServerWrapper
1 parent 3ce40f5 commit 0ab5cb7

File tree

2 files changed

+28
-31
lines changed

2 files changed

+28
-31
lines changed

subservers/manager.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (s *Manager) AddServer(ss SubServer) {
4646
defer s.mu.Unlock()
4747

4848
s.servers = append(s.servers, &subServerWrapper{
49-
subServer: ss,
49+
SubServer: ss,
5050
quit: make(chan struct{}),
5151
})
5252
}
@@ -60,16 +60,16 @@ func (s *Manager) StartIntegratedServers(lndClient lnrpc.LightningClient,
6060
defer s.mu.Unlock()
6161

6262
for _, ss := range s.servers {
63-
if ss.subServer.Remote() {
63+
if ss.Remote() {
6464
continue
6565
}
6666

6767
err := ss.startIntegrated(
6868
lndClient, lndGrpc, withMacaroonService,
6969
)
7070
if err != nil {
71-
return fmt.Errorf("Unable to start %v in integrated "+
72-
"mode: %v", ss.subServer.Name(), err)
71+
return fmt.Errorf("unable to start %v in integrated "+
72+
"mode: %v", ss.Name(), err)
7373
}
7474
}
7575

@@ -83,14 +83,14 @@ func (s *Manager) ConnectRemoteSubServers() {
8383
defer s.mu.Unlock()
8484

8585
for _, ss := range s.servers {
86-
if !ss.subServer.Remote() {
86+
if !ss.Remote() {
8787
continue
8888
}
8989

9090
err := ss.connectRemote()
9191
if err != nil {
9292
log.Errorf("Failed to connect to remote %s: %v",
93-
ss.subServer.Name(), err)
93+
ss.Name(), err)
9494

9595
continue
9696
}
@@ -108,11 +108,11 @@ func (s *Manager) RegisterRPCServices(server grpc.ServiceRegistrar) {
108108
// a catch-all for any gRPC request that isn't known because we
109109
// didn't register any server for it. The director will then
110110
// forward the request to the remote service.
111-
if ss.subServer.Remote() {
111+
if ss.Remote() {
112112
continue
113113
}
114114

115-
ss.subServer.RegisterGrpcService(server)
115+
ss.RegisterGrpcService(server)
116116
}
117117
}
118118

@@ -125,11 +125,11 @@ func (s *Manager) GetRemoteConn(uri string) (bool, *grpc.ClientConn) {
125125
defer s.mu.RUnlock()
126126

127127
for _, ss := range s.servers {
128-
if !s.permsMgr.IsSubServerURI(ss.subServer.Name(), uri) {
128+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
129129
continue
130130
}
131131

132-
if !ss.subServer.Remote() {
132+
if !ss.Remote() {
133133
return false, nil
134134
}
135135

@@ -151,15 +151,15 @@ func (s *Manager) ValidateMacaroon(ctx context.Context,
151151
defer s.mu.RUnlock()
152152

153153
for _, ss := range s.servers {
154-
if !s.permsMgr.IsSubServerURI(ss.subServer.Name(), uri) {
154+
if !s.permsMgr.IsSubServerURI(ss.Name(), uri) {
155155
continue
156156
}
157157

158158
// If the sub-server is running in remote mode, then we don't
159159
// need to validate the macaroon here since the remote server
160160
// will do it when the request arrives. But we have handled the
161161
// request, as we were able to identify it.
162-
if ss.subServer.Remote() {
162+
if ss.Remote() {
163163
return true, nil
164164
}
165165

@@ -170,14 +170,12 @@ func (s *Manager) ValidateMacaroon(ctx context.Context,
170170
return true, fmt.Errorf("%s is not yet ready for "+
171171
"requests, the subserver has not started or "+
172172
"lnd still starting/syncing",
173-
ss.subServer.Name())
173+
ss.Name())
174174
}
175175

176176
// Validate the macaroon with the integrated sub-server's own
177177
// validator.
178-
err := ss.subServer.ValidateMacaroon(
179-
ctx, requiredPermissions, uri,
180-
)
178+
err := ss.ValidateMacaroon(ctx, requiredPermissions, uri)
181179
if err != nil {
182180
return true, fmt.Errorf("invalid macaroon: %v", err)
183181
}
@@ -199,14 +197,13 @@ func (s *Manager) Stop() error {
199197
defer s.mu.RUnlock()
200198

201199
for _, ss := range s.servers {
202-
if ss.subServer.Remote() {
200+
if ss.Remote() {
203201
continue
204202
}
205203

206204
err := ss.stop()
207205
if err != nil {
208-
log.Errorf("Error stopping %s: %v", ss.subServer.Name(),
209-
err)
206+
log.Errorf("Error stopping %s: %v", ss.Name(), err)
210207
returnErr = err
211208
}
212209
}

subservers/subserver.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ const (
2121
// subServerWrapper is a wrapper around the SubServer interface and is used by
2222
// the subServerMgr to manage a SubServer.
2323
type subServerWrapper struct {
24+
SubServer
25+
2426
integratedStarted bool
2527
startedMu sync.RWMutex
2628

2729
stopped sync.Once
2830

29-
subServer SubServer
30-
3131
remoteConn *grpc.ClientConn
3232

3333
wg sync.WaitGroup
@@ -66,7 +66,7 @@ func (s *subServerWrapper) stop() error {
6666
s.wg.Wait()
6767

6868
// If running in remote mode, close the connection.
69-
if s.subServer.Remote() && s.remoteConn != nil {
69+
if s.Remote() && s.remoteConn != nil {
7070
err := s.remoteConn.Close()
7171
if err != nil {
7272
returnErr = fmt.Errorf("could not close "+
@@ -76,19 +76,19 @@ func (s *subServerWrapper) stop() error {
7676
}
7777

7878
// Else, stop the integrated sub-server process.
79-
err := s.subServer.Stop()
79+
err := s.Stop()
8080
if err != nil {
8181
returnErr = fmt.Errorf("could not close "+
8282
"integrated connection: %v", err)
8383
return
8484
}
8585

86-
if s.subServer.ServerErrChan() == nil {
86+
if s.ServerErrChan() == nil {
8787
return
8888
}
8989

9090
select {
91-
case returnErr = <-s.subServer.ServerErrChan():
91+
case returnErr = <-s.ServerErrChan():
9292
default:
9393
}
9494
})
@@ -100,13 +100,13 @@ func (s *subServerWrapper) stop() error {
100100
func (s *subServerWrapper) startIntegrated(lndClient lnrpc.LightningClient,
101101
lndGrpc *lndclient.GrpcLndServices, withMacaroonService bool) error {
102102

103-
err := s.subServer.Start(lndClient, lndGrpc, withMacaroonService)
103+
err := s.Start(lndClient, lndGrpc, withMacaroonService)
104104
if err != nil {
105105
return err
106106
}
107107
s.setStarted(true)
108108

109-
if s.subServer.ServerErrChan() == nil {
109+
if s.ServerErrChan() == nil {
110110
return nil
111111
}
112112

@@ -115,14 +115,14 @@ func (s *subServerWrapper) startIntegrated(lndClient lnrpc.LightningClient,
115115
defer s.wg.Done()
116116

117117
select {
118-
case err := <-s.subServer.ServerErrChan():
118+
case err := <-s.ServerErrChan():
119119
// The sub server should shut itself down if an error
120120
// happens. We don't need to try to stop it again.
121121
s.setStarted(false)
122122

123123
err = fmt.Errorf("received critical error from "+
124124
"sub-server (%s), shutting down: %v",
125-
s.subServer.Name(), err)
125+
s.Name(), err)
126126

127127
log.Error(err)
128128

@@ -135,9 +135,9 @@ func (s *subServerWrapper) startIntegrated(lndClient lnrpc.LightningClient,
135135

136136
// connectRemote attempts to make a connection to the remote sub-server.
137137
func (s *subServerWrapper) connectRemote() error {
138-
cfg := s.subServer.RemoteConfig()
138+
cfg := s.RemoteConfig()
139139
certPath := lncfg.CleanAndExpandPath(cfg.TLSCertPath)
140-
name := s.subServer.Name()
140+
name := s.Name()
141141
conn, err := dialBackend(name, cfg.RPCServer, certPath)
142142
if err != nil {
143143
return fmt.Errorf("remote dial error: %v", err)

0 commit comments

Comments
 (0)