main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package main
  2. import (
  3. "github.com/pkg/profile"
  4. "io/ioutil"
  5. _ "net/http/pprof"
  6. "os"
  7. "rocommon"
  8. "rocommon/service"
  9. _ "rocommon/socket"
  10. _ "rocommon/socket/tcp"
  11. _ "rocommon/socket/websocket"
  12. "roserver/baseserver"
  13. "roserver/baseserver/model"
  14. _ "roserver/gate/model"
  15. "runtime"
  16. "strconv"
  17. "syscall"
  18. )
  19. func main() {
  20. //记录gate pid用来做关闭操作
  21. sysType := runtime.GOOS
  22. if sysType != "windows" {
  23. if pid := syscall.Getpid(); pid != 1 {
  24. fileName := "gate_server.pid" + strconv.Itoa(pid)
  25. ioutil.WriteFile(fileName, []byte(strconv.Itoa(pid)), 0777)
  26. defer os.Remove(fileName)
  27. }
  28. }
  29. //chybenchmark
  30. //go func() {
  31. // log.Fatal(http.ListenAndServe(":9091", nil))
  32. //}()
  33. //go func() {
  34. // delayTimer := time.NewTimer(10 * time.Second)
  35. // for {
  36. // delayTimer.Reset(10 * time.Second)
  37. // select {
  38. // case <-delayTimer.C:
  39. // //debug.FreeOSMemory()
  40. // runtime.GC()
  41. //
  42. // var ms runtime.MemStats
  43. // runtime.ReadMemStats(&ms)
  44. // log.Printf("Alloc:%d(kb) HeapIdle:%d(kb) HeapReleased:%d(kb)", ms.Alloc/1024, ms.HeapIdle/1024, ms.HeapReleased/1024)
  45. // log.Printf("Alloc:%d(M) HeapIdle:%d(M) HeapReleased:%d(M)", ms.Alloc/1024/1024, ms.HeapIdle/1024/1024, ms.HeapReleased/1024/1024)
  46. // }
  47. // }
  48. //}()
  49. //CPU使用详情
  50. //https://github.com/pkg/profile/blob/master/profile.go
  51. //prof := profile.Start(profile.CPUProfile, profile.ProfilePath("./pprof/gate.pprof"), profile.NoShutdownHook)
  52. prof := profile.Start(profile.MemProfile, profile.ProfilePath("./pprof/gatemem.pprof"), profile.NoShutdownHook)
  53. baseserver.Init(model.SERVICE_NODE_TYPE_GATE_STR, nil, &model.GateUpdate{})
  54. //先建立服务器对应的连接,再监听客户端
  55. sConfig := service.GetServiceConfig()
  56. model.GateServiceID = service.GetLocalServiceID()
  57. //需要连接哪些类型的服务器可以通过配置表来决定
  58. //连接auth用来处理sdk验证相关信息
  59. for _, concern := range sConfig.Node.Concern {
  60. //建立需要链接的服务器,可以通过服务器发现etcd来处理(包含在了CreateConnector中)
  61. baseserver.CreateConnector(baseserver.ServiceParam{
  62. DiscoveryServiceName: concern,
  63. DiscoveryServiceZone: sConfig.Node.Zone,
  64. ServiceType: "tcpConnector",
  65. ServiceName: model.SERVICE_NODE_TYPE_GATE_STR,
  66. ProcName: "gate.backend",
  67. })
  68. }
  69. var acceNode rocommon.ServerNode = nil
  70. if sConfig.Acceptor.Addr != "" {
  71. if !sConfig.Node.ISWS {
  72. //创建客户端监听
  73. acceNode = baseserver.CreateClientAcceptor(baseserver.ServiceParam{
  74. ServiceName: model.SERVICE_NODE_TYPE_GATE_STR,
  75. ServiceType: "tcpAcceptor",
  76. ProcName: "gate.frontend",
  77. LisAddr: sConfig.Acceptor.Addr,
  78. }, sConfig)
  79. } else {
  80. //websocket
  81. //创建客户端监听
  82. acceNode = baseserver.CreateWebSocketAcceptor(baseserver.ServiceParam{
  83. ServiceName: model.SERVICE_NODE_TYPE_GATE_STR,
  84. ServiceType: "wsAcceptor",
  85. ProcName: "gatews.frontend",
  86. LisAddr: sConfig.Acceptor.Addr,
  87. }, sConfig)
  88. }
  89. }
  90. baseserver.Wait()
  91. prof.Stop()
  92. baseserver.Exit(acceNode) //需要unregister的node节点
  93. ////内存使用详情
  94. //f, err := os.Create("./pprof/gate.pprof/gate_mem_profile.pro")
  95. //if err != nil {
  96. // log.Println("err:", err)
  97. //}
  98. //pprof.WriteHeapProfile(f)
  99. //f.Close()
  100. }