Close the shutdown channel instead of sending a value down

This commit is contained in:
Jeff Mitchell 2016-08-01 11:58:45 -04:00
parent 577cd9de35
commit 32b39e808b
3 changed files with 11 additions and 14 deletions

View File

@ -375,8 +375,6 @@ func (c *ServerCommand) Run(args []string) int {
// Instantiate the wait group // Instantiate the wait group
c.WaitGroup = &sync.WaitGroup{} c.WaitGroup = &sync.WaitGroup{}
// Wait for shutdown
shutdownTriggered := false
// If the backend supports service discovery, run service discovery // If the backend supports service discovery, run service discovery
if coreConfig.HAPhysical != nil && coreConfig.HAPhysical.HAEnabled() { if coreConfig.HAPhysical != nil && coreConfig.HAPhysical.HAEnabled() {
@ -396,7 +394,7 @@ func (c *ServerCommand) Run(args []string) int {
return true return true
} }
if err := sd.RunServiceDiscovery(&shutdownTriggered, c.WaitGroup, c.ShutdownCh, coreConfig.AdvertiseAddr, activeFunc, sealedFunc); err != nil { if err := sd.RunServiceDiscovery(c.WaitGroup, c.ShutdownCh, coreConfig.AdvertiseAddr, activeFunc, sealedFunc); err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing service discovery: %v", err)) c.Ui.Error(fmt.Sprintf("Error initializing service discovery: %v", err))
return 1 return 1
} }
@ -421,6 +419,9 @@ func (c *ServerCommand) Run(args []string) int {
// Release the log gate. // Release the log gate.
logGate.Flush() logGate.Flush()
// Wait for shutdown
shutdownTriggered := false
for !shutdownTriggered { for !shutdownTriggered {
select { select {
case <-c.ShutdownCh: case <-c.ShutdownCh:
@ -757,10 +758,8 @@ func MakeShutdownCh() chan struct{} {
shutdownCh := make(chan os.Signal, 4) shutdownCh := make(chan os.Signal, 4)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM) signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
for {
<-shutdownCh <-shutdownCh
resultCh <- struct{}{} close(resultCh)
}
}() }()
return resultCh return resultCh
} }

View File

@ -416,7 +416,7 @@ func (c *ConsulBackend) checkDuration() time.Duration {
return lib.DurationMinusBuffer(c.checkTimeout, checkMinBuffer, checkJitterFactor) return lib.DurationMinusBuffer(c.checkTimeout, checkMinBuffer, checkJitterFactor)
} }
func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) (err error) { func (c *ConsulBackend) RunServiceDiscovery(waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) (err error) {
if err := c.setAdvertiseAddr(advertiseAddr); err != nil { if err := c.setAdvertiseAddr(advertiseAddr); err != nil {
return err return err
} }
@ -424,12 +424,12 @@ func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup *
// 'server' command will wait for the below goroutine to complete // 'server' command will wait for the below goroutine to complete
waitGroup.Add(1) waitGroup.Add(1)
go c.runEventDemuxer(shutdownTriggered, waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc) go c.runEventDemuxer(waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc)
return nil return nil
} }
func (c *ConsulBackend) runEventDemuxer(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) { func (c *ConsulBackend) runEventDemuxer(waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) {
// This defer statement should be executed last. So push it first. // This defer statement should be executed last. So push it first.
defer waitGroup.Done() defer waitGroup.Done()
@ -456,7 +456,7 @@ func (c *ConsulBackend) runEventDemuxer(shutdownTriggered *bool, waitGroup *sync
var checkLock int64 var checkLock int64
var registeredServiceID string var registeredServiceID string
var serviceRegLock int64 var serviceRegLock int64
shutdown:
for !shutdown { for !shutdown {
select { select {
case <-c.notifyActiveCh: case <-c.notifyActiveCh:
@ -513,9 +513,7 @@ shutdown:
case <-shutdownCh: case <-shutdownCh:
c.logger.Printf("[INFO]: physical/consul: Shutting down consul backend") c.logger.Printf("[INFO]: physical/consul: Shutting down consul backend")
shutdown = true shutdown = true
break shutdown
} }
shutdown = *shutdownTriggered
} }
c.serviceLock.RLock() c.serviceLock.RLock()

View File

@ -72,7 +72,7 @@ type ServiceDiscovery interface {
// Run executes any background service discovery tasks until the // Run executes any background service discovery tasks until the
// shutdown channel is closed. // shutdown channel is closed.
RunServiceDiscovery(shutdownTriggered *bool, waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) error RunServiceDiscovery(waitGroup *sync.WaitGroup, shutdownCh ShutdownChannel, advertiseAddr string, activeFunc activeFunction, sealedFunc sealedFunction) error
} }
type Lock interface { type Lock interface {