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.

rpc_client.go 4.5 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package getty
  18. import (
  19. "crypto/tls"
  20. "fmt"
  21. "net"
  22. "strings"
  23. "sync"
  24. "github.com/seata/seata-go/pkg/protocol/codec"
  25. "github.com/seata/seata-go/pkg/util/log"
  26. getty "github.com/apache/dubbo-getty"
  27. gxsync "github.com/dubbogo/gost/sync"
  28. "github.com/pkg/errors"
  29. )
  30. type RpcClient struct {
  31. gettyConf *Config
  32. seataConf *SeataConfig
  33. gettyClients []getty.Client
  34. futures *sync.Map
  35. }
  36. func InitRpcClient(gettyConfig *Config, seataConfig *SeataConfig) {
  37. iniConfig(seataConfig)
  38. rpcClient := &RpcClient{
  39. gettyConf: gettyConfig,
  40. seataConf: seataConfig,
  41. gettyClients: make([]getty.Client, 0),
  42. }
  43. codec.Init()
  44. rpcClient.init()
  45. }
  46. func (c *RpcClient) init() {
  47. addressList := c.getAvailServerList()
  48. if len(addressList) == 0 {
  49. log.Warn("no have valid seata server list")
  50. }
  51. for _, address := range addressList {
  52. gettyClient := getty.NewTCPClient(
  53. getty.WithServerAddress(address),
  54. // todo if read c.gettyConf.ConnectionNum, will cause the connect to fail
  55. getty.WithConnectionNumber(1),
  56. getty.WithReconnectInterval(c.gettyConf.ReconnectInterval),
  57. getty.WithClientTaskPool(gxsync.NewTaskPoolSimple(0)),
  58. )
  59. go gettyClient.RunEventLoop(c.newSession)
  60. // c.gettyClients = append(c.gettyClients, gettyClient)
  61. }
  62. }
  63. func (c *RpcClient) getAvailServerList() []string {
  64. defaultAddressList := []string{"127.0.0.1:8091"}
  65. txServiceGroup := c.seataConf.TxServiceGroup
  66. if txServiceGroup == "" {
  67. return defaultAddressList
  68. }
  69. clusterName := c.seataConf.ServiceVgroupMapping[txServiceGroup]
  70. if clusterName == "" {
  71. return defaultAddressList
  72. }
  73. grouplist := c.seataConf.ServiceGrouplist[clusterName]
  74. if grouplist == "" {
  75. return defaultAddressList
  76. }
  77. return strings.Split(grouplist, ",")
  78. }
  79. func (c *RpcClient) newSession(session getty.Session) error {
  80. var (
  81. ok bool
  82. tcpConn *net.TCPConn
  83. err error
  84. )
  85. if c.gettyConf.SessionConfig.CompressEncoding {
  86. session.SetCompressType(getty.CompressZip)
  87. }
  88. if _, ok = session.Conn().(*tls.Conn); ok {
  89. c.setSessionConfig(session)
  90. log.Debugf("server accepts new tls session:%s\n", session.Stat())
  91. return nil
  92. }
  93. if _, ok = session.Conn().(*net.TCPConn); !ok {
  94. panic(fmt.Sprintf("%s, session.conn{%#v} is not a tcp connection\n", session.Stat(), session.Conn()))
  95. }
  96. if _, ok = session.Conn().(*tls.Conn); !ok {
  97. if tcpConn, ok = session.Conn().(*net.TCPConn); !ok {
  98. return errors.New(fmt.Sprintf("%s, session.conn{%#v} is not tcp connection", session.Stat(), session.Conn()))
  99. }
  100. if err = tcpConn.SetNoDelay(c.gettyConf.SessionConfig.TCPNoDelay); err != nil {
  101. return err
  102. }
  103. if err = tcpConn.SetKeepAlive(c.gettyConf.SessionConfig.TCPKeepAlive); err != nil {
  104. return err
  105. }
  106. if c.gettyConf.SessionConfig.TCPKeepAlive {
  107. if err = tcpConn.SetKeepAlivePeriod(c.gettyConf.SessionConfig.KeepAlivePeriod); err != nil {
  108. return err
  109. }
  110. }
  111. if err = tcpConn.SetReadBuffer(c.gettyConf.SessionConfig.TCPRBufSize); err != nil {
  112. return err
  113. }
  114. if err = tcpConn.SetWriteBuffer(c.gettyConf.SessionConfig.TCPWBufSize); err != nil {
  115. return err
  116. }
  117. }
  118. c.setSessionConfig(session)
  119. log.Debugf("rpc_client new session:%s\n", session.Stat())
  120. return nil
  121. }
  122. func (c *RpcClient) setSessionConfig(session getty.Session) {
  123. session.SetName(c.gettyConf.SessionConfig.SessionName)
  124. session.SetMaxMsgLen(c.gettyConf.SessionConfig.MaxMsgLen)
  125. session.SetPkgHandler(rpcPkgHandler)
  126. session.SetEventListener(GetGettyClientHandlerInstance())
  127. session.SetReadTimeout(c.gettyConf.SessionConfig.TCPReadTimeout)
  128. session.SetWriteTimeout(c.gettyConf.SessionConfig.TCPWriteTimeout)
  129. session.SetCronPeriod((int)(c.gettyConf.SessionConfig.CronPeriod.Nanoseconds() / 1e6))
  130. session.SetWaitTime(c.gettyConf.SessionConfig.WaitTimeout)
  131. }