Skip to content

Commit ad73d05

Browse files
committed
update go-svc v1.2.1; expose context.Context from nsqd
1 parent 8adb229 commit ad73d05

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

apps/nsqadmin/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"syscall"
1010

1111
"github.com/BurntSushi/toml"
12-
"github.com/judwhite/go-svc/svc"
12+
"github.com/judwhite/go-svc"
1313
"github.com/mreiferson/go-options"
1414
"github.com/nsqio/nsq/internal/app"
1515
"github.com/nsqio/nsq/internal/lg"

apps/nsqd/main.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"math/rand"
78
"os"
9+
"os/signal"
810
"path/filepath"
911
"sync"
1012
"syscall"
1113
"time"
1214

1315
"github.com/BurntSushi/toml"
14-
"github.com/judwhite/go-svc/svc"
16+
"github.com/judwhite/go-svc"
1517
"github.com/mreiferson/go-options"
1618
"github.com/nsqio/nsq/internal/lg"
1719
"github.com/nsqio/nsq/internal/version"
@@ -25,7 +27,8 @@ type program struct {
2527

2628
func main() {
2729
prg := &program{}
28-
if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil {
30+
// SIGTERM handling is in Start()
31+
if err := svc.Run(prg, syscall.SIGINT); err != nil {
2932
logFatal("%s", err)
3033
}
3134
}
@@ -62,6 +65,7 @@ func (p *program) Start() error {
6265
cfg.Validate()
6366

6467
options.Resolve(opts, flagSet, cfg)
68+
6569
nsqd, err := nsqd.New(opts)
6670
if err != nil {
6771
logFatal("failed to instantiate nsqd - %s", err)
@@ -77,6 +81,17 @@ func (p *program) Start() error {
7781
logFatal("failed to persist metadata - %s", err)
7882
}
7983

84+
signalChan := make(chan os.Signal, 1)
85+
go func() {
86+
// range over all term signals
87+
// we don't want to un-register our sigterm handler which would
88+
// cause default go behavior to apply
89+
for range signalChan {
90+
p.nsqd.TermSignal()
91+
}
92+
}()
93+
signal.Notify(signalChan, syscall.SIGTERM)
94+
8095
go func() {
8196
err := p.nsqd.Main()
8297
if err != nil {
@@ -95,6 +110,11 @@ func (p *program) Stop() error {
95110
return nil
96111
}
97112

113+
// Context returns a context that will be canceled when nsqd initiates the shutdown
114+
func (p *program) Context() context.Context {
115+
return p.nsqd.Context()
116+
}
117+
98118
func logFatal(f string, args ...interface{}) {
99119
lg.LogFatal("[nsqd] ", f, args...)
100120
}

apps/nsqlookupd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"syscall"
1010

1111
"github.com/BurntSushi/toml"
12-
"github.com/judwhite/go-svc/svc"
12+
"github.com/judwhite/go-svc"
1313
"github.com/mreiferson/go-options"
1414
"github.com/nsqio/nsq/internal/lg"
1515
"github.com/nsqio/nsq/internal/version"

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module github.com/nsqio/nsq
22

3+
go 1.13
4+
35
require (
46
github.com/BurntSushi/toml v0.3.1
57
github.com/bitly/go-hostpool v0.1.0
@@ -8,12 +10,9 @@ require (
810
github.com/bmizerany/perks v0.0.0-20141205001514-d9a9656a3a4b
911
github.com/davecgh/go-spew v1.1.1 // indirect
1012
github.com/golang/snappy v0.0.1
11-
github.com/judwhite/go-svc v1.1.2
13+
github.com/judwhite/go-svc v1.2.1
1214
github.com/julienschmidt/httprouter v1.3.0
1315
github.com/mreiferson/go-options v1.0.0
1416
github.com/nsqio/go-diskqueue v1.0.0
1517
github.com/nsqio/go-nsq v1.0.8
16-
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 // indirect
1718
)
18-
19-
go 1.13

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
1313
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1414
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
1515
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
16-
github.com/judwhite/go-svc v1.1.2 h1:wKroC8SKFs2EmtoS3XVmZinnRtGmu9qVrjubFp8talY=
17-
github.com/judwhite/go-svc v1.1.2/go.mod h1:EeMSAFO3mLgEQfcvnZ50JDG0O1uQlagpAbMS6talrXE=
16+
github.com/judwhite/go-svc v1.2.1 h1:a7fsJzYUa33sfDJRF2N/WXhA+LonCEEY8BJb1tuS5tA=
17+
github.com/judwhite/go-svc v1.2.1/go.mod h1:mo/P2JNX8C07ywpP9YtO2gnBgnUiFTHqtsZekJrUuTk=
1818
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
1919
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
2020
github.com/mreiferson/go-options v1.0.0 h1:RMLidydGlDWpL+lQTXo0bVIf/XT2CTq7AEJMoz5/VWs=
@@ -28,8 +28,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
2828
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2929
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
3030
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
31-
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76 h1:Dho5nD6R3PcW2SH1or8vS0dszDaXRxIw55lBX7XiE5g=
32-
golang.org/x/sys v0.0.0-20191224085550-c709ea063b76/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=
32+
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3333
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3434
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3535
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

nsqd/nsqd.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nsqd
22

33
import (
4+
"context"
45
"crypto/tls"
56
"crypto/x509"
67
"encoding/json"
@@ -46,6 +47,9 @@ type NSQD struct {
4647
clientIDSequence int64
4748

4849
sync.RWMutex
50+
ctx context.Context
51+
// ctxCancel cancels a context that main() is waiting on
52+
ctxCancel context.CancelFunc
4953

5054
opts atomic.Value
5155

@@ -98,6 +102,7 @@ func New(opts *Options) (*NSQD, error) {
98102
optsNotificationChan: make(chan struct{}, 1),
99103
dl: dirlock.New(dataPath),
100104
}
105+
n.ctx, n.ctxCancel = context.WithCancel(context.Background())
101106
httpcli := http_api.NewClient(nil, opts.HTTPClientConnectTimeout, opts.HTTPClientRequestTimeout)
102107
n.ci = clusterinfo.New(n.logf, httpcli)
103108

@@ -425,6 +430,12 @@ func (n *NSQD) PersistMetadata() error {
425430
return nil
426431
}
427432

433+
// TermSignal handles a SIGTERM calling Exit
434+
// This is a noop after first call
435+
func (n *NSQD) TermSignal() {
436+
n.Exit()
437+
}
438+
428439
func (n *NSQD) Exit() {
429440
if n.tcpListener != nil {
430441
n.tcpListener.Close()
@@ -457,6 +468,7 @@ func (n *NSQD) Exit() {
457468
n.waitGroup.Wait()
458469
n.dl.Unlock()
459470
n.logf(LOG_INFO, "NSQ: bye")
471+
n.ctxCancel()
460472
}
461473

462474
// GetTopic performs a thread safe operation
@@ -756,3 +768,8 @@ func buildTLSConfig(opts *Options) (*tls.Config, error) {
756768
func (n *NSQD) IsAuthEnabled() bool {
757769
return len(n.getOpts().AuthHTTPAddresses) != 0
758770
}
771+
772+
// Context returns a context that will be canceled when nsqd initiates the shutdown
773+
func (n *NSQD) Context() context.Context {
774+
return n.ctx
775+
}

0 commit comments

Comments
 (0)