/
nsqlookupd.go
83 lines (68 loc) · 2.12 KB
/
nsqlookupd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"syscall"
"time"
"github.com/BurntSushi/toml"
"github.com/judwhite/go-svc/svc"
"github.com/mreiferson/go-options"
"github.com/nsqio/nsq/internal/version"
"github.com/nsqio/nsq/nsqlookupd"
)
var (
flagSet = flag.NewFlagSet("nsqlookupd", flag.ExitOnError)
config = flagSet.String("config", "", "path to config file")
showVersion = flagSet.Bool("version", false, "print version string")
verbose = flagSet.Bool("verbose", false, "enable verbose logging")
tcpAddress = flagSet.String("tcp-address", "0.0.0.0:4160", "<addr>:<port> to listen on for TCP clients")
httpAddress = flagSet.String("http-address", "0.0.0.0:4161", "<addr>:<port> to listen on for HTTP clients")
broadcastAddress = flagSet.String("broadcast-address", "", "address of this lookupd node, (default to the OS hostname)")
inactiveProducerTimeout = flagSet.Duration("inactive-producer-timeout", 300*time.Second, "duration of time a producer will remain in the active list since its last ping")
tombstoneLifetime = flagSet.Duration("tombstone-lifetime", 45*time.Second, "duration of time a producer will remain tombstoned if registration remains")
)
type program struct {
nsqlookupd *nsqlookupd.NSQLookupd
}
func main() {
prg := &program{}
if err := svc.Run(prg, syscall.SIGINT, syscall.SIGTERM); err != nil {
log.Fatal(err)
}
}
func (p *program) Init(env svc.Environment) error {
if env.IsWindowsService() {
dir := filepath.Dir(os.Args[0])
return os.Chdir(dir)
}
return nil
}
func (p *program) Start() error {
flagSet.Parse(os.Args[1:])
if *showVersion {
fmt.Println(version.String("nsqlookupd"))
os.Exit(0)
}
var cfg map[string]interface{}
if *config != "" {
_, err := toml.DecodeFile(*config, &cfg)
if err != nil {
log.Fatalf("ERROR: failed to load config file %s - %s", *config, err.Error())
}
}
opts := nsqlookupd.NewOptions()
options.Resolve(opts, flagSet, cfg)
daemon := nsqlookupd.New(opts)
daemon.Main()
p.nsqlookupd = daemon
return nil
}
func (p *program) Stop() error {
if p.nsqlookupd != nil {
p.nsqlookupd.Exit()
}
return nil
}