| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package main
- import (
- "github.com/pkg/profile"
- "io/ioutil"
- _ "net/http/pprof"
- "os"
- "rocommon"
- "rocommon/service"
- _ "rocommon/socket"
- _ "rocommon/socket/tcp"
- _ "rocommon/socket/websocket"
- "roserver/baseserver"
- "roserver/baseserver/model"
- _ "roserver/gate/model"
- "runtime"
- "strconv"
- "syscall"
- )
- func main() {
- //记录gate pid用来做关闭操作
- sysType := runtime.GOOS
- if sysType != "windows" {
- if pid := syscall.Getpid(); pid != 1 {
- fileName := "gate_server.pid" + strconv.Itoa(pid)
- ioutil.WriteFile(fileName, []byte(strconv.Itoa(pid)), 0777)
- defer os.Remove(fileName)
- }
- }
- //chybenchmark
- //go func() {
- // log.Fatal(http.ListenAndServe(":9091", nil))
- //}()
- //go func() {
- // delayTimer := time.NewTimer(10 * time.Second)
- // for {
- // delayTimer.Reset(10 * time.Second)
- // select {
- // case <-delayTimer.C:
- // //debug.FreeOSMemory()
- // runtime.GC()
- //
- // var ms runtime.MemStats
- // runtime.ReadMemStats(&ms)
- // log.Printf("Alloc:%d(kb) HeapIdle:%d(kb) HeapReleased:%d(kb)", ms.Alloc/1024, ms.HeapIdle/1024, ms.HeapReleased/1024)
- // log.Printf("Alloc:%d(M) HeapIdle:%d(M) HeapReleased:%d(M)", ms.Alloc/1024/1024, ms.HeapIdle/1024/1024, ms.HeapReleased/1024/1024)
- // }
- // }
- //}()
- //CPU使用详情
- //https://github.com/pkg/profile/blob/master/profile.go
- //prof := profile.Start(profile.CPUProfile, profile.ProfilePath("./pprof/gate.pprof"), profile.NoShutdownHook)
- prof := profile.Start(profile.MemProfile, profile.ProfilePath("./pprof/gatemem.pprof"), profile.NoShutdownHook)
- baseserver.Init(model.SERVICE_NODE_TYPE_GATE_STR, nil, &model.GateUpdate{})
- //先建立服务器对应的连接,再监听客户端
- sConfig := service.GetServiceConfig()
- model.GateServiceID = service.GetLocalServiceID()
- //需要连接哪些类型的服务器可以通过配置表来决定
- //连接auth用来处理sdk验证相关信息
- for _, concern := range sConfig.Node.Concern {
- //建立需要链接的服务器,可以通过服务器发现etcd来处理(包含在了CreateConnector中)
- baseserver.CreateConnector(baseserver.ServiceParam{
- DiscoveryServiceName: concern,
- DiscoveryServiceZone: sConfig.Node.Zone,
- ServiceType: "tcpConnector",
- ServiceName: model.SERVICE_NODE_TYPE_GATE_STR,
- ProcName: "gate.backend",
- })
- }
- var acceNode rocommon.ServerNode = nil
- if sConfig.Acceptor.Addr != "" {
- if !sConfig.Node.ISWS {
- //创建客户端监听
- acceNode = baseserver.CreateClientAcceptor(baseserver.ServiceParam{
- ServiceName: model.SERVICE_NODE_TYPE_GATE_STR,
- ServiceType: "tcpAcceptor",
- ProcName: "gate.frontend",
- LisAddr: sConfig.Acceptor.Addr,
- }, sConfig)
- } else {
- //websocket
- //创建客户端监听
- acceNode = baseserver.CreateWebSocketAcceptor(baseserver.ServiceParam{
- ServiceName: model.SERVICE_NODE_TYPE_GATE_STR,
- ServiceType: "wsAcceptor",
- ProcName: "gatews.frontend",
- LisAddr: sConfig.Acceptor.Addr,
- }, sConfig)
- }
- }
- baseserver.Wait()
- prof.Stop()
- baseserver.Exit(acceNode) //需要unregister的node节点
- ////内存使用详情
- //f, err := os.Create("./pprof/gate.pprof/gate_mem_profile.pro")
- //if err != nil {
- // log.Println("err:", err)
- //}
- //pprof.WriteHeapProfile(f)
- //f.Close()
- }
|