From f3d4e77becdc423624aa4523320818d3f74767bd Mon Sep 17 00:00:00 2001 From: shyam-king Date: Fri, 13 Nov 2020 12:46:06 +0530 Subject: [PATCH] nsqd: add flags -broadcast-tcp-port and -broadcast-http-address --- apps/nsqd/options.go | 2 ++ nsqd/lookup.go | 4 ++-- nsqd/nsqd.go | 32 ++++++++++++++++++-------------- nsqd/options.go | 12 ++++++++---- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/apps/nsqd/options.go b/apps/nsqd/options.go index 70be620f3..ddc3c115f 100644 --- a/apps/nsqd/options.go +++ b/apps/nsqd/options.go @@ -114,6 +114,8 @@ func nsqdFlagSet(opts *nsqd.Options) *flag.FlagSet { authHTTPAddresses := app.StringArray{} flagSet.Var(&authHTTPAddresses, "auth-http-address", ": to query auth server (may be given multiple times)") flagSet.String("broadcast-address", opts.BroadcastAddress, "address that will be registered with lookupd (defaults to the OS hostname)") + flagSet.Int("broadcast-tcp-port", opts.BroadcastTCPPort, "TCP port that will be registered with lookupd (defaults to the TCP port that this nsqd is listening on)") + flagSet.Int("broadcast-http-port", opts.BroadcastHTTPPort, "HTTP port that will be registered with lookupd (defaults to the HTTP port that this nsqd is listening on)") lookupdTCPAddrs := app.StringArray{} flagSet.Var(&lookupdTCPAddrs, "lookupd-tcp-address", "lookupd TCP address (may be given multiple times)") flagSet.Duration("http-client-connect-timeout", opts.HTTPClientConnectTimeout, "timeout for HTTP connect") diff --git a/nsqd/lookup.go b/nsqd/lookup.go index 37d9f1991..953a450eb 100644 --- a/nsqd/lookup.go +++ b/nsqd/lookup.go @@ -16,8 +16,8 @@ func connectCallback(n *NSQD, hostname string) func(*lookupPeer) { return func(lp *lookupPeer) { ci := make(map[string]interface{}) ci["version"] = version.Binary - ci["tcp_port"] = n.RealTCPAddr().Port - ci["http_port"] = n.RealHTTPAddr().Port + ci["tcp_port"] = n.getOpts().BroadcastTCPPort + ci["http_port"] = n.getOpts().BroadcastHTTPPort ci["hostname"] = hostname ci["broadcast_address"] = n.getOpts().BroadcastAddress diff --git a/nsqd/nsqd.go b/nsqd/nsqd.go index 2a6b4b0b4..87df91c94 100644 --- a/nsqd/nsqd.go +++ b/nsqd/nsqd.go @@ -119,20 +119,6 @@ func New(opts *Options) (*NSQD, error) { return nil, errors.New("--node-id must be [0,1024)") } - if opts.StatsdPrefix != "" { - var port string - _, port, err = net.SplitHostPort(opts.HTTPAddress) - if err != nil { - return nil, fmt.Errorf("failed to parse HTTP address (%s) - %s", opts.HTTPAddress, err) - } - statsdHostKey := statsd.HostKey(net.JoinHostPort(opts.BroadcastAddress, port)) - prefixWithHost := strings.Replace(opts.StatsdPrefix, "%s", statsdHostKey, -1) - if prefixWithHost[len(prefixWithHost)-1] != '.' { - prefixWithHost += "." - } - opts.StatsdPrefix = prefixWithHost - } - if opts.TLSClientAuthPolicy != "" && opts.TLSRequired == TLSNotRequired { opts.TLSRequired = TLSRequired } @@ -171,6 +157,24 @@ func New(opts *Options) (*NSQD, error) { } } + if opts.BroadcastHTTPPort == 0 { + opts.BroadcastHTTPPort = n.RealHTTPAddr().Port + } + + if opts.BroadcastTCPPort == 0 { + opts.BroadcastTCPPort = n.RealTCPAddr().Port + } + + if opts.StatsdPrefix != "" { + var port string = fmt.Sprint(opts.BroadcastHTTPPort) + statsdHostKey := statsd.HostKey(net.JoinHostPort(opts.BroadcastAddress, port)) + prefixWithHost := strings.Replace(opts.StatsdPrefix, "%s", statsdHostKey, -1) + if prefixWithHost[len(prefixWithHost)-1] != '.' { + prefixWithHost += "." + } + opts.StatsdPrefix = prefixWithHost + } + return n, nil } diff --git a/nsqd/options.go b/nsqd/options.go index 216eeeb76..20cf3ed3d 100644 --- a/nsqd/options.go +++ b/nsqd/options.go @@ -23,6 +23,8 @@ type Options struct { HTTPAddress string `flag:"http-address"` HTTPSAddress string `flag:"https-address"` BroadcastAddress string `flag:"broadcast-address"` + BroadcastTCPPort int `flag:"broadcast-tcp-port"` + BroadcastHTTPPort int `flag:"broadcast-http-port"` NSQLookupdTCPAddresses []string `flag:"lookupd-tcp-address" cfg:"nsqlookupd_tcp_addresses"` AuthHTTPAddresses []string `flag:"auth-http-address" cfg:"auth_http_addresses"` HTTPClientConnectTimeout time.Duration `flag:"http-client-connect-timeout" cfg:"http_client_connect_timeout"` @@ -98,10 +100,12 @@ func NewOptions() *Options { LogPrefix: "[nsqd] ", LogLevel: lg.INFO, - TCPAddress: "0.0.0.0:4150", - HTTPAddress: "0.0.0.0:4151", - HTTPSAddress: "0.0.0.0:4152", - BroadcastAddress: hostname, + TCPAddress: "0.0.0.0:4150", + HTTPAddress: "0.0.0.0:4151", + HTTPSAddress: "0.0.0.0:4152", + BroadcastAddress: hostname, + BroadcastTCPPort: 0, + BroadcastHTTPPort: 0, NSQLookupdTCPAddresses: make([]string, 0), AuthHTTPAddresses: make([]string, 0),