Skip to content

Commit 38a4496

Browse files
committed
loopd: atomic var to enforce daemon singelton
1 parent e98f733 commit 38a4496

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

loopd/daemon.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ type Daemon struct {
7070
// same process.
7171
swapClientServer
7272

73-
// To be used atomically.
74-
started int32
73+
started atomic.Bool
7574

7675
// ErrChan is an error channel that users of the Daemon struct must use
7776
// to detect runtime errors and also whether a shutdown is fully
@@ -128,7 +127,7 @@ func (d *Daemon) Start() error {
128127
// There should be no reason to start the daemon twice. Therefore,
129128
// return an error if that's tried. This is mostly to guard against
130129
// Start and StartAsSubserver both being called.
131-
if atomic.AddInt32(&d.started, 1) != 1 {
130+
if !d.started.CompareAndSwap(false, true) {
132131
return errOnlyStartOnce
133132
}
134133

@@ -190,7 +189,7 @@ func (d *Daemon) StartAsSubserver(lndGrpc *lndclient.GrpcLndServices,
190189
// There should be no reason to start the daemon twice. Therefore,
191190
// return an error if that's tried. This is mostly to guard against
192191
// Start and StartAsSubserver both being called.
193-
if atomic.AddInt32(&d.started, 1) != 1 {
192+
if !d.started.CompareAndSwap(false, true) {
194193
return errOnlyStartOnce
195194
}
196195

0 commit comments

Comments
 (0)