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 05b8ce8348
commit 6ffefb649d
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
c.WaitGroup = &sync.WaitGroup{}
// Wait for shutdown
shutdownTriggered := false
// If the backend supports service discovery, run service discovery
if coreConfig.HAPhysical != nil && coreConfig.HAPhysical.HAEnabled() {
@ -396,7 +394,7 @@ func (c *ServerCommand) Run(args []string) int {
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))
return 1
}
@ -421,6 +419,9 @@ func (c *ServerCommand) Run(args []string) int {
// Release the log gate.
logGate.Flush()
// Wait for shutdown
shutdownTriggered := false
for !shutdownTriggered {
select {
case <-c.ShutdownCh:
@ -757,10 +758,8 @@ func MakeShutdownCh() chan struct{} {
shutdownCh := make(chan os.Signal, 4)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
go func() {
for {
<-shutdownCh
resultCh <- struct{}{}
}
<-shutdownCh
close(resultCh)
}()
return resultCh
}

View File

@ -416,7 +416,7 @@ func (c *ConsulBackend) checkDuration() time.Duration {
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 {
return err
}
@ -424,12 +424,12 @@ func (c *ConsulBackend) RunServiceDiscovery(shutdownTriggered *bool, waitGroup *
// 'server' command will wait for the below goroutine to complete
waitGroup.Add(1)
go c.runEventDemuxer(shutdownTriggered, waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc)
go c.runEventDemuxer(waitGroup, shutdownCh, advertiseAddr, activeFunc, sealedFunc)
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.
defer waitGroup.Done()
@ -456,7 +456,7 @@ func (c *ConsulBackend) runEventDemuxer(shutdownTriggered *bool, waitGroup *sync
var checkLock int64
var registeredServiceID string
var serviceRegLock int64
shutdown:
for !shutdown {
select {
case <-c.notifyActiveCh:
@ -513,9 +513,7 @@ shutdown:
case <-shutdownCh:
c.logger.Printf("[INFO]: physical/consul: Shutting down consul backend")
shutdown = true
break shutdown
}
shutdown = *shutdownTriggered
}
c.serviceLock.RLock()

View File

@ -72,7 +72,7 @@ type ServiceDiscovery interface {
// Run executes any background service discovery tasks until the
// 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 {