func main() { //以守护进程方式运行 // if os.Getppid() != 1 { // filePath, _ := filepath.Abs(os.Args[0]) // fmt.Printf("filePath=%s\n", filePath) // cmd := exec.Command(filePath, os.Args[1:]...) // cmd.Start() // return // } runtime.GOMAXPROCS(runtime.NumCPU() * 2) err := config.LoadConfig("setting") if err != nil { panic(err) } l, err := logger.StartLog(config.DataSetting.LogDir, config.DataSetting.LogLevel, true) if err != nil { panic(err) } logger.Export(l) defer util.LogPanicStack() logger.Info("开始加载配置....") err = config.LoadAllConfig() if err != nil { logger.Critical("%s", err.Error()) return } logger.Info("===>>配置加载成功") err = db.Start() if err != nil { logger.Critical("===>数据库启动错误:%v", err) } logger.Info("db start...") //加载所有名字 name.InitAllRoleName() word.RegisterModule() gateway.RegisterModule() console.RegisterModule(config.DataSetting.ConsoleAddr, 10, 1024) //注册方法运行时间统计命令 util.RegisterMTCommand() module.Init() logger.Info("服务器启动成功") c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) sig := <-c module.Destroy() db.Stop() logger.Info("服务器退出:%v", sig) }
//加载所有名字 func InitAllRoleName() { allNames = concurrent.NewConcurrentMap(100000) logger.Info("===========>>开启缓存所有名字") allRoleNames, err := db.FindAllName() if err != nil { panic(err) } for _, RN := range allRoleNames { allNames.Put(RN.RoleName, RN.RoleID) } logger.Info("===========>>缓存所有名字成功") }
func (c *console) OnInit() { msgParser, err := msg.NewMsgParserLine(c.maxDataLen) if err != nil { panic(err) } server, err := network.StartTCPServer(c.addr, c.maxConnNum, msgParser, NewAgant) if err != nil { panic(err) } c.server = server logger.Info("console start...") }
func (gw *gateway) OnInit() { msgParser, err := msg.NewMsgParserProtobuf(gw.headLen, gw.msgIDLen, gw.maxDataLen) if err != nil { panic(err) } server, err := network.StartTCPServer(gw.addr, gw.maxConnNum, msgParser, NewAgent) if err != nil { panic(err) } gw.server = server logger.Info("gateway start...") }
func (server *TCPServer) run() { server.wgLn.Add(1) defer server.wgLn.Done() var tempDelay time.Duration for { conn, err := server.ln.Accept() if err != nil { if ne, ok := err.(net.Error); ok && ne.Temporary() { if tempDelay == 0 { tempDelay = 5 * time.Millisecond } else { tempDelay *= 2 } if max := 1 * time.Second; tempDelay > max { tempDelay = max } logger.Error("accept error: %v; retrying in %v", err, tempDelay) time.Sleep(tempDelay) continue } return } tempDelay = 0 //判断是否超最高在线个数 agent := server.newAgent(conn, server.msgParser) server.mutexAgents.Lock() if len(server.agents) >= server.maxConnNum { server.mutexAgents.Unlock() agent.Close(1) logger.Info("too many connections") continue } server.agents[agent] = struct{}{} server.mutexAgents.Unlock() server.wgAgents.Add(1) go func() { agent.Run() agent.Close(0) conn.Close() server.wgAgents.Done() }() } }
func (wds words) OnInit() { for i := 0; i < len(wds); i++ { wds[i].init() } logger.Info("word start...") }