Skip to content

Commit

Permalink
alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Mar 28, 2021
1 parent 1ced836 commit 954e32b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
19 changes: 5 additions & 14 deletions apps/nsqd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
Expand All @@ -27,8 +26,7 @@ type program struct {

func main() {
prg := &program{}
// SIGTERM handling is in Start()
if err := svc.Run(prg, syscall.SIGINT); err != nil {
if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil {
logFatal("%s", err)
}
}
Expand Down Expand Up @@ -81,17 +79,6 @@ func (p *program) Start() error {
logFatal("failed to persist metadata - %s", err)
}

signalChan := make(chan os.Signal, 1)
go func() {
// range over all term signals
// we don't want to un-register our sigterm handler which would
// cause default go behavior to apply
for range signalChan {
p.nsqd.TermSignal()
}
}()
signal.Notify(signalChan, syscall.SIGTERM)

go func() {
err := p.nsqd.Main()
if err != nil {
Expand All @@ -103,6 +90,10 @@ func (p *program) Start() error {
return nil
}

func (p *program) Handle(sig os.Signal) error {
return svc.ErrStop
}

func (p *program) Stop() error {
p.once.Do(func() {
p.nsqd.Exit()
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ require (
github.com/nsqio/go-diskqueue v1.0.0
github.com/nsqio/go-nsq v1.0.8
)

replace github.com/judwhite/go-svc => /Users/mreiferson/dev/src/github.com/judwhite/go-svc
11 changes: 0 additions & 11 deletions nsqd/nsqd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type NSQD struct {

dl *dirlock.DirLock
isLoading int32
isExiting int32
errValue atomic.Value
startTime time.Time

Expand Down Expand Up @@ -403,17 +402,7 @@ func (n *NSQD) PersistMetadata() error {
return nil
}

// TermSignal handles a SIGTERM calling Exit
// This is a noop after first call
func (n *NSQD) TermSignal() {
n.Exit()
}

func (n *NSQD) Exit() {
if !atomic.CompareAndSwapInt32(&n.isExiting, 0, 1) {
// avoid double call
return
}
if n.tcpListener != nil {
n.tcpListener.Close()
}
Expand Down

0 comments on commit 954e32b

Please sign in to comment.