multi: replace manual CAS with sync.Once in component start/stop

This guarantees callers that the method will not return until it
has executed completely at least once.
This commit is contained in:
Federico Bond committed 2019-05-30 17:14:04 -03:00
1 parent f802ebddba
commit aea52f4bef
5 files changed
+68 -70

No files matched your search

+12 -19
View File
@@ -5,7 +5,6 @@ import (
"fmt"
"net"
"sync"
"sync/atomic"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
@@ -79,8 +78,8 @@ type ChannelNotifier interface {
//
// TODO(roasbeef): better name lol
type SubSwapper struct {
started uint32
stopped uint32
started sync.Once
stopped sync.Once
// backupState are the set of SCBs for all open channels we know of.
backupState map[wire.OutPoint]Single
@@ -135,29 +134,23 @@ func NewSubSwapper(startingChans []Single, chanNotifier ChannelNotifier,
// Start starts the chanbackup.SubSwapper.
func (s *SubSwapper) Start() error {
if !atomic.CompareAndSwapUint32(&s.started, 0, 1) {
return nil
}
log.Infof("Starting chanbackup.SubSwapper")
s.wg.Add(1)
go s.backupUpdater()
s.started.Do(func() {
log.Infof("Starting chanbackup.SubSwapper")
s.wg.Add(1)
go s.backupUpdater()
})
return nil
}
// Stop signals the SubSwapper to being a graceful shutdown.
func (s *SubSwapper) Stop() error {
if !atomic.CompareAndSwapUint32(&s.stopped, 0, 1) {
return nil
}
log.Infof("Stopping chanbackup.SubSwapper")
close(s.quit)
s.wg.Wait()
s.stopped.Do(func() {
log.Infof("Stopping chanbackup.SubSwapper")
close(s.quit)
s.wg.Wait()
})
return nil
}