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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. getty.WithConnectionNumber(c.gettyConf.ConnectionNum),
  55. getty.WithReconnectInterval(c.gettyConf.ReconnectInterval),
  56. getty.WithClientTaskPool(gxsync.NewTaskPoolSimple(0)),
  57. )
  58. go gettyClient.RunEventLoop(c.newSession)
  59. // c.gettyClients = append(c.gettyClients, gettyClient)
  60. }
  61. }
  62. func (c *RpcClient) getAvailServerList() []string {
  63. defaultAddressList := []string{"127.0.0.1:8091"}
  64. txServiceGroup := c.seataConf.TxServiceGroup
  65. if txServiceGroup == "" {
  66. return defaultAddressList
  67. }
  68. clusterName := c.seataConf.ServiceVgroupMapping[txServiceGroup]
  69. if clusterName == "" {
  70. return defaultAddressList
  71. }
  72. grouplist := c.seataConf.ServiceGrouplist[clusterName]
  73. if grouplist == "" {
  74. return defaultAddressList
  75. }
  76. return strings.Split(grouplist, ",")
  77. }
  78. func (c *RpcClient) newSession(session getty.Session) error {
  79. var (
  80. ok bool
  81. tcpConn *net.TCPConn
  82. err error
  83. )
  84. if c.gettyConf.SessionConfig.CompressEncoding {
  85. session.SetCompressType(getty.CompressZip)
  86. }
  87. if _, ok = session.Conn().(*tls.Conn); ok {
  88. c.setSessionConfig(session)
  89. log.Debugf("server accepts new tls session:%s\n", session.Stat())
  90. return nil
  91. }
  92. if _, ok = session.Conn().(*net.TCPConn); !ok {
  93. panic(fmt.Sprintf("%s, session.conn{%#v} is not a tcp connection\n", session.Stat(), session.Conn()))
  94. }
  95. if _, ok = session.Conn().(*tls.Conn); !ok {
  96. if tcpConn, ok = session.Conn().(*net.TCPConn); !ok {
  97. return errors.New(fmt.Sprintf("%s, session.conn{%#v} is not tcp connection", session.Stat(), session.Conn()))
  98. }
  99. if err = tcpConn.SetNoDelay(c.gettyConf.SessionConfig.TCPNoDelay); err != nil {
  100. return err
  101. }
  102. if err = tcpConn.SetKeepAlive(c.gettyConf.SessionConfig.TCPKeepAlive); err != nil {
  103. return err
  104. }
  105. if c.gettyConf.SessionConfig.TCPKeepAlive {
  106. if err = tcpConn.SetKeepAlivePeriod(c.gettyConf.SessionConfig.KeepAlivePeriod); err != nil {
  107. return err
  108. }
  109. }
  110. if err = tcpConn.SetReadBuffer(c.gettyConf.SessionConfig.TCPRBufSize); err != nil {
  111. return err
  112. }
  113. if err = tcpConn.SetWriteBuffer(c.gettyConf.SessionConfig.TCPWBufSize); err != nil {
  114. return err
  115. }
  116. }
  117. c.setSessionConfig(session)
  118. log.Debugf("rpc_client new session:%s\n", session.Stat())
  119. return nil
  120. }
  121. func (c *RpcClient) setSessionConfig(session getty.Session) {
  122. session.SetName(c.gettyConf.SessionConfig.SessionName)
  123. session.SetMaxMsgLen(c.gettyConf.SessionConfig.MaxMsgLen)
  124. session.SetPkgHandler(rpcPkgHandler)
  125. session.SetEventListener(GetGettyClientHandlerInstance())
  126. session.SetReadTimeout(c.gettyConf.SessionConfig.TCPReadTimeout)
  127. session.SetWriteTimeout(c.gettyConf.SessionConfig.TCPWriteTimeout)
  128. session.SetCronPeriod((int)(c.gettyConf.SessionConfig.CronPeriod.Milliseconds()))
  129. session.SetWaitTime(c.gettyConf.SessionConfig.WaitTimeout)
  130. }