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

785 lines
21 KiB

p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
p2p: implement new Transport interface (#5791) This implements a new `Transport` interface and related types for the P2P refactor in #5670. Previously, `conn.MConnection` was very tightly coupled to the `Peer` implementation -- in order to allow alternative non-multiplexed transports (e.g. QUIC), MConnection has now been moved below the `Transport` interface, as `MConnTransport`, and decoupled from the peer. Since the `p2p` package is not covered by our Go API stability, this is not considered a breaking change, and not listed in the changelog. The initial approach was to implement the new interface in its final form (which also involved possible protocol changes, see https://github.com/tendermint/spec/pull/227). However, it turned out that this would require a large amount of changes to existing P2P code because of the previous tight coupling between `Peer` and `MConnection` and the reliance on subtleties in the MConnection behavior. Instead, I have broadened the `Transport` interface to expose much of the existing MConnection interface, preserved much of the existing MConnection logic and behavior in the transport implementation, and tried to make as few changes to the rest of the P2P stack as possible. We will instead reduce this interface gradually as we refactor other parts of the P2P stack. The low-level transport code and protocol (e.g. MConnection, SecretConnection and so on) has not been significantly changed, and refactoring this is not a priority until we come up with a plan for QUIC adoption, as we may end up discarding the MConnection code entirely. There are no tests of the new `MConnTransport`, as this code is likely to evolve as we proceed with the P2P refactor, but tests should be added before a final release. The E2E tests are sufficient for basic validation in the meanwhile.
4 years ago
  1. package conn
  2. import (
  3. "bufio"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "math"
  9. "net"
  10. "reflect"
  11. "runtime/debug"
  12. "sync"
  13. "sync/atomic"
  14. "time"
  15. "github.com/gogo/protobuf/proto"
  16. "github.com/tendermint/tendermint/internal/libs/flowrate"
  17. "github.com/tendermint/tendermint/internal/libs/protoio"
  18. "github.com/tendermint/tendermint/internal/libs/timer"
  19. "github.com/tendermint/tendermint/libs/log"
  20. tmmath "github.com/tendermint/tendermint/libs/math"
  21. "github.com/tendermint/tendermint/libs/service"
  22. tmp2p "github.com/tendermint/tendermint/proto/tendermint/p2p"
  23. )
  24. const (
  25. // mirrors MaxPacketMsgPayloadSize from config/config.go
  26. defaultMaxPacketMsgPayloadSize = 1400
  27. numBatchPacketMsgs = 10
  28. minReadBufferSize = 1024
  29. minWriteBufferSize = 65536
  30. updateStats = 2 * time.Second
  31. // some of these defaults are written in the user config
  32. // flushThrottle, sendRate, recvRate
  33. // TODO: remove values present in config
  34. defaultFlushThrottle = 100 * time.Millisecond
  35. defaultSendQueueCapacity = 1
  36. defaultRecvBufferCapacity = 4096
  37. defaultRecvMessageCapacity = 22020096 // 21MB
  38. defaultSendRate = int64(512000) // 500KB/s
  39. defaultRecvRate = int64(512000) // 500KB/s
  40. defaultSendTimeout = 10 * time.Second
  41. defaultPingInterval = 60 * time.Second
  42. defaultPongTimeout = 45 * time.Second
  43. )
  44. type receiveCbFunc func(ctx context.Context, chID ChannelID, msgBytes []byte)
  45. type errorCbFunc func(context.Context, interface{})
  46. /*
  47. Each peer has one `MConnection` (multiplex connection) instance.
  48. __multiplex__ *noun* a system or signal involving simultaneous transmission of
  49. several messages along a single channel of communication.
  50. Each `MConnection` handles message transmission on multiple abstract communication
  51. `Channel`s. Each channel has a globally unique byte id.
  52. The byte id and the relative priorities of each `Channel` are configured upon
  53. initialization of the connection.
  54. There are two methods for sending messages:
  55. func (m MConnection) Send(chID byte, msgBytes []byte) bool {}
  56. `Send(chID, msgBytes)` is a blocking call that waits until `msg` is
  57. successfully queued for the channel with the given id byte `chID`, or until the
  58. request times out. The message `msg` is serialized using Protobuf.
  59. Inbound message bytes are handled with an onReceive callback function.
  60. */
  61. type MConnection struct {
  62. service.BaseService
  63. logger log.Logger
  64. conn net.Conn
  65. bufConnReader *bufio.Reader
  66. bufConnWriter *bufio.Writer
  67. sendMonitor *flowrate.Monitor
  68. recvMonitor *flowrate.Monitor
  69. send chan struct{}
  70. pong chan struct{}
  71. channels []*channel
  72. channelsIdx map[ChannelID]*channel
  73. onReceive receiveCbFunc
  74. onError errorCbFunc
  75. errored uint32
  76. config MConnConfig
  77. // Closing quitSendRoutine will cause the sendRoutine to eventually quit.
  78. // doneSendRoutine is closed when the sendRoutine actually quits.
  79. quitSendRoutine chan struct{}
  80. doneSendRoutine chan struct{}
  81. // Closing quitRecvRouting will cause the recvRouting to eventually quit.
  82. quitRecvRoutine chan struct{}
  83. // used to ensure FlushStop and OnStop
  84. // are safe to call concurrently.
  85. stopMtx sync.Mutex
  86. cancel context.CancelFunc
  87. flushTimer *timer.ThrottleTimer // flush writes as necessary but throttled.
  88. pingTimer *time.Ticker // send pings periodically
  89. // close conn if pong is not received in pongTimeout
  90. pongTimer *time.Timer
  91. pongTimeoutCh chan bool // true - timeout, false - peer sent pong
  92. chStatsTimer *time.Ticker // update channel stats periodically
  93. created time.Time // time of creation
  94. _maxPacketMsgSize int
  95. }
  96. // MConnConfig is a MConnection configuration.
  97. type MConnConfig struct {
  98. SendRate int64 `mapstructure:"send_rate"`
  99. RecvRate int64 `mapstructure:"recv_rate"`
  100. // Maximum payload size
  101. MaxPacketMsgPayloadSize int `mapstructure:"max_packet_msg_payload_size"`
  102. // Interval to flush writes (throttled)
  103. FlushThrottle time.Duration `mapstructure:"flush_throttle"`
  104. // Interval to send pings
  105. PingInterval time.Duration `mapstructure:"ping_interval"`
  106. // Maximum wait time for pongs
  107. PongTimeout time.Duration `mapstructure:"pong_timeout"`
  108. }
  109. // DefaultMConnConfig returns the default config.
  110. func DefaultMConnConfig() MConnConfig {
  111. return MConnConfig{
  112. SendRate: defaultSendRate,
  113. RecvRate: defaultRecvRate,
  114. MaxPacketMsgPayloadSize: defaultMaxPacketMsgPayloadSize,
  115. FlushThrottle: defaultFlushThrottle,
  116. PingInterval: defaultPingInterval,
  117. PongTimeout: defaultPongTimeout,
  118. }
  119. }
  120. // NewMConnection wraps net.Conn and creates multiplex connection
  121. func NewMConnection(
  122. logger log.Logger,
  123. conn net.Conn,
  124. chDescs []*ChannelDescriptor,
  125. onReceive receiveCbFunc,
  126. onError errorCbFunc,
  127. ) *MConnection {
  128. return NewMConnectionWithConfig(
  129. logger,
  130. conn,
  131. chDescs,
  132. onReceive,
  133. onError,
  134. DefaultMConnConfig())
  135. }
  136. // NewMConnectionWithConfig wraps net.Conn and creates multiplex connection with a config
  137. func NewMConnectionWithConfig(
  138. logger log.Logger,
  139. conn net.Conn,
  140. chDescs []*ChannelDescriptor,
  141. onReceive receiveCbFunc,
  142. onError errorCbFunc,
  143. config MConnConfig,
  144. ) *MConnection {
  145. if config.PongTimeout >= config.PingInterval {
  146. panic("pongTimeout must be less than pingInterval (otherwise, next ping will reset pong timer)")
  147. }
  148. mconn := &MConnection{
  149. logger: logger,
  150. conn: conn,
  151. bufConnReader: bufio.NewReaderSize(conn, minReadBufferSize),
  152. bufConnWriter: bufio.NewWriterSize(conn, minWriteBufferSize),
  153. sendMonitor: flowrate.New(0, 0),
  154. recvMonitor: flowrate.New(0, 0),
  155. send: make(chan struct{}, 1),
  156. pong: make(chan struct{}, 1),
  157. onReceive: onReceive,
  158. onError: onError,
  159. config: config,
  160. created: time.Now(),
  161. cancel: func() {},
  162. }
  163. mconn.BaseService = *service.NewBaseService(logger, "MConnection", mconn)
  164. // Create channels
  165. var channelsIdx = map[ChannelID]*channel{}
  166. var channels = []*channel{}
  167. for _, desc := range chDescs {
  168. channel := newChannel(mconn, *desc)
  169. channelsIdx[channel.desc.ID] = channel
  170. channels = append(channels, channel)
  171. }
  172. mconn.channels = channels
  173. mconn.channelsIdx = channelsIdx
  174. // maxPacketMsgSize() is a bit heavy, so call just once
  175. mconn._maxPacketMsgSize = mconn.maxPacketMsgSize()
  176. return mconn
  177. }
  178. // OnStart implements BaseService
  179. func (c *MConnection) OnStart(ctx context.Context) error {
  180. c.flushTimer = timer.NewThrottleTimer("flush", c.config.FlushThrottle)
  181. c.pingTimer = time.NewTicker(c.config.PingInterval)
  182. c.pongTimeoutCh = make(chan bool, 1)
  183. c.chStatsTimer = time.NewTicker(updateStats)
  184. c.quitSendRoutine = make(chan struct{})
  185. c.doneSendRoutine = make(chan struct{})
  186. c.quitRecvRoutine = make(chan struct{})
  187. go c.sendRoutine(ctx)
  188. go c.recvRoutine(ctx)
  189. return nil
  190. }
  191. // stopServices stops the BaseService and timers and closes the quitSendRoutine.
  192. // if the quitSendRoutine was already closed, it returns true, otherwise it returns false.
  193. // It uses the stopMtx to ensure only one of FlushStop and OnStop can do this at a time.
  194. func (c *MConnection) stopServices() (alreadyStopped bool) {
  195. c.stopMtx.Lock()
  196. defer c.stopMtx.Unlock()
  197. select {
  198. case <-c.quitSendRoutine:
  199. // already quit
  200. return true
  201. default:
  202. }
  203. select {
  204. case <-c.quitRecvRoutine:
  205. // already quit
  206. return true
  207. default:
  208. }
  209. c.flushTimer.Stop()
  210. c.pingTimer.Stop()
  211. c.chStatsTimer.Stop()
  212. // inform the recvRouting that we are shutting down
  213. close(c.quitRecvRoutine)
  214. close(c.quitSendRoutine)
  215. return false
  216. }
  217. // OnStop implements BaseService
  218. func (c *MConnection) OnStop() {
  219. if c.stopServices() {
  220. return
  221. }
  222. c.conn.Close()
  223. // We can't close pong safely here because
  224. // recvRoutine may write to it after we've stopped.
  225. // Though it doesn't need to get closed at all,
  226. // we close it @ recvRoutine.
  227. }
  228. func (c *MConnection) String() string {
  229. return fmt.Sprintf("MConn{%v}", c.conn.RemoteAddr())
  230. }
  231. func (c *MConnection) flush() {
  232. c.logger.Debug("Flush", "conn", c)
  233. err := c.bufConnWriter.Flush()
  234. if err != nil {
  235. c.logger.Debug("MConnection flush failed", "err", err)
  236. }
  237. }
  238. // Catch panics, usually caused by remote disconnects.
  239. func (c *MConnection) _recover(ctx context.Context) {
  240. if r := recover(); r != nil {
  241. c.logger.Error("MConnection panicked", "err", r, "stack", string(debug.Stack()))
  242. c.stopForError(ctx, fmt.Errorf("recovered from panic: %v", r))
  243. }
  244. }
  245. func (c *MConnection) stopForError(ctx context.Context, r interface{}) {
  246. if err := c.Stop(); err != nil {
  247. c.logger.Error("error stopping connection", "err", err)
  248. }
  249. if atomic.CompareAndSwapUint32(&c.errored, 0, 1) {
  250. if c.onError != nil {
  251. c.onError(ctx, r)
  252. }
  253. }
  254. }
  255. // Queues a message to be sent to channel.
  256. func (c *MConnection) Send(chID ChannelID, msgBytes []byte) bool {
  257. if !c.IsRunning() {
  258. return false
  259. }
  260. c.logger.Debug("Send", "channel", chID, "conn", c, "msgBytes", msgBytes)
  261. // Send message to channel.
  262. channel, ok := c.channelsIdx[chID]
  263. if !ok {
  264. c.logger.Error(fmt.Sprintf("Cannot send bytes, unknown channel %X", chID))
  265. return false
  266. }
  267. success := channel.sendBytes(msgBytes)
  268. if success {
  269. // Wake up sendRoutine if necessary
  270. select {
  271. case c.send <- struct{}{}:
  272. default:
  273. }
  274. } else {
  275. c.logger.Debug("Send failed", "channel", chID, "conn", c, "msgBytes", msgBytes)
  276. }
  277. return success
  278. }
  279. // sendRoutine polls for packets to send from channels.
  280. func (c *MConnection) sendRoutine(ctx context.Context) {
  281. defer c._recover(ctx)
  282. protoWriter := protoio.NewDelimitedWriter(c.bufConnWriter)
  283. FOR_LOOP:
  284. for {
  285. var _n int
  286. var err error
  287. SELECTION:
  288. select {
  289. case <-c.flushTimer.Ch:
  290. // NOTE: flushTimer.Set() must be called every time
  291. // something is written to .bufConnWriter.
  292. c.flush()
  293. case <-c.chStatsTimer.C:
  294. for _, channel := range c.channels {
  295. channel.updateStats()
  296. }
  297. case <-c.pingTimer.C:
  298. _n, err = protoWriter.WriteMsg(mustWrapPacket(&tmp2p.PacketPing{}))
  299. if err != nil {
  300. c.logger.Error("Failed to send PacketPing", "err", err)
  301. break SELECTION
  302. }
  303. c.sendMonitor.Update(_n)
  304. c.logger.Debug("Starting pong timer", "dur", c.config.PongTimeout)
  305. c.pongTimer = time.AfterFunc(c.config.PongTimeout, func() {
  306. select {
  307. case c.pongTimeoutCh <- true:
  308. default:
  309. }
  310. })
  311. c.flush()
  312. case timeout := <-c.pongTimeoutCh:
  313. if timeout {
  314. err = errors.New("pong timeout")
  315. } else {
  316. c.stopPongTimer()
  317. }
  318. case <-c.pong:
  319. _n, err = protoWriter.WriteMsg(mustWrapPacket(&tmp2p.PacketPong{}))
  320. if err != nil {
  321. c.logger.Error("Failed to send PacketPong", "err", err)
  322. break SELECTION
  323. }
  324. c.sendMonitor.Update(_n)
  325. c.flush()
  326. case <-ctx.Done():
  327. break FOR_LOOP
  328. case <-c.quitSendRoutine:
  329. break FOR_LOOP
  330. case <-c.send:
  331. // Send some PacketMsgs
  332. eof := c.sendSomePacketMsgs(ctx)
  333. if !eof {
  334. // Keep sendRoutine awake.
  335. select {
  336. case c.send <- struct{}{}:
  337. default:
  338. }
  339. }
  340. }
  341. if !c.IsRunning() {
  342. break FOR_LOOP
  343. }
  344. if err != nil {
  345. c.logger.Error("Connection failed @ sendRoutine", "conn", c, "err", err)
  346. c.stopForError(ctx, err)
  347. break FOR_LOOP
  348. }
  349. }
  350. // Cleanup
  351. c.stopPongTimer()
  352. close(c.doneSendRoutine)
  353. }
  354. // Returns true if messages from channels were exhausted.
  355. // Blocks in accordance to .sendMonitor throttling.
  356. func (c *MConnection) sendSomePacketMsgs(ctx context.Context) bool {
  357. // Block until .sendMonitor says we can write.
  358. // Once we're ready we send more than we asked for,
  359. // but amortized it should even out.
  360. c.sendMonitor.Limit(c._maxPacketMsgSize, atomic.LoadInt64(&c.config.SendRate), true)
  361. // Now send some PacketMsgs.
  362. for i := 0; i < numBatchPacketMsgs; i++ {
  363. if c.sendPacketMsg(ctx) {
  364. return true
  365. }
  366. }
  367. return false
  368. }
  369. // Returns true if messages from channels were exhausted.
  370. func (c *MConnection) sendPacketMsg(ctx context.Context) bool {
  371. // Choose a channel to create a PacketMsg from.
  372. // The chosen channel will be the one whose recentlySent/priority is the least.
  373. var leastRatio float32 = math.MaxFloat32
  374. var leastChannel *channel
  375. for _, channel := range c.channels {
  376. // If nothing to send, skip this channel
  377. if !channel.isSendPending() {
  378. continue
  379. }
  380. // Get ratio, and keep track of lowest ratio.
  381. ratio := float32(channel.recentlySent) / float32(channel.desc.Priority)
  382. if ratio < leastRatio {
  383. leastRatio = ratio
  384. leastChannel = channel
  385. }
  386. }
  387. // Nothing to send?
  388. if leastChannel == nil {
  389. return true
  390. }
  391. // c.logger.Info("Found a msgPacket to send")
  392. // Make & send a PacketMsg from this channel
  393. _n, err := leastChannel.writePacketMsgTo(c.bufConnWriter)
  394. if err != nil {
  395. c.logger.Error("Failed to write PacketMsg", "err", err)
  396. c.stopForError(ctx, err)
  397. return true
  398. }
  399. c.sendMonitor.Update(_n)
  400. c.flushTimer.Set()
  401. return false
  402. }
  403. // recvRoutine reads PacketMsgs and reconstructs the message using the channels' "recving" buffer.
  404. // After a whole message has been assembled, it's pushed to onReceive().
  405. // Blocks depending on how the connection is throttled.
  406. // Otherwise, it never blocks.
  407. func (c *MConnection) recvRoutine(ctx context.Context) {
  408. defer c._recover(ctx)
  409. protoReader := protoio.NewDelimitedReader(c.bufConnReader, c._maxPacketMsgSize)
  410. FOR_LOOP:
  411. for {
  412. // Block until .recvMonitor says we can read.
  413. c.recvMonitor.Limit(c._maxPacketMsgSize, atomic.LoadInt64(&c.config.RecvRate), true)
  414. // Peek into bufConnReader for debugging
  415. /*
  416. if numBytes := c.bufConnReader.Buffered(); numBytes > 0 {
  417. bz, err := c.bufConnReader.Peek(tmmath.MinInt(numBytes, 100))
  418. if err == nil {
  419. // return
  420. } else {
  421. c.logger.Debug("error peeking connection buffer", "err", err)
  422. // return nil
  423. }
  424. c.logger.Info("Peek connection buffer", "numBytes", numBytes, "bz", bz)
  425. }
  426. */
  427. // Read packet type
  428. var packet tmp2p.Packet
  429. _n, err := protoReader.ReadMsg(&packet)
  430. c.recvMonitor.Update(_n)
  431. if err != nil {
  432. // stopServices was invoked and we are shutting down
  433. // receiving is excpected to fail since we will close the connection
  434. select {
  435. case <-ctx.Done():
  436. case <-c.quitRecvRoutine:
  437. break FOR_LOOP
  438. default:
  439. }
  440. if c.IsRunning() {
  441. if err == io.EOF {
  442. c.logger.Info("Connection is closed @ recvRoutine (likely by the other side)", "conn", c)
  443. } else {
  444. c.logger.Debug("Connection failed @ recvRoutine (reading byte)", "conn", c, "err", err)
  445. }
  446. c.stopForError(ctx, err)
  447. }
  448. break FOR_LOOP
  449. }
  450. // Read more depending on packet type.
  451. switch pkt := packet.Sum.(type) {
  452. case *tmp2p.Packet_PacketPing:
  453. // TODO: prevent abuse, as they cause flush()'s.
  454. // https://github.com/tendermint/tendermint/issues/1190
  455. select {
  456. case c.pong <- struct{}{}:
  457. default:
  458. // never block
  459. }
  460. case *tmp2p.Packet_PacketPong:
  461. select {
  462. case c.pongTimeoutCh <- false:
  463. default:
  464. // never block
  465. }
  466. case *tmp2p.Packet_PacketMsg:
  467. channelID := ChannelID(pkt.PacketMsg.ChannelID)
  468. channel, ok := c.channelsIdx[channelID]
  469. if pkt.PacketMsg.ChannelID < 0 || pkt.PacketMsg.ChannelID > math.MaxUint8 || !ok || channel == nil {
  470. err := fmt.Errorf("unknown channel %X", pkt.PacketMsg.ChannelID)
  471. c.logger.Debug("Connection failed @ recvRoutine", "conn", c, "err", err)
  472. c.stopForError(ctx, err)
  473. break FOR_LOOP
  474. }
  475. msgBytes, err := channel.recvPacketMsg(*pkt.PacketMsg)
  476. if err != nil {
  477. if c.IsRunning() {
  478. c.logger.Debug("Connection failed @ recvRoutine", "conn", c, "err", err)
  479. c.stopForError(ctx, err)
  480. }
  481. break FOR_LOOP
  482. }
  483. if msgBytes != nil {
  484. c.logger.Debug("Received bytes", "chID", channelID, "msgBytes", msgBytes)
  485. // NOTE: This means the reactor.Receive runs in the same thread as the p2p recv routine
  486. c.onReceive(ctx, channelID, msgBytes)
  487. }
  488. default:
  489. err := fmt.Errorf("unknown message type %v", reflect.TypeOf(packet))
  490. c.logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err)
  491. c.stopForError(ctx, err)
  492. break FOR_LOOP
  493. }
  494. }
  495. // Cleanup
  496. close(c.pong)
  497. for range c.pong {
  498. // Drain
  499. }
  500. }
  501. // not goroutine-safe
  502. func (c *MConnection) stopPongTimer() {
  503. if c.pongTimer != nil {
  504. _ = c.pongTimer.Stop()
  505. c.pongTimer = nil
  506. }
  507. }
  508. // maxPacketMsgSize returns a maximum size of PacketMsg
  509. func (c *MConnection) maxPacketMsgSize() int {
  510. bz, err := proto.Marshal(mustWrapPacket(&tmp2p.PacketMsg{
  511. ChannelID: 0x01,
  512. EOF: true,
  513. Data: make([]byte, c.config.MaxPacketMsgPayloadSize),
  514. }))
  515. if err != nil {
  516. panic(err)
  517. }
  518. return len(bz)
  519. }
  520. type ChannelStatus struct {
  521. ID byte
  522. SendQueueCapacity int
  523. SendQueueSize int
  524. Priority int
  525. RecentlySent int64
  526. }
  527. //-----------------------------------------------------------------------------
  528. // ChannelID is an arbitrary channel ID.
  529. type ChannelID uint16
  530. type ChannelDescriptor struct {
  531. ID ChannelID
  532. Priority int
  533. MessageType proto.Message
  534. // TODO: Remove once p2p refactor is complete.
  535. SendQueueCapacity int
  536. RecvMessageCapacity int
  537. // RecvBufferCapacity defines the max buffer size of inbound messages for a
  538. // given p2p Channel queue.
  539. RecvBufferCapacity int
  540. }
  541. func (chDesc ChannelDescriptor) FillDefaults() (filled ChannelDescriptor) {
  542. if chDesc.SendQueueCapacity == 0 {
  543. chDesc.SendQueueCapacity = defaultSendQueueCapacity
  544. }
  545. if chDesc.RecvBufferCapacity == 0 {
  546. chDesc.RecvBufferCapacity = defaultRecvBufferCapacity
  547. }
  548. if chDesc.RecvMessageCapacity == 0 {
  549. chDesc.RecvMessageCapacity = defaultRecvMessageCapacity
  550. }
  551. filled = chDesc
  552. return
  553. }
  554. // NOTE: not goroutine-safe.
  555. type channel struct {
  556. // Exponential moving average.
  557. // This field must be accessed atomically.
  558. // It is first in the struct to ensure correct alignment.
  559. // See https://github.com/tendermint/tendermint/issues/7000.
  560. recentlySent int64
  561. conn *MConnection
  562. desc ChannelDescriptor
  563. sendQueue chan []byte
  564. sendQueueSize int32 // atomic.
  565. recving []byte
  566. sending []byte
  567. maxPacketMsgPayloadSize int
  568. logger log.Logger
  569. }
  570. func newChannel(conn *MConnection, desc ChannelDescriptor) *channel {
  571. desc = desc.FillDefaults()
  572. if desc.Priority <= 0 {
  573. panic("Channel default priority must be a positive integer")
  574. }
  575. return &channel{
  576. conn: conn,
  577. desc: desc,
  578. sendQueue: make(chan []byte, desc.SendQueueCapacity),
  579. recving: make([]byte, 0, desc.RecvBufferCapacity),
  580. maxPacketMsgPayloadSize: conn.config.MaxPacketMsgPayloadSize,
  581. logger: conn.logger,
  582. }
  583. }
  584. // Queues message to send to this channel.
  585. // Goroutine-safe
  586. // Times out (and returns false) after defaultSendTimeout
  587. func (ch *channel) sendBytes(bytes []byte) bool {
  588. select {
  589. case ch.sendQueue <- bytes:
  590. atomic.AddInt32(&ch.sendQueueSize, 1)
  591. return true
  592. case <-time.After(defaultSendTimeout):
  593. return false
  594. }
  595. }
  596. // Returns true if any PacketMsgs are pending to be sent.
  597. // Call before calling nextPacketMsg()
  598. // Goroutine-safe
  599. func (ch *channel) isSendPending() bool {
  600. if len(ch.sending) == 0 {
  601. if len(ch.sendQueue) == 0 {
  602. return false
  603. }
  604. ch.sending = <-ch.sendQueue
  605. }
  606. return true
  607. }
  608. // Creates a new PacketMsg to send.
  609. // Not goroutine-safe
  610. func (ch *channel) nextPacketMsg() tmp2p.PacketMsg {
  611. packet := tmp2p.PacketMsg{ChannelID: int32(ch.desc.ID)}
  612. maxSize := ch.maxPacketMsgPayloadSize
  613. packet.Data = ch.sending[:tmmath.MinInt(maxSize, len(ch.sending))]
  614. if len(ch.sending) <= maxSize {
  615. packet.EOF = true
  616. ch.sending = nil
  617. atomic.AddInt32(&ch.sendQueueSize, -1) // decrement sendQueueSize
  618. } else {
  619. packet.EOF = false
  620. ch.sending = ch.sending[tmmath.MinInt(maxSize, len(ch.sending)):]
  621. }
  622. return packet
  623. }
  624. // Writes next PacketMsg to w and updates c.recentlySent.
  625. // Not goroutine-safe
  626. func (ch *channel) writePacketMsgTo(w io.Writer) (n int, err error) {
  627. packet := ch.nextPacketMsg()
  628. n, err = protoio.NewDelimitedWriter(w).WriteMsg(mustWrapPacket(&packet))
  629. atomic.AddInt64(&ch.recentlySent, int64(n))
  630. return
  631. }
  632. // Handles incoming PacketMsgs. It returns a message bytes if message is
  633. // complete, which is owned by the caller and will not be modified.
  634. // Not goroutine-safe
  635. func (ch *channel) recvPacketMsg(packet tmp2p.PacketMsg) ([]byte, error) {
  636. ch.logger.Debug("Read PacketMsg", "conn", ch.conn, "packet", packet)
  637. var recvCap, recvReceived = ch.desc.RecvMessageCapacity, len(ch.recving) + len(packet.Data)
  638. if recvCap < recvReceived {
  639. return nil, fmt.Errorf("received message exceeds available capacity: %v < %v", recvCap, recvReceived)
  640. }
  641. ch.recving = append(ch.recving, packet.Data...)
  642. if packet.EOF {
  643. msgBytes := ch.recving
  644. ch.recving = make([]byte, 0, ch.desc.RecvBufferCapacity)
  645. return msgBytes, nil
  646. }
  647. return nil, nil
  648. }
  649. // Call this periodically to update stats for throttling purposes.
  650. // Not goroutine-safe
  651. func (ch *channel) updateStats() {
  652. // Exponential decay of stats.
  653. // TODO: optimize.
  654. atomic.StoreInt64(&ch.recentlySent, int64(float64(atomic.LoadInt64(&ch.recentlySent))*0.8))
  655. }
  656. //----------------------------------------
  657. // Packet
  658. // mustWrapPacket takes a packet kind (oneof) and wraps it in a tmp2p.Packet message.
  659. func mustWrapPacket(pb proto.Message) *tmp2p.Packet {
  660. var msg tmp2p.Packet
  661. switch pb := pb.(type) {
  662. case *tmp2p.Packet: // already a packet
  663. msg = *pb
  664. case *tmp2p.PacketPing:
  665. msg = tmp2p.Packet{
  666. Sum: &tmp2p.Packet_PacketPing{
  667. PacketPing: pb,
  668. },
  669. }
  670. case *tmp2p.PacketPong:
  671. msg = tmp2p.Packet{
  672. Sum: &tmp2p.Packet_PacketPong{
  673. PacketPong: pb,
  674. },
  675. }
  676. case *tmp2p.PacketMsg:
  677. msg = tmp2p.Packet{
  678. Sum: &tmp2p.Packet_PacketMsg{
  679. PacketMsg: pb,
  680. },
  681. }
  682. default:
  683. panic(fmt.Errorf("unknown packet type %T", pb))
  684. }
  685. return &msg
  686. }