func (s *NsqdServer) Main() { var httpListener net.Listener var httpsListener net.Listener if s.ctx.nsqdCoord != nil { err := s.ctx.nsqdCoord.Start() if err != nil { nsqd.NsqLogger().LogErrorf("FATAL: start coordinator failed - %v", err) os.Exit(1) } } opts := s.ctx.getOpts() tcpListener, err := net.Listen("tcp", opts.TCPAddress) if err != nil { nsqd.NsqLogger().LogErrorf("FATAL: listen (%s) failed - %s", opts.TCPAddress, err) os.Exit(1) } s.tcpListener = tcpListener s.ctx.tcpAddr = tcpListener.Addr().(*net.TCPAddr) nsqd.NsqLogger().Logf("TCP: listening on %s", tcpListener.Addr()) tcpServer := &tcpServer{ctx: s.ctx} s.waitGroup.Wrap(func() { protocol.TCPServer(s.tcpListener, tcpServer) nsqd.NsqLogger().Logf("TCP: closing %s", s.tcpListener.Addr()) }) if s.ctx.GetTlsConfig() != nil && opts.HTTPSAddress != "" { httpsListener, err = tls.Listen("tcp", opts.HTTPSAddress, s.ctx.GetTlsConfig()) if err != nil { nsqd.NsqLogger().LogErrorf("FATAL: listen (%s) failed - %s", opts.HTTPSAddress, err) os.Exit(1) } s.httpsListener = httpsListener httpsServer := newHTTPServer(s.ctx, true, true) s.waitGroup.Wrap(func() { http_api.Serve(s.httpsListener, httpsServer, "HTTPS", opts.Logger) }) } httpListener, err = net.Listen("tcp", opts.HTTPAddress) if err != nil { nsqd.NsqLogger().LogErrorf("FATAL: listen (%s) failed - %s", opts.HTTPAddress, err) os.Exit(1) } s.httpListener = httpListener s.ctx.httpAddr = httpListener.Addr().(*net.TCPAddr) s.ctx.reverseProxyPort = opts.ReverseProxyPort httpServer := newHTTPServer(s.ctx, false, opts.TLSRequired == TLSRequired) s.waitGroup.Wrap(func() { http_api.Serve(s.httpListener, httpServer, "HTTP", opts.Logger) }) s.ctx.nsqd.Start() s.waitGroup.Wrap(func() { s.lookupLoop(opts.LookupPingInterval, s.ctx.nsqd.MetaNotifyChan, s.ctx.nsqd.OptsNotificationChan, s.exitChan) }) if opts.StatsdAddress != "" { s.waitGroup.Wrap(s.statsdLoop) } }
func (l *NSQLookupd) Main() { ctx := &Context{l} tcpListener, err := net.Listen("tcp", l.opts.TCPAddress) if err != nil { nsqlookupLog.LogErrorf("FATAL: listen (%s) failed - %s", l.opts.TCPAddress, err) os.Exit(1) } l.Lock() l.tcpListener = tcpListener l.Unlock() nsqlookupLog.Logf("TCP: listening on %s", tcpListener.Addr()) tcpServer := &tcpServer{ctx: ctx} l.waitGroup.Wrap(func() { protocol.TCPServer(tcpListener, tcpServer) nsqlookupLog.Logf("TCP: closing %s", tcpListener.Addr()) }) var node consistence.NsqLookupdNodeInfo _, node.HttpPort, _ = net.SplitHostPort(l.opts.HTTPAddress) if l.opts.ReverseProxyPort != "" { node.HttpPort = l.opts.ReverseProxyPort } node.NodeIP, node.TcpPort, _ = net.SplitHostPort(l.opts.TCPAddress) if l.opts.RPCPort != "" { nsqlookupLog.Logf("broadcast option: %s, %s", l.opts.BroadcastAddress, l.opts.BroadcastInterface) if l.opts.BroadcastInterface != "" { node.NodeIP = getIPv4ForInterfaceName(l.opts.BroadcastInterface) } if node.NodeIP == "" { node.NodeIP = l.opts.BroadcastAddress } else { l.opts.BroadcastAddress = node.NodeIP } if node.NodeIP == "0.0.0.0" || node.NodeIP == "" { nsqlookupLog.LogErrorf("can not decide the broadcast ip: %v", node.NodeIP) os.Exit(1) } nsqlookupLog.Logf("Start with broadcast ip:%s", node.NodeIP) node.RpcPort = l.opts.RPCPort node.ID = consistence.GenNsqLookupNodeID(&node, "nsqlookup") nsqlookupLog.Logf("balance interval is: %v", l.opts.BalanceInterval) coordOpts := &consistence.Options{} if len(l.opts.BalanceInterval) == 2 { coordOpts.BalanceStart, err = strconv.Atoi(l.opts.BalanceInterval[0]) if err != nil { nsqlookupLog.LogErrorf("invalid balance interval: %v", err) os.Exit(1) } coordOpts.BalanceEnd, err = strconv.Atoi(l.opts.BalanceInterval[1]) if err != nil { nsqlookupLog.LogErrorf("invalid balance interval: %v", err) os.Exit(1) } } l.Lock() consistence.SetCoordLogger(l.opts.Logger, l.opts.LogLevel) l.coordinator = consistence.NewNsqLookupCoordinator(l.opts.ClusterID, &node, coordOpts) l.Unlock() // set etcd leader manager here leadership := consistence.NewNsqLookupdEtcdMgr(l.opts.ClusterLeadershipAddresses) l.coordinator.SetLeadershipMgr(leadership) err = l.coordinator.Start() if err != nil { nsqlookupLog.LogErrorf("FATAL: start coordinator failed - %s", err) os.Exit(1) } } else { nsqlookupLog.Logf("lookup start without the coordinator enabled.") l.Lock() l.coordinator = nil l.Unlock() } // wait coordinator ready time.Sleep(time.Millisecond * 500) httpListener, err := net.Listen("tcp", l.opts.HTTPAddress) if err != nil { nsqlookupLog.LogErrorf("FATAL: listen (%s) failed - %s", l.opts.HTTPAddress, err) os.Exit(1) } l.Lock() l.httpListener = httpListener l.Unlock() httpServer := newHTTPServer(ctx) l.waitGroup.Wrap(func() { http_api.Serve(httpListener, httpServer, "HTTP", l.opts.Logger) }) }