func main() { flag.Parse() // init config if err := InitConfig(); err != nil { log.Fatalf("InitConfig failed : %v", err) panic(err) } fmt.Println("init configure successful") isDebug = Conf.Common.Debug runtime.GOMAXPROCS(Conf.Common.MaxProc) // set log if err := InitLog(Conf.Log); err != nil { log.Fatalf("init log failed : %v", err) fmt.Printf("init log failed : %v", err) panic(err) } defer log.Close() log.Infof("init log successful") // init pprof perf.Init(Conf.Common.PprofBind) // server CometServer = NewServer() if err := CometServer.Init(); err != nil { log.Fatalf("CometServer.Init failed : %v", err) panic(err) } log.Infof("init comet server successful") // rpc server if err := InitRPC(); err != nil { log.Fatalf("InitRpcServer failed : %v", err) panic(err) } log.Infof("init rpc server successful") Conf.RegisterUpdateNotify(CometServer.UpdateConfig) Conf.RegisterUpdateNotify(UpdateRpcConfig) // wait signal signal.RegisterExit(CometServer.UnInit) signal.RegisterExit(UnInitRPC) signal.RegisterReload(reload) signal.WaitSignal() }
func (server *Server) InitTlsWebsocket(addrs []string, cert, priv string) (err error) { var ( httpServeMux = http.NewServeMux() ) httpServeMux.Handle("/subscribe", websocket.Handler(server.subscribeWS)) config := &tls.Config{} config.Certificates = make([]tls.Certificate, 1) if config.Certificates[0], err = tls.LoadX509KeyPair(cert, priv); err != nil { return } for _, bind := range addrs { server := &http.Server{Addr: bind, Handler: httpServeMux} server.SetKeepAlivesEnabled(true) log.Infof("comet server start websocket TLS listen: %s", bind) go func() { ln, err := net.Listen("tcp", bind) if err != nil { return } tlsListener := tls.NewListener(ln, config) if err = server.Serve(tlsListener); err != nil { log.Errorf("server.Serve(%s) failed : %v", bind, err) return } }() } return }
func (server *Server) InitWebsocket() (err error) { var ( bind string listener *net.TCPListener addr *net.TCPAddr httpServeMux = http.NewServeMux() ) httpServeMux.Handle("/subscribe", websocket.Handler(server.subscribeWS)) for _, bind = range server.Config.WebSocket.Bind { if addr, err = net.ResolveTCPAddr("tcp4", bind); err != nil { log.Errorf("net.ResolveTCPAddr(tcp4, %s) failed : %v", bind, err) return } if listener, err = net.ListenTCP("tcp4", addr); err != nil { log.Errorf("net.ListenTCP(tcp4, %s) failed : %v", bind, err) return } httpserver := &http.Server{Handler: httpServeMux} log.Infof("comet server start websocket listen: %s", bind) go func() { if err = httpserver.Serve(listener); err != nil { log.Errorf("httpserver.Serve(%s) failed : %v", bind, err) panic(err) } }() } return }
func rpcListen(network, addr string) { l, err := net.Listen(network, addr) if err != nil { log.Fatalf("Rpc listen(%s, %s) failed(%v)", network, addr, err) panic(err) } log.Infof("Rpc listen(%s@%s) successful", network, addr) defer func() { log.Infof("net.Listen(%s, %s) closed", network, addr) if err := l.Close(); err != nil { log.Errorf("Rpc Close() failed(%v)", err) } }() rpc.Accept(l) }
func NewOnlineNotify() *OnlineNotify { on := new(OnlineNotify) for i := 0; i < chansize; i++ { c := NewNotifyChannel(100) on.chans = append(on.chans, c) } log.Infof("init online notify channel size : %d", len(on.chans)) return on }
func (r *PushRpc) Init(conf ConfigRpcPush) (err error) { network := conf.Network addr := conf.Addr r.Client, err = rpc.Dial(network, addr) if err != nil || r.Client == nil { log.Errorf("pushRpc.Dial(%s@%s) failed : %s", network, addr, err) //panic(err) } r.rpcQuit = make(chan struct{}, 1) go r.Keepalive(r.rpcQuit, network, addr) log.Infof("Init push Rpc (%s@%s) sub successfuls", network, addr) return }
func (c *CometRpc) Init() (err error) { conf := Conf.Comets c.Clients = make(map[int32]*CometRpcClient) for _, comet := range conf.Comet { log.Infof("dial comet rpc (%s@%s)......", comet.Network, comet.Addr) client, err := rpc.Dial(comet.Network, comet.Addr) if err != nil { // dial later log.Infof("comet rpc first dial...") } quit := make(chan struct{}, 1) c.Clients[comet.Id] = &CometRpcClient{client, quit, comet.Network, comet.Addr} go c.Keepalive(comet.Id, quit, comet.Network, comet.Addr) log.Infof("connect to comet [%s@%s] successful.", comet.Network, comet.Addr) } c.PushChannels = make([]chan *proto.MQMsg, conf.Push.ChannelSize) for i := 0; i < len(c.PushChannels); i++ { c.PushChannels[i] = make(chan *proto.MQMsg, conf.Push.ChannelBufferSize) go c.processPushMsg(c.PushChannels[i]) } c.Config = conf return nil }
// StartPprof start http pprof. func Init(pprofBind []string) { // pprofServeMux := http.NewServeMux() // pprofServeMux.HandleFunc("/debug/pprof/", pprof.Index) // pprofServeMux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) // pprofServeMux.HandleFunc("/debug/pprof/profile", pprof.Profile) // pprofServeMux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) // pprofServeMux.HandleFunc("/debug/pprof/trace", pprof.Trace) for _, addr := range pprofBind { go func() { if err := http.ListenAndServe(addr, nil); err != nil { log.Fatalf("perf.Init failed : %v", err) panic(err) } else { log.Infof("perf listen addr : %v", addr) } }() } }
func (h *HttpServer) Init() (err error) { for _, b := range h.Config.Bind { httpServeMux := http.NewServeMux() httpServeMux.HandleFunc("/push/user", h.Push) httpServeMux.HandleFunc("/push/users", h.Pushs) httpServeMux.HandleFunc("/push/topic", h.PushTopic) httpServeMux.HandleFunc("/push/all", h.PushAll) httpServeMux.HandleFunc("/server/del", h.Server) httpServeMux.HandleFunc("/server/count", h.ServerCount) httpServeMux.HandleFunc("/users", h.UsersInfo) httpServeMux.HandleFunc("/info", h.Info) log.Infof("start http listen : %v", b) // panic directly go h.httpListen(httpServeMux, b.Network, b.Addr) } return }