Skip to content

Commit

Permalink
use a context for cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
jehiah committed Nov 27, 2020
1 parent dd44ba1 commit e629e4c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions apps/nsqd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"flag"
"fmt"
"math/rand"
Expand Down Expand Up @@ -95,6 +96,11 @@ func (p *program) Stop() error {
return nil
}

// Context returns a context that will be canceled when nsqd initiates the shutdown
func (p program) Context() context.Context {
return p.nsqd.Context()
}

func logFatal(f string, args ...interface{}) {
lg.LogFatal("[nsqd] ", f, args...)
}
9 changes: 9 additions & 0 deletions nsqd/nsqd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nsqd

import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand Down Expand Up @@ -46,6 +47,8 @@ type NSQD struct {
clientIDSequence int64

sync.RWMutex
ctx context.Context
ctxCancel context.CancelFunc

opts atomic.Value

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

Expand Down Expand Up @@ -786,3 +790,8 @@ func buildTLSConfig(opts *Options) (*tls.Config, error) {
func (n *NSQD) IsAuthEnabled() bool {
return len(n.getOpts().AuthHTTPAddresses) != 0
}

// Context returns a context that will be canceled when nsqd initiates the shutdown
func (n *NSQD) Context() context.Context {
return n.ctx
}

0 comments on commit e629e4c

Please sign in to comment.