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.

707 lines
19 KiB

9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package p2p
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "math"
  7. "net"
  8. "runtime/debug"
  9. "sync/atomic"
  10. "time"
  11. wire "github.com/tendermint/go-wire"
  12. cmn "github.com/tendermint/tmlibs/common"
  13. flow "github.com/tendermint/tmlibs/flowrate"
  14. )
  15. const (
  16. numBatchMsgPackets = 10
  17. minReadBufferSize = 1024
  18. minWriteBufferSize = 65536
  19. updateState = 2 * time.Second
  20. pingTimeout = 40 * time.Second
  21. // some of these defaults are written in the user config
  22. // flushThrottle, sendRate, recvRate
  23. // TODO: remove values present in config
  24. defaultFlushThrottle = 100 * time.Millisecond
  25. defaultSendQueueCapacity = 1
  26. defaultRecvBufferCapacity = 4096
  27. defaultRecvMessageCapacity = 22020096 // 21MB
  28. defaultSendRate = int64(512000) // 500KB/s
  29. defaultRecvRate = int64(512000) // 500KB/s
  30. defaultSendTimeout = 10 * time.Second
  31. )
  32. type receiveCbFunc func(chID byte, msgBytes []byte)
  33. type errorCbFunc func(interface{})
  34. /*
  35. Each peer has one `MConnection` (multiplex connection) instance.
  36. __multiplex__ *noun* a system or signal involving simultaneous transmission of
  37. several messages along a single channel of communication.
  38. Each `MConnection` handles message transmission on multiple abstract communication
  39. `Channel`s. Each channel has a globally unique byte id.
  40. The byte id and the relative priorities of each `Channel` are configured upon
  41. initialization of the connection.
  42. There are two methods for sending messages:
  43. func (m MConnection) Send(chID byte, msg interface{}) bool {}
  44. func (m MConnection) TrySend(chID byte, msg interface{}) bool {}
  45. `Send(chID, msg)` is a blocking call that waits until `msg` is successfully queued
  46. for the channel with the given id byte `chID`, or until the request times out.
  47. The message `msg` is serialized using the `tendermint/wire` submodule's
  48. `WriteBinary()` reflection routine.
  49. `TrySend(chID, msg)` is a nonblocking call that returns false if the channel's
  50. queue is full.
  51. Inbound message bytes are handled with an onReceive callback function.
  52. */
  53. type MConnection struct {
  54. cmn.BaseService
  55. conn net.Conn
  56. bufReader *bufio.Reader
  57. bufWriter *bufio.Writer
  58. sendMonitor *flow.Monitor
  59. recvMonitor *flow.Monitor
  60. send chan struct{}
  61. pong chan struct{}
  62. channels []*Channel
  63. channelsIdx map[byte]*Channel
  64. onReceive receiveCbFunc
  65. onError errorCbFunc
  66. errored uint32
  67. config *MConnConfig
  68. quit chan struct{}
  69. flushTimer *cmn.ThrottleTimer // flush writes as necessary but throttled.
  70. pingTimer *cmn.RepeatTimer // send pings periodically
  71. chStatsTimer *cmn.RepeatTimer // update channel stats periodically
  72. LocalAddress *NetAddress
  73. RemoteAddress *NetAddress
  74. }
  75. // MConnConfig is a MConnection configuration.
  76. type MConnConfig struct {
  77. SendRate int64 `mapstructure:"send_rate"`
  78. RecvRate int64 `mapstructure:"recv_rate"`
  79. maxMsgPacketPayloadSize int
  80. flushThrottle time.Duration
  81. }
  82. func (cfg *MConnConfig) maxMsgPacketTotalSize() int {
  83. return cfg.maxMsgPacketPayloadSize + maxMsgPacketOverheadSize
  84. }
  85. // DefaultMConnConfig returns the default config.
  86. func DefaultMConnConfig() *MConnConfig {
  87. return &MConnConfig{
  88. SendRate: defaultSendRate,
  89. RecvRate: defaultRecvRate,
  90. maxMsgPacketPayloadSize: defaultMaxMsgPacketPayloadSize,
  91. flushThrottle: defaultFlushThrottle,
  92. }
  93. }
  94. // NewMConnection wraps net.Conn and creates multiplex connection
  95. func NewMConnection(conn net.Conn, chDescs []*ChannelDescriptor, onReceive receiveCbFunc, onError errorCbFunc) *MConnection {
  96. return NewMConnectionWithConfig(
  97. conn,
  98. chDescs,
  99. onReceive,
  100. onError,
  101. DefaultMConnConfig())
  102. }
  103. // NewMConnectionWithConfig wraps net.Conn and creates multiplex connection with a config
  104. func NewMConnectionWithConfig(conn net.Conn, chDescs []*ChannelDescriptor, onReceive receiveCbFunc, onError errorCbFunc, config *MConnConfig) *MConnection {
  105. mconn := &MConnection{
  106. conn: conn,
  107. bufReader: bufio.NewReaderSize(conn, minReadBufferSize),
  108. bufWriter: bufio.NewWriterSize(conn, minWriteBufferSize),
  109. sendMonitor: flow.New(0, 0),
  110. recvMonitor: flow.New(0, 0),
  111. send: make(chan struct{}, 1),
  112. pong: make(chan struct{}),
  113. onReceive: onReceive,
  114. onError: onError,
  115. config: config,
  116. LocalAddress: NewNetAddress(conn.LocalAddr()),
  117. RemoteAddress: NewNetAddress(conn.RemoteAddr()),
  118. }
  119. // Create channels
  120. var channelsIdx = map[byte]*Channel{}
  121. var channels = []*Channel{}
  122. for _, desc := range chDescs {
  123. descCopy := *desc // copy the desc else unsafe access across connections
  124. channel := newChannel(mconn, &descCopy)
  125. channelsIdx[channel.id] = channel
  126. channels = append(channels, channel)
  127. }
  128. mconn.channels = channels
  129. mconn.channelsIdx = channelsIdx
  130. mconn.BaseService = *cmn.NewBaseService(nil, "MConnection", mconn)
  131. return mconn
  132. }
  133. // OnStart implements BaseService
  134. func (c *MConnection) OnStart() error {
  135. c.BaseService.OnStart()
  136. c.quit = make(chan struct{})
  137. c.flushTimer = cmn.NewThrottleTimer("flush", c.config.flushThrottle)
  138. c.pingTimer = cmn.NewRepeatTimer("ping", pingTimeout)
  139. c.chStatsTimer = cmn.NewRepeatTimer("chStats", updateState)
  140. go c.sendRoutine()
  141. go c.recvRoutine()
  142. return nil
  143. }
  144. // OnStop implements BaseService
  145. func (c *MConnection) OnStop() {
  146. c.BaseService.OnStop()
  147. c.flushTimer.Stop()
  148. c.pingTimer.Stop()
  149. c.chStatsTimer.Stop()
  150. if c.quit != nil {
  151. close(c.quit)
  152. }
  153. c.conn.Close()
  154. // We can't close pong safely here because
  155. // recvRoutine may write to it after we've stopped.
  156. // Though it doesn't need to get closed at all,
  157. // we close it @ recvRoutine.
  158. // close(c.pong)
  159. }
  160. func (c *MConnection) String() string {
  161. return fmt.Sprintf("MConn{%v}", c.conn.RemoteAddr())
  162. }
  163. func (c *MConnection) flush() {
  164. c.Logger.Debug("Flush", "conn", c)
  165. err := c.bufWriter.Flush()
  166. if err != nil {
  167. c.Logger.Error("MConnection flush failed", "err", err)
  168. }
  169. }
  170. // Catch panics, usually caused by remote disconnects.
  171. func (c *MConnection) _recover() {
  172. if r := recover(); r != nil {
  173. stack := debug.Stack()
  174. err := cmn.StackError{r, stack}
  175. c.stopForError(err)
  176. }
  177. }
  178. func (c *MConnection) stopForError(r interface{}) {
  179. c.Stop()
  180. if atomic.CompareAndSwapUint32(&c.errored, 0, 1) {
  181. if c.onError != nil {
  182. c.onError(r)
  183. }
  184. }
  185. }
  186. // Queues a message to be sent to channel.
  187. func (c *MConnection) Send(chID byte, msg interface{}) bool {
  188. if !c.IsRunning() {
  189. return false
  190. }
  191. c.Logger.Debug("Send", "channel", chID, "conn", c, "msg", msg) //, "bytes", wire.BinaryBytes(msg))
  192. // Send message to channel.
  193. channel, ok := c.channelsIdx[chID]
  194. if !ok {
  195. c.Logger.Error(cmn.Fmt("Cannot send bytes, unknown channel %X", chID))
  196. return false
  197. }
  198. success := channel.sendBytes(wire.BinaryBytes(msg))
  199. if success {
  200. // Wake up sendRoutine if necessary
  201. select {
  202. case c.send <- struct{}{}:
  203. default:
  204. }
  205. } else {
  206. c.Logger.Error("Send failed", "channel", chID, "conn", c, "msg", msg)
  207. }
  208. return success
  209. }
  210. // Queues a message to be sent to channel.
  211. // Nonblocking, returns true if successful.
  212. func (c *MConnection) TrySend(chID byte, msg interface{}) bool {
  213. if !c.IsRunning() {
  214. return false
  215. }
  216. c.Logger.Debug("TrySend", "channel", chID, "conn", c, "msg", msg)
  217. // Send message to channel.
  218. channel, ok := c.channelsIdx[chID]
  219. if !ok {
  220. c.Logger.Error(cmn.Fmt("Cannot send bytes, unknown channel %X", chID))
  221. return false
  222. }
  223. ok = channel.trySendBytes(wire.BinaryBytes(msg))
  224. if ok {
  225. // Wake up sendRoutine if necessary
  226. select {
  227. case c.send <- struct{}{}:
  228. default:
  229. }
  230. }
  231. return ok
  232. }
  233. // CanSend returns true if you can send more data onto the chID, false
  234. // otherwise. Use only as a heuristic.
  235. func (c *MConnection) CanSend(chID byte) bool {
  236. if !c.IsRunning() {
  237. return false
  238. }
  239. channel, ok := c.channelsIdx[chID]
  240. if !ok {
  241. c.Logger.Error(cmn.Fmt("Unknown channel %X", chID))
  242. return false
  243. }
  244. return channel.canSend()
  245. }
  246. // sendRoutine polls for packets to send from channels.
  247. func (c *MConnection) sendRoutine() {
  248. defer c._recover()
  249. FOR_LOOP:
  250. for {
  251. var n int
  252. var err error
  253. select {
  254. case <-c.flushTimer.Ch:
  255. // NOTE: flushTimer.Set() must be called every time
  256. // something is written to .bufWriter.
  257. c.flush()
  258. case <-c.chStatsTimer.Ch:
  259. for _, channel := range c.channels {
  260. channel.updateStats()
  261. }
  262. case <-c.pingTimer.Ch:
  263. c.Logger.Debug("Send Ping")
  264. wire.WriteByte(packetTypePing, c.bufWriter, &n, &err)
  265. c.sendMonitor.Update(int(n))
  266. c.flush()
  267. case <-c.pong:
  268. c.Logger.Debug("Send Pong")
  269. wire.WriteByte(packetTypePong, c.bufWriter, &n, &err)
  270. c.sendMonitor.Update(int(n))
  271. c.flush()
  272. case <-c.quit:
  273. break FOR_LOOP
  274. case <-c.send:
  275. // Send some msgPackets
  276. eof := c.sendSomeMsgPackets()
  277. if !eof {
  278. // Keep sendRoutine awake.
  279. select {
  280. case c.send <- struct{}{}:
  281. default:
  282. }
  283. }
  284. }
  285. if !c.IsRunning() {
  286. break FOR_LOOP
  287. }
  288. if err != nil {
  289. c.Logger.Error("Connection failed @ sendRoutine", "conn", c, "err", err)
  290. c.stopForError(err)
  291. break FOR_LOOP
  292. }
  293. }
  294. // Cleanup
  295. }
  296. // Returns true if messages from channels were exhausted.
  297. // Blocks in accordance to .sendMonitor throttling.
  298. func (c *MConnection) sendSomeMsgPackets() bool {
  299. // Block until .sendMonitor says we can write.
  300. // Once we're ready we send more than we asked for,
  301. // but amortized it should even out.
  302. c.sendMonitor.Limit(c.config.maxMsgPacketTotalSize(), atomic.LoadInt64(&c.config.SendRate), true)
  303. // Now send some msgPackets.
  304. for i := 0; i < numBatchMsgPackets; i++ {
  305. if c.sendMsgPacket() {
  306. return true
  307. }
  308. }
  309. return false
  310. }
  311. // Returns true if messages from channels were exhausted.
  312. func (c *MConnection) sendMsgPacket() bool {
  313. // Choose a channel to create a msgPacket from.
  314. // The chosen channel will be the one whose recentlySent/priority is the least.
  315. var leastRatio float32 = math.MaxFloat32
  316. var leastChannel *Channel
  317. for _, channel := range c.channels {
  318. // If nothing to send, skip this channel
  319. if !channel.isSendPending() {
  320. continue
  321. }
  322. // Get ratio, and keep track of lowest ratio.
  323. ratio := float32(channel.recentlySent) / float32(channel.priority)
  324. if ratio < leastRatio {
  325. leastRatio = ratio
  326. leastChannel = channel
  327. }
  328. }
  329. // Nothing to send?
  330. if leastChannel == nil {
  331. return true
  332. } else {
  333. // c.Logger.Info("Found a msgPacket to send")
  334. }
  335. // Make & send a msgPacket from this channel
  336. n, err := leastChannel.writeMsgPacketTo(c.bufWriter)
  337. if err != nil {
  338. c.Logger.Error("Failed to write msgPacket", "err", err)
  339. c.stopForError(err)
  340. return true
  341. }
  342. c.sendMonitor.Update(int(n))
  343. c.flushTimer.Set()
  344. return false
  345. }
  346. // recvRoutine reads msgPackets and reconstructs the message using the channels' "recving" buffer.
  347. // After a whole message has been assembled, it's pushed to onReceive().
  348. // Blocks depending on how the connection is throttled.
  349. func (c *MConnection) recvRoutine() {
  350. defer c._recover()
  351. FOR_LOOP:
  352. for {
  353. // Block until .recvMonitor says we can read.
  354. c.recvMonitor.Limit(c.config.maxMsgPacketTotalSize(), atomic.LoadInt64(&c.config.RecvRate), true)
  355. /*
  356. // Peek into bufReader for debugging
  357. if numBytes := c.bufReader.Buffered(); numBytes > 0 {
  358. log.Info("Peek connection buffer", "numBytes", numBytes, "bytes", log15.Lazy{func() []byte {
  359. bytes, err := c.bufReader.Peek(MinInt(numBytes, 100))
  360. if err == nil {
  361. return bytes
  362. } else {
  363. log.Warn("Error peeking connection buffer", "err", err)
  364. return nil
  365. }
  366. }})
  367. }
  368. */
  369. // Read packet type
  370. var n int
  371. var err error
  372. pktType := wire.ReadByte(c.bufReader, &n, &err)
  373. c.recvMonitor.Update(int(n))
  374. if err != nil {
  375. if c.IsRunning() {
  376. c.Logger.Error("Connection failed @ recvRoutine (reading byte)", "conn", c, "err", err)
  377. c.stopForError(err)
  378. }
  379. break FOR_LOOP
  380. }
  381. // Read more depending on packet type.
  382. switch pktType {
  383. case packetTypePing:
  384. // TODO: prevent abuse, as they cause flush()'s.
  385. c.Logger.Debug("Receive Ping")
  386. c.pong <- struct{}{}
  387. case packetTypePong:
  388. // do nothing
  389. c.Logger.Debug("Receive Pong")
  390. case packetTypeMsg:
  391. pkt, n, err := msgPacket{}, int(0), error(nil)
  392. wire.ReadBinaryPtr(&pkt, c.bufReader, c.config.maxMsgPacketTotalSize(), &n, &err)
  393. c.recvMonitor.Update(int(n))
  394. if err != nil {
  395. if c.IsRunning() {
  396. c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err)
  397. c.stopForError(err)
  398. }
  399. break FOR_LOOP
  400. }
  401. channel, ok := c.channelsIdx[pkt.ChannelID]
  402. if !ok || channel == nil {
  403. cmn.PanicQ(cmn.Fmt("Unknown channel %X", pkt.ChannelID))
  404. }
  405. msgBytes, err := channel.recvMsgPacket(pkt)
  406. if err != nil {
  407. if c.IsRunning() {
  408. c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err)
  409. c.stopForError(err)
  410. }
  411. break FOR_LOOP
  412. }
  413. if msgBytes != nil {
  414. c.Logger.Debug("Received bytes", "chID", pkt.ChannelID, "msgBytes", msgBytes)
  415. // NOTE: This means the reactor.Receive runs in the same thread as the p2p recv routine
  416. c.onReceive(pkt.ChannelID, msgBytes)
  417. }
  418. default:
  419. cmn.PanicSanity(cmn.Fmt("Unknown message type %X", pktType))
  420. }
  421. // TODO: shouldn't this go in the sendRoutine?
  422. // Better to send a ping packet when *we* haven't sent anything for a while.
  423. c.pingTimer.Reset()
  424. }
  425. // Cleanup
  426. close(c.pong)
  427. for range c.pong {
  428. // Drain
  429. }
  430. }
  431. type ConnectionStatus struct {
  432. SendMonitor flow.Status
  433. RecvMonitor flow.Status
  434. Channels []ChannelStatus
  435. }
  436. type ChannelStatus struct {
  437. ID byte
  438. SendQueueCapacity int
  439. SendQueueSize int
  440. Priority int
  441. RecentlySent int64
  442. }
  443. func (c *MConnection) Status() ConnectionStatus {
  444. var status ConnectionStatus
  445. status.SendMonitor = c.sendMonitor.Status()
  446. status.RecvMonitor = c.recvMonitor.Status()
  447. status.Channels = make([]ChannelStatus, len(c.channels))
  448. for i, channel := range c.channels {
  449. status.Channels[i] = ChannelStatus{
  450. ID: channel.id,
  451. SendQueueCapacity: cap(channel.sendQueue),
  452. SendQueueSize: int(channel.sendQueueSize), // TODO use atomic
  453. Priority: channel.priority,
  454. RecentlySent: channel.recentlySent,
  455. }
  456. }
  457. return status
  458. }
  459. //-----------------------------------------------------------------------------
  460. type ChannelDescriptor struct {
  461. ID byte
  462. Priority int
  463. SendQueueCapacity int
  464. RecvBufferCapacity int
  465. RecvMessageCapacity int
  466. }
  467. func (chDesc *ChannelDescriptor) FillDefaults() {
  468. if chDesc.SendQueueCapacity == 0 {
  469. chDesc.SendQueueCapacity = defaultSendQueueCapacity
  470. }
  471. if chDesc.RecvBufferCapacity == 0 {
  472. chDesc.RecvBufferCapacity = defaultRecvBufferCapacity
  473. }
  474. if chDesc.RecvMessageCapacity == 0 {
  475. chDesc.RecvMessageCapacity = defaultRecvMessageCapacity
  476. }
  477. }
  478. // TODO: lowercase.
  479. // NOTE: not goroutine-safe.
  480. type Channel struct {
  481. conn *MConnection
  482. desc *ChannelDescriptor
  483. id byte
  484. sendQueue chan []byte
  485. sendQueueSize int32 // atomic.
  486. recving []byte
  487. sending []byte
  488. priority int
  489. recentlySent int64 // exponential moving average
  490. maxMsgPacketPayloadSize int
  491. }
  492. func newChannel(conn *MConnection, desc *ChannelDescriptor) *Channel {
  493. desc.FillDefaults()
  494. if desc.Priority <= 0 {
  495. cmn.PanicSanity("Channel default priority must be a postive integer")
  496. }
  497. return &Channel{
  498. conn: conn,
  499. desc: desc,
  500. id: desc.ID,
  501. sendQueue: make(chan []byte, desc.SendQueueCapacity),
  502. recving: make([]byte, 0, desc.RecvBufferCapacity),
  503. priority: desc.Priority,
  504. maxMsgPacketPayloadSize: conn.config.maxMsgPacketPayloadSize,
  505. }
  506. }
  507. // Queues message to send to this channel.
  508. // Goroutine-safe
  509. // Times out (and returns false) after defaultSendTimeout
  510. func (ch *Channel) sendBytes(bytes []byte) bool {
  511. select {
  512. case ch.sendQueue <- bytes:
  513. atomic.AddInt32(&ch.sendQueueSize, 1)
  514. return true
  515. case <-time.After(defaultSendTimeout):
  516. return false
  517. }
  518. }
  519. // Queues message to send to this channel.
  520. // Nonblocking, returns true if successful.
  521. // Goroutine-safe
  522. func (ch *Channel) trySendBytes(bytes []byte) bool {
  523. select {
  524. case ch.sendQueue <- bytes:
  525. atomic.AddInt32(&ch.sendQueueSize, 1)
  526. return true
  527. default:
  528. return false
  529. }
  530. }
  531. // Goroutine-safe
  532. func (ch *Channel) loadSendQueueSize() (size int) {
  533. return int(atomic.LoadInt32(&ch.sendQueueSize))
  534. }
  535. // Goroutine-safe
  536. // Use only as a heuristic.
  537. func (ch *Channel) canSend() bool {
  538. return ch.loadSendQueueSize() < defaultSendQueueCapacity
  539. }
  540. // Returns true if any msgPackets are pending to be sent.
  541. // Call before calling nextMsgPacket()
  542. // Goroutine-safe
  543. func (ch *Channel) isSendPending() bool {
  544. if len(ch.sending) == 0 {
  545. if len(ch.sendQueue) == 0 {
  546. return false
  547. }
  548. ch.sending = <-ch.sendQueue
  549. }
  550. return true
  551. }
  552. // Creates a new msgPacket to send.
  553. // Not goroutine-safe
  554. func (ch *Channel) nextMsgPacket() msgPacket {
  555. packet := msgPacket{}
  556. packet.ChannelID = byte(ch.id)
  557. maxSize := ch.maxMsgPacketPayloadSize
  558. packet.Bytes = ch.sending[:cmn.MinInt(maxSize, len(ch.sending))]
  559. if len(ch.sending) <= maxSize {
  560. packet.EOF = byte(0x01)
  561. ch.sending = nil
  562. atomic.AddInt32(&ch.sendQueueSize, -1) // decrement sendQueueSize
  563. } else {
  564. packet.EOF = byte(0x00)
  565. ch.sending = ch.sending[cmn.MinInt(maxSize, len(ch.sending)):]
  566. }
  567. return packet
  568. }
  569. // Writes next msgPacket to w.
  570. // Not goroutine-safe
  571. func (ch *Channel) writeMsgPacketTo(w io.Writer) (n int, err error) {
  572. packet := ch.nextMsgPacket()
  573. // log.Debug("Write Msg Packet", "conn", ch.conn, "packet", packet)
  574. wire.WriteByte(packetTypeMsg, w, &n, &err)
  575. wire.WriteBinary(packet, w, &n, &err)
  576. if err == nil {
  577. ch.recentlySent += int64(n)
  578. }
  579. return
  580. }
  581. // Handles incoming msgPackets. Returns a msg bytes if msg is complete.
  582. // Not goroutine-safe
  583. func (ch *Channel) recvMsgPacket(packet msgPacket) ([]byte, error) {
  584. // log.Debug("Read Msg Packet", "conn", ch.conn, "packet", packet)
  585. if ch.desc.RecvMessageCapacity < len(ch.recving)+len(packet.Bytes) {
  586. return nil, wire.ErrBinaryReadOverflow
  587. }
  588. ch.recving = append(ch.recving, packet.Bytes...)
  589. if packet.EOF == byte(0x01) {
  590. msgBytes := ch.recving
  591. // clear the slice without re-allocating.
  592. // http://stackoverflow.com/questions/16971741/how-do-you-clear-a-slice-in-go
  593. // suggests this could be a memory leak, but we might as well keep the memory for the channel until it closes,
  594. // at which point the recving slice stops being used and should be garbage collected
  595. ch.recving = ch.recving[:0] // make([]byte, 0, ch.desc.RecvBufferCapacity)
  596. return msgBytes, nil
  597. }
  598. return nil, nil
  599. }
  600. // Call this periodically to update stats for throttling purposes.
  601. // Not goroutine-safe
  602. func (ch *Channel) updateStats() {
  603. // Exponential decay of stats.
  604. // TODO: optimize.
  605. ch.recentlySent = int64(float64(ch.recentlySent) * 0.8)
  606. }
  607. //-----------------------------------------------------------------------------
  608. const (
  609. defaultMaxMsgPacketPayloadSize = 1024
  610. maxMsgPacketOverheadSize = 10 // It's actually lower but good enough
  611. packetTypePing = byte(0x01)
  612. packetTypePong = byte(0x02)
  613. packetTypeMsg = byte(0x03)
  614. )
  615. // Messages in channels are chopped into smaller msgPackets for multiplexing.
  616. type msgPacket struct {
  617. ChannelID byte
  618. EOF byte // 1 means message ends here.
  619. Bytes []byte
  620. }
  621. func (p msgPacket) String() string {
  622. return fmt.Sprintf("MsgPacket{%X:%X T:%X}", p.ChannelID, p.Bytes, p.EOF)
  623. }