Merge pull request #590 from neilalexander/multicast

No longer use atomic for isOpen in multicast
This commit is contained in:
Arceliar 2019-10-24 21:47:42 -05:00 committed by GitHub
commit 4c7d04941a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"net" "net"
"regexp" "regexp"
"sync/atomic"
"time" "time"
"github.com/Arceliar/phony" "github.com/Arceliar/phony"
@ -29,7 +28,7 @@ type Multicast struct {
groupAddr string groupAddr string
listeners map[string]*listenerInfo listeners map[string]*listenerInfo
listenPort uint16 listenPort uint16
isOpen atomic.Value // bool isOpen bool
announcer *time.Timer announcer *time.Timer
platformhandler *time.Timer platformhandler *time.Timer
} }
@ -49,7 +48,6 @@ func (m *Multicast) Init(core *yggdrasil.Core, state *config.NodeState, log *log
current := m.config.GetCurrent() current := m.config.GetCurrent()
m.listenPort = current.LinkLocalTCPPort m.listenPort = current.LinkLocalTCPPort
m.groupAddr = "[ff02::114]:9001" m.groupAddr = "[ff02::114]:9001"
m.isOpen.Store(false)
return nil return nil
} }
@ -66,7 +64,7 @@ func (m *Multicast) Start() error {
} }
func (m *Multicast) _start() error { func (m *Multicast) _start() error {
if m.IsStarted() { if m.isOpen {
return fmt.Errorf("multicast module is already started") return fmt.Errorf("multicast module is already started")
} }
if len(m.config.GetCurrent().MulticastInterfaces) == 0 { if len(m.config.GetCurrent().MulticastInterfaces) == 0 {
@ -90,7 +88,7 @@ func (m *Multicast) _start() error {
// Windows can't set this flag, so we need to handle it in other ways // Windows can't set this flag, so we need to handle it in other ways
} }
m.isOpen.Store(true) m.isOpen = true
go m.listen() go m.listen()
m.Act(m, m.multicastStarted) m.Act(m, m.multicastStarted)
m.Act(m, m.announce) m.Act(m, m.announce)
@ -100,10 +98,11 @@ func (m *Multicast) _start() error {
// IsStarted returns true if the module has been started. // IsStarted returns true if the module has been started.
func (m *Multicast) IsStarted() bool { func (m *Multicast) IsStarted() bool {
if m.isOpen.Load() == nil { var isOpen bool
return false phony.Block(m, func() {
} isOpen = m.isOpen
return m.isOpen.Load().(bool) })
return isOpen
} }
// Stop stops the multicast module. // Stop stops the multicast module.
@ -118,7 +117,7 @@ func (m *Multicast) Stop() error {
func (m *Multicast) _stop() error { func (m *Multicast) _stop() error {
m.log.Infoln("Stopping multicast module") m.log.Infoln("Stopping multicast module")
m.isOpen.Store(false) m.isOpen = false
if m.announcer != nil { if m.announcer != nil {
m.announcer.Stop() m.announcer.Stop()
} }
@ -138,7 +137,7 @@ func (m *Multicast) UpdateConfig(config *config.NodeConfig) {
func (m *Multicast) _updateConfig(config *config.NodeConfig) { func (m *Multicast) _updateConfig(config *config.NodeConfig) {
m.log.Infoln("Reloading multicast configuration...") m.log.Infoln("Reloading multicast configuration...")
if m.IsStarted() { if m.isOpen {
if len(config.MulticastInterfaces) == 0 || config.LinkLocalTCPPort != m.listenPort { if len(config.MulticastInterfaces) == 0 || config.LinkLocalTCPPort != m.listenPort {
if err := m._stop(); err != nil { if err := m._stop(); err != nil {
m.log.Errorln("Error stopping multicast module:", err) m.log.Errorln("Error stopping multicast module:", err)
@ -147,7 +146,7 @@ func (m *Multicast) _updateConfig(config *config.NodeConfig) {
} }
m.config.Replace(*config) m.config.Replace(*config)
m.listenPort = config.LinkLocalTCPPort m.listenPort = config.LinkLocalTCPPort
if !m.IsStarted() && len(config.MulticastInterfaces) > 0 { if !m.isOpen && len(config.MulticastInterfaces) > 0 {
if err := m._start(); err != nil { if err := m._start(); err != nil {
m.log.Errorln("Error starting multicast module:", err) m.log.Errorln("Error starting multicast module:", err)
} }