Skip to content

Commit e4c7257

Browse files
committed
use a context for cancelation
1 parent 91340ab commit e4c7257

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

apps/nsqd/main.go

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

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"math/rand"
@@ -95,6 +96,11 @@ func (p *program) Stop() error {
9596
return nil
9697
}
9798

99+
// Context returns a context that will be canceled when nsqd initiates the shutdown
100+
func (p program) Context() context.Context {
101+
return p.nsqd.Context()
102+
}
103+
98104
func logFatal(f string, args ...interface{}) {
99105
lg.LogFatal("[nsqd] ", f, args...)
100106
}

nsqd/nsqd.go

Lines changed: 9 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,8 @@ type NSQD struct {
4647
clientIDSequence int64
4748

4849
sync.RWMutex
50+
ctx context.Context
51+
ctxCancel context.CancelFunc
4952

5053
opts atomic.Value
5154

@@ -99,6 +102,7 @@ func New(opts *Options) (*NSQD, error) {
99102
optsNotificationChan: make(chan struct{}, 1),
100103
dl: dirlock.New(dataPath),
101104
}
105+
n.ctx, n.ctxCancel = context.WithCancel(context.Background())
102106
httpcli := http_api.NewClient(nil, opts.HTTPClientConnectTimeout, opts.HTTPClientRequestTimeout)
103107
n.ci = clusterinfo.New(n.logf, httpcli)
104108

@@ -786,3 +790,8 @@ func buildTLSConfig(opts *Options) (*tls.Config, error) {
786790
func (n *NSQD) IsAuthEnabled() bool {
787791
return len(n.getOpts().AuthHTTPAddresses) != 0
788792
}
793+
794+
// Context returns a context that will be canceled when nsqd initiates the shutdown
795+
func (n *NSQD) Context() context.Context {
796+
return n.ctx
797+
}

0 commit comments

Comments
 (0)