-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
*: update go-svc #1319
*: update go-svc #1319
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"os/signal" | ||
"path/filepath" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/judwhite/go-svc/svc" | ||
"github.com/judwhite/go-svc" | ||
"github.com/mreiferson/go-options" | ||
"github.com/nsqio/nsq/internal/lg" | ||
"github.com/nsqio/nsq/internal/version" | ||
|
@@ -25,7 +27,8 @@ type program struct { | |
|
||
func main() { | ||
prg := &program{} | ||
if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil { | ||
// SIGTERM handling is in Start() | ||
if err := svc.Run(prg, syscall.SIGINT); err != nil { | ||
logFatal("%s", err) | ||
} | ||
} | ||
|
@@ -62,6 +65,7 @@ func (p *program) Start() error { | |
cfg.Validate() | ||
|
||
options.Resolve(opts, flagSet, cfg) | ||
|
||
nsqd, err := nsqd.New(opts) | ||
if err != nil { | ||
logFatal("failed to instantiate nsqd - %s", err) | ||
|
@@ -77,6 +81,17 @@ 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still feels odd to me that we have to separate out a signal to get the behavior we want. Feels like |
||
|
||
go func() { | ||
err := p.nsqd.Main() | ||
if err != nil { | ||
|
@@ -95,6 +110,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...) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jehiah You know, it was the early days. Every package started with
go-
😆There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goodness knows I have a bunch of those myself 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jehiah Sometimes I forget to mention what I'm trying to point out 🤦♂️
go-options
(the line I clicked 'Comment' on) is used likeoptions
. It's no big deal, just pointing it out for consistency.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@judwhite That's a good point (and consistency is a compelling argument)
What i didn't know when i wrote this (it took me too long to track this down) is that
goimports
does special case the"go-"
prefix when handling import paths; I didn't know that.https://github.com/golang/tools/blob/b894a3290fff7ed8373c3156460600f8216a6c2d/internal/imports/fix.go#L1128-L1151