示例#1
0
func main() {
	var err error

	runtime.GOMAXPROCS(runtime.NumCPU())

	// Load config
	InitConfig()
	flag.Parse()
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		Log.Error("NewConfig(\"ConfigPath\":%s) failed(%v)", ConfFile, err)
		os.Exit(-1)
	}

	// Load log
	Log, err = log.New(Conf.LogPath, Conf.LogLevel)
	if err != nil {
		Log.Error("log.New(\"LogPath\":%s) failed(%v)", Conf.LogPath, err)
		os.Exit(-1)
	}

	// Initialize redis
	InitRedis()

	// Start rpc
	if err := StartRPC(); err != nil {
		Log.Error("StartRPC() failed (%v)", err)
		os.Exit(-1)
	}
}
示例#2
0
func main() {
	var err error
	// parse cmd-line arguments
	flag.Parse()
	signalCH := InitSignal()
	// init config
	Conf, err = InitConfig(ConfFile)
	if err != nil {
		Log.Error("NewConfig(\"%s\") error(%v)", ConfFile, err)
		os.Exit(-1)
	}
	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)
	// init log
	if Log, err = log.New(Conf.LogFile, Conf.LogLevel); err != nil {
		Log.Error("log.New(\"%s\", %s) error(%v)", Conf.LogFile, Conf.LogLevel, err)
		os.Exit(-1)
	}
	// if process exit, close log
	defer Log.Close()
	Log.Info("comet start")
	// create channel
	UserChannel = NewChannelList()
	// if process exit, close channel
	defer UserChannel.Close()
	// start stats
	StartStats()
	// start pprof http
	StartPprof()
	// init message rpc, block until message rpc init.
	InitMessageRPC()
	// start rpc
	StartRPC()
	// start comet
	StartComet()
	// init zookeeper
	zk, err := InitZookeeper()
	if err != nil {
		Log.Error("InitZookeeper() error(%v)", err)
		os.Exit(-1)
	}
	// if process exit, close zk
	defer zk.Close()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	if err = InitProcess(); err != nil {
		Log.Error("InitProcess() error(%v)", err)
		os.Exit(-1)
	}
	// init signals, block wait signals
	HandleSignal(signalCH)
	// exit
	Log.Info("comet stop")
}
示例#3
0
func main() {
	var err error
	// Parse cmd-line arguments
	flag.Parse()
	signalCH := InitSignal()

	// Load config
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}

	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)

	// Load log
	Log, err = log.New(Conf.LogFile, Conf.LogLevel)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}

	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	if err = InitProcess(); err != nil {
		Log.Error("InitProcess() error(%v)", err)
		os.Exit(-1)
	}

	// start pprof http
	StartPprof()

	// Initialize redis
	InitRedis()

	// Start rpc
	Log.Info("Message service start")
	go StartRPC()

	// init signals, block wait signals
	HandleSignal(signalCH)

	// exit
	Log.Info("Message service end")
}
示例#4
0
func main() {
	var err error
	// Parse cmd-line arguments
	flag.Parse()
	signalCH := InitSignal()

	// Load config
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}

	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)

	// Load log
	Log, err = log.New(Conf.LogPath, Conf.LogLevel)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}
	Log.Debug("initialize config %v", *Conf)

	// Initialize zookeeper
	if err := InitWatch(); err != nil {
		Log.Error("InitWatch() failed(%v)", err)
		os.Exit(-1)
	}

	// Begin watch nodes
	if err := BeginWatchNode(); err != nil {
		Log.Error("BeginWatchNode() failed(%v)", err)
		os.Exit(-1)
	}

	// Initialize message server client
	if err := InitMsgSvrClient(); err != nil {
		Log.Error("InitMsgSvrClient() failed(%v)", err)
		os.Exit(-1)
	}

	// init process
	if err = InitProcess(); err != nil {
		Log.Error("InitProcess() error(%v)", err)
		os.Exit(-1)
	}

	// start pprof http
	StartPprof()

	// External service handle
	//http.HandleFunc("/server/get", ServerGet)
	//http.HandleFunc("/msg/get", MsgGet)

	// Internal admin handle
	go func() {
		adminServeMux := http.NewServeMux()

		adminServeMux.HandleFunc("/admin/push", AdminPushPrivate)
		adminServeMux.HandleFunc("/admin/push/public", AdminPushPublic)
		adminServeMux.HandleFunc("/admin/node/add", AdminNodeAdd)
		adminServeMux.HandleFunc("/admin/node/del", AdminNodeDel)
		adminServeMux.HandleFunc("/admin/msg/clean", AdminMsgClean)

		err := http.ListenAndServe(Conf.AdminAddr, adminServeMux)
		if err != nil {
			Log.Error("http.ListenAndServe(\"%s\") failed(%v)", Conf.AdminAddr, err)
			os.Exit(-1)
		}
	}()

	// Start service
	go func() {
		// External service handle
		httpServeMux := http.NewServeMux()
		httpServeMux.HandleFunc("/server/get", ServerGet)
		httpServeMux.HandleFunc("/msg/get", MsgGet)

		server := &http.Server{Handler: httpServeMux, ReadTimeout: httpReadTimeout * time.Second}
		l, err := net.Listen("tcp", Conf.Addr)
		if err != nil {
			Log.Error("net.Listen(\"tcp\", \"%s\") error(%v)", Conf.Addr, err)
			os.Exit(-1)
		}
		if err := server.Serve(l); err != nil {
			Log.Error("server.Serve(\"%s\") error(%v)", Conf.Addr, err)
			os.Exit(-1)
		}
		/*
			if err := http.ListenAndServe(Conf.Addr, nil); err != nil {
				Log.Error("http.ListenAndServe(\"%s\") failed(%v)", Conf.Addr, err)
				os.Exit(-1)
			}*/
	}()

	// init signals, block wait signals
	Log.Info("Web service start")
	HandleSignal(signalCH)

	// Clost message service client
	MsgSvrClose()
	// Stop watch
	WatchStop()
	Log.Warn("Web service end")
}
示例#5
0
func main() {
	var err error
	// parse cmd-line arguments
	flag.Parse()
	// init config
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		log.DefaultLogger.Error("NewConfig(\"%s\") failed (%s)", ConfFile, err.Error())
		os.Exit(-1)
	}

	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProcs)
	// init log
	if Log, err = log.New(Conf.Log, Conf.LogLevel); err != nil {
		log.DefaultLogger.Error("log.New(\"%s\", %d) failed (%s)", Conf.Log, Conf.LogLevel, err.Error())
		os.Exit(-1)
	}

	defer Log.Close()
	if Conf.Addr == Conf.AdminAddr {
		Log.Warn("Configure \"AdminAdd\" = \"Addr\" is not allowed for security reason")
		os.Exit(-1)
	}

	Log.Info("gopush2 start")
	StartStats()
	if zk, err := InitZookeeper(); err != nil {
		Log.Error("InitZookeeper() failed (%s)", err.Error())
		os.Exit(-1)
	} else {
		defer func() {
			if err = zk.Close(); err != nil {
				Log.Error("zk.Close() failed (%s)", err.Error())
			}
		}()
	}

	// create channel
	UserChannel = NewChannelList()
	// start stats
	StartStats()
	// start pprof http
	go func() {
		if err := StartPprofHttp(); err != nil {
			Log.Error("StartPprofHttp() failed (%s)", err.Error())
			os.Exit(-1)
		}
	}()

	// start rpc
	go func() {
		if err := StartRPC(); err != nil {
			Log.Error("StartRPC() failed (%s)", err.Error())
			os.Exit(-1)
		}

		Log.Crit("rpc crash")
		os.Exit(-1)
	}()

	if Conf.Protocol == WebsocketProtocol {
		// Start http push service
		if err = StartHttp(); err != nil {
			Log.Error("StartHttp() failed (%s)", err.Error())
		}
	} else if Conf.Protocol == TCPProtocol {
		// Start http push service
		if err = StartTCP(); err != nil {
			Log.Error("StartTCP() failed (%s)", err.Error())
		}
	} else {
		Log.Warn("unknown gopush-cluster protocol %d, (0: websocket, 1: tcp)", Conf.Protocol)
		os.Exit(-1)
	}

	// exit
	Log.Info("gopush2 stop")
	os.Exit(-1)
}
示例#6
0
func main() {
	var err error

	runtime.GOMAXPROCS(runtime.NumCPU())

	// Load config
	InitConfig()
	flag.Parse()
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		Log.Error("NewConfig(\"ConfigPath\":%s) failed(%v)", ConfFile, err)
		os.Exit(-1)
	}

	// Load log
	Log, err = log.New(Conf.LogPath, Conf.LogLevel)
	if err != nil {
		Log.Error("log.New(\"LogPath\":%s) failed(%v)", Conf.LogPath, err)
		os.Exit(-1)
	}

	// Initialize zookeeper
	if err := InitWatch(); err != nil {
		Log.Error("InitWatch() failed(%v)", err)
		os.Exit(-1)
	}

	// Begin watch nodes
	if err := BeginWatchNode(); err != nil {
		Log.Error("BeginWatchNode() failed(%v)", err)
		os.Exit(-1)
	}

	// Initialize message server client
	if err := InitMsgSvrClient(); err != nil {
		Log.Error("InitMsgSvrClient() failed(%v)", err)
		os.Exit(-1)
	}

	// External service handle
	http.HandleFunc("/server/get", ServerGet)
	http.HandleFunc("/msg/get", MsgGet)

	// Internal admin handle
	go func() {
		adminServeMux := http.NewServeMux()

		adminServeMux.HandleFunc("/admin/push", AdminPush)
		adminServeMux.HandleFunc("/admin/node/add", AdminNodeAdd)
		adminServeMux.HandleFunc("/admin/node/del", AdminNodeDel)

		err := http.ListenAndServe(Conf.AdminAddr, adminServeMux)
		if err != nil {
			Log.Error("http.ListenAndServe(%s) failed(%v)", Conf.AdminAddr, err)
			os.Exit(-1)
		}
	}()

	// Start service
	if err := http.ListenAndServe(Conf.Addr, nil); err != nil {
		Log.Error("http.ListenAndServe(%s) failed(%v)", Conf.Addr, err)
		os.Exit(-1)
	}
}
示例#7
0
func main() {
	var err error
	flag.Parse()
	// init config
	Conf, err = InitConfig(ConfFile)
	if err != nil {
		Log.Error("NewConfig(\"%s\") failed (%s)", ConfFile, err.Error())
		os.Exit(-1)
	}
	// init log
	if Log, err = log.New(Conf.LogFile, Conf.LogLevel); err != nil {
		Log.Error("log.New(\"%s\", %s) failed (%s)", Conf.LogFile, Conf.LogLevel, err.Error())
		os.Exit(-1)
	}
	defer Log.Close()
	addr, err := net.ResolveTCPAddr("tcp", Conf.Addr)
	if err != nil {
		Log.Error("net.ResolveTCPAddr(\"tcp\", \"%s\") failed (%s)", Conf.Addr, err.Error())
		os.Exit(-1)
	}
	Log.Info("connect to gopush-cluster comet")
	conn, err := net.DialTCP("tcp", nil, addr)
	if err != nil {
		Log.Error("net.DialTCP() failed (%s)", err.Error())
		os.Exit(-1)
	}
	Log.Info("send sub request")
	proto := []byte(fmt.Sprintf("*3\r\n$3\r\nsub\r\n$%d\r\n%s\r\n$%d\r\n%d\r\n", len(Conf.Key), Conf.Key, len(strconv.Itoa(int(Conf.Heartbeat))), Conf.Heartbeat))
	Log.Info("send protocol: %s", string(proto))
	if _, err := conn.Write(proto); err != nil {
		Log.Error("conn.Write() failed (%s)", err.Error())
		os.Exit(-1)
	}
	// get first heartbeat
	first := false
	rd := bufio.NewReader(conn)
	// block read reply from service
	Log.Info("wait message")
	for {
		if err := conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(Conf.Heartbeat) * 2)); err != nil {
			Log.Error("conn.SetReadDeadline() failed (%s)", err.Error())
			os.Exit(-1)
		}
		line, err := rd.ReadBytes('\n')
		if err != nil {
			Log.Error("rd.ReadBytes() failed (%s)", err.Error())
			os.Exit(-1)
		}
		if line[len(line)-2] != '\r' {
			Log.Error("protocol reply format error")
			os.Exit(-1)
		}
		Log.Info("line: %s", line)
		switch line[0] {
		// reply
		case '$':
			cmdSize, err := strconv.Atoi(string(line[1 : len(line)-2]))
			if err != nil {
				Log.Error("protocol reply format error")
				os.Exit(-1)
			}
			data, err := rd.ReadBytes('\n')
			if err != nil {
				Log.Error("protocol reply format error")
				os.Exit(-1)
			}
			if len(data) != cmdSize+2 {
				Log.Error("protocol reply format error: %s", data)
				os.Exit(-1)
			}
			if data[cmdSize] != '\r' || data[cmdSize+1] != '\n' {
				Log.Error("protocol reply format error")
				os.Exit(-1)
			}
			reply := string(data[0:cmdSize])
			Log.Info("receive msg: %s", reply)
			break
			// heartbeat
		case '+':
			if !first {
				// send heartbeat
				go func() {
					for {
						Log.Info("send heartbeat")
						if _, err := conn.Write([]byte("h")); err != nil {
							Log.Error("conn.Write() failed (%s)", err.Error())
							os.Exit(-1)
						}
						time.Sleep(time.Duration(Conf.Heartbeat) * time.Second)
					}
				}()
				first = true
			}
			Log.Info("receive heartbeat")
			break
		}
	}
}
示例#8
0
func main() {
	var err error
	// Parse cmd-line arguments
	flag.Parse()
	signalCH := InitSignal()

	// Load config
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}

	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)

	// Load log
	Log, err = log.New(Conf.LogPath, Conf.LogLevel)
	if err != nil {
		panic(err)
		os.Exit(-1)
	}

	// Initialize zookeeper
	if err := InitWatch(); err != nil {
		Log.Error("InitWatch() failed(%v)", err)
		os.Exit(-1)
	}

	// Begin watch nodes
	if err := BeginWatchNode(); err != nil {
		Log.Error("BeginWatchNode() failed(%v)", err)
		os.Exit(-1)
	}

	// Initialize message server client
	if err := InitMsgSvrClient(); err != nil {
		Log.Error("InitMsgSvrClient() failed(%v)", err)
		os.Exit(-1)
	}

	// External service handle
	http.HandleFunc("/server/get", ServerGet)
	http.HandleFunc("/msg/get", MsgGet)

	// Internal admin handle
	go func() {
		adminServeMux := http.NewServeMux()

		adminServeMux.HandleFunc("/admin/push", AdminPushPrivate)
		adminServeMux.HandleFunc("/admin/push/public", AdminPushPublic)
		adminServeMux.HandleFunc("/admin/node/add", AdminNodeAdd)
		adminServeMux.HandleFunc("/admin/node/del", AdminNodeDel)

		err := http.ListenAndServe(Conf.AdminAddr, adminServeMux)
		if err != nil {
			Log.Error("http.ListenAndServe(\"%s\") failed(%v)", Conf.AdminAddr, err)
			os.Exit(-1)
		}
	}()

	// Start service
	go func() {
		if err := http.ListenAndServe(Conf.Addr, nil); err != nil {
			Log.Error("http.ListenAndServe(\"%s\") failed(%v)", Conf.Addr, err)
			os.Exit(-1)
		}
	}()

	// init signals, block wait signals
	Log.Info("Web service start")
	HandleSignal(signalCH)

	// Clost message service client
	MsgSvrClose()
	// Stop watch
	WatchStop()
	Log.Warn("Web service end")
}
示例#9
0
func main() {
	var err error
	// Parse cmd-line arguments
	flag.Parse()
	signalCH := InitSignal()
	// Load config
	Conf, err = NewConfig(ConfFile)
	if err != nil {
		Log.Error("NewConfig(\"%s\") error(%v)", ConfFile, err)
		return
	}
	// Set max routine
	runtime.GOMAXPROCS(Conf.MaxProc)
	// Load log
	if Log, err = log.New(Conf.LogPath, Conf.LogLevel); err != nil {
		Log.Error("log.New(\"%s\", %s) error(%v)", Conf.LogPath, Conf.LogLevel, err)
		return
	}
	// if process exit, close log
	defer Log.Close()
	// Initialize zookeeper
	zk, err := InitZK()
	if err != nil {
		Log.Error("InitZK() failed(%v)", err)
		return
	}
	// if process exit, close zk
	defer zk.Close()
	// Initialize message server client
	if err := InitMsgSvrClient(); err != nil {
		Log.Error("InitMsgSvrClient() failed(%v)", err)
		return
	}
	// Clost message service client
	defer MsgSvrClose()
	// start pprof http
	perf.Init(Conf.PprofBind)
	// Internal admin handle
	go func() {
		adminServeMux := http.NewServeMux()
		adminServeMux.HandleFunc("/admin/push", AdminPushPrivate)
		adminServeMux.HandleFunc("/admin/push/public", AdminPushPublic)
		adminServeMux.HandleFunc("/admin/node/add", AdminNodeAdd)
		adminServeMux.HandleFunc("/admin/node/del", AdminNodeDel)
		adminServeMux.HandleFunc("/admin/msg/clean", AdminMsgClean)
		err := http.ListenAndServe(Conf.AdminAddr, adminServeMux)
		if err != nil {
			Log.Error("http.ListenAndServe(\"%s\") failed(%v)", Conf.AdminAddr, err)
			panic(err)
		}
	}()
	// Start service
	go func() {
		// External service handle
		httpServeMux := http.NewServeMux()
		httpServeMux.HandleFunc("/server/get", ServerGet)
		httpServeMux.HandleFunc("/msg/get", MsgGet)
		httpServeMux.HandleFunc("/time/get", TimeGet)
		server := &http.Server{Handler: httpServeMux, ReadTimeout: httpReadTimeout * time.Second}
		l, err := net.Listen("tcp", Conf.Addr)
		if err != nil {
			Log.Error("net.Listen(\"tcp\", \"%s\") error(%v)", Conf.Addr, err)
			panic(err)
		}
		if err := server.Serve(l); err != nil {
			Log.Error("server.Serve(\"%s\") error(%v)", Conf.Addr, err)
			panic(err)
		}
	}()
	// init process
	// sleep one second, let the listen start
	time.Sleep(time.Second)
	if err = process.Init(Conf.User, Conf.Dir, Conf.PidFile); err != nil {
		Log.Error("process.Init() error(%v)", err)
		return
	}
	// init signals, block wait signals
	Log.Info("Web service start")
	HandleSignal(signalCH)
	Log.Warn("Web service end")
}