Skip to content

Commit 774a44f

Browse files
committed
loopd: add StartAsSubserver method and started flag
1 parent 506d0c2 commit 774a44f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

loopd/daemon.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ func New(config *Config, lisCfg *listenerCfg) *Daemon {
100100
// execute commands and pass back swap status information.
101101
func (d *Daemon) Start() error {
102102
// There should be no reason to start the daemon twice. Therefore return
103-
// an error if that's tried.
103+
// an error if that's tried. This is mostly to guard against Start and
104+
// StartAsSubserver both being called.
104105
if atomic.AddInt32(&d.started, 1) != 1 {
105106
return errOnlyStartOnce
106107
}
@@ -136,6 +137,29 @@ func (d *Daemon) Start() error {
136137
return nil
137138
}
138139

140+
// StartAsSubserver is an alternative to Start where the RPC server does not
141+
// create its own gRPC server but registers to an existing one. The same goes
142+
// for REST (if enabled), instead of creating an own mux and HTTP server, we
143+
// register to an existing one.
144+
func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices) error {
145+
// There should be no reason to start the daemon twice. Therefore return
146+
// an error if that's tried. This is mostly to guard against Start and
147+
// StartAsSubserver both being called.
148+
if atomic.AddInt32(&d.started, 1) != 1 {
149+
return errOnlyStartOnce
150+
}
151+
152+
// When starting as a subserver, we get passed in an already established
153+
// connection to lnd that might be shared among other subservers.
154+
d.lnd = lndGrpc
155+
156+
// With lnd already pre-connected, initialize everything else, such as
157+
// the swap server client, the RPC server instance and our main swap
158+
// handlers. If this fails, then nothing has been started yet and we can
159+
// just return the error.
160+
return d.initialize()
161+
}
162+
139163
// startWebServers starts the gRPC and REST servers in goroutines.
140164
func (d *Daemon) startWebServers() error {
141165
var err error

0 commit comments

Comments
 (0)