You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.go 3.4 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package server
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "os/signal"
  7. "strconv"
  8. "syscall"
  9. "time"
  10. )
  11. import (
  12. getty "github.com/apache/dubbo-getty"
  13. gxnet "github.com/dubbogo/gost/net"
  14. "github.com/dubbogo/gost/sync"
  15. )
  16. import (
  17. "github.com/transaction-wg/seata-golang/pkg/base/extension"
  18. "github.com/transaction-wg/seata-golang/pkg/base/getty/readwriter"
  19. "github.com/transaction-wg/seata-golang/pkg/base/registry"
  20. "github.com/transaction-wg/seata-golang/pkg/tc/config"
  21. "github.com/transaction-wg/seata-golang/pkg/util/log"
  22. )
  23. type Server struct {
  24. conf *config.ServerConfig
  25. tcpServer getty.Server
  26. rpcHandler *DefaultCoordinator
  27. }
  28. func NewServer() *Server {
  29. s := &Server{
  30. conf: config.GetServerConfig(),
  31. }
  32. coordinator := NewDefaultCoordinator(s.conf)
  33. s.rpcHandler = coordinator
  34. return s
  35. }
  36. func (s *Server) newSession(session getty.Session) error {
  37. var (
  38. ok bool
  39. tcpConn *net.TCPConn
  40. )
  41. conf := s.conf
  42. if conf.GettyConfig.GettySessionParam.CompressEncoding {
  43. session.SetCompressType(getty.CompressZip)
  44. }
  45. if tcpConn, ok = session.Conn().(*net.TCPConn); !ok {
  46. panic(fmt.Sprintf("%s, session.conn{%#v} is not tcp connection\n", session.Stat(), session.Conn()))
  47. }
  48. tcpConn.SetNoDelay(conf.GettyConfig.GettySessionParam.TCPNoDelay)
  49. tcpConn.SetKeepAlive(conf.GettyConfig.GettySessionParam.TCPKeepAlive)
  50. if conf.GettyConfig.GettySessionParam.TCPKeepAlive {
  51. tcpConn.SetKeepAlivePeriod(conf.GettyConfig.GettySessionParam.KeepAlivePeriod)
  52. }
  53. tcpConn.SetReadBuffer(conf.GettyConfig.GettySessionParam.TCPRBufSize)
  54. tcpConn.SetWriteBuffer(conf.GettyConfig.GettySessionParam.TCPWBufSize)
  55. session.SetName(conf.GettyConfig.GettySessionParam.SessionName)
  56. session.SetMaxMsgLen(conf.GettyConfig.GettySessionParam.MaxMsgLen)
  57. session.SetPkgHandler(readwriter.RpcPkgHandler)
  58. session.SetEventListener(s.rpcHandler)
  59. session.SetReadTimeout(conf.GettyConfig.GettySessionParam.TCPReadTimeout)
  60. session.SetWriteTimeout(conf.GettyConfig.GettySessionParam.TCPWriteTimeout)
  61. session.SetCronPeriod((int)(conf.GettyConfig.SessionTimeout.Nanoseconds() / 1e6))
  62. session.SetWaitTime(conf.GettyConfig.GettySessionParam.WaitTimeout)
  63. log.Debugf("cmd accepts new session:%s\n", session.Stat())
  64. return nil
  65. }
  66. func (s *Server) Start(addr string) {
  67. var (
  68. tcpServer getty.Server
  69. )
  70. tcpServer = getty.NewTCPServer(
  71. getty.WithLocalAddress(addr),
  72. getty.WithServerTaskPool(gxsync.NewTaskPoolSimple(0)),
  73. )
  74. tcpServer.RunEventLoop(s.newSession)
  75. log.Debugf("s bind addr{%s} ok!", addr)
  76. s.tcpServer = tcpServer
  77. //向注册中心注册实例
  78. registryInstance(s.conf)
  79. c := make(chan os.Signal, 1)
  80. signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
  81. for {
  82. sig := <-c
  83. log.Info("get a signal %s", sig.String())
  84. switch sig {
  85. case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
  86. s.Stop()
  87. time.Sleep(time.Second)
  88. return
  89. case syscall.SIGHUP:
  90. default:
  91. return
  92. }
  93. }
  94. }
  95. func registryInstance(config *config.ServerConfig) {
  96. reg, err := extension.GetRegistry(config.RegistryConfig.Mode)
  97. if err != nil {
  98. log.Error("Registry can not connect success, program is going to panic.Error message is %s", err.Error())
  99. panic(err.Error())
  100. }
  101. ip, _ := gxnet.GetLocalIP()
  102. port, _ := strconv.Atoi(config.Port)
  103. reg.Register(&registry.Address{
  104. IP: ip,
  105. Port: uint64(port),
  106. })
  107. }
  108. func (s *Server) Stop() {
  109. s.tcpServer.Close()
  110. s.rpcHandler.Stop()
  111. }