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.

632 lines
17 KiB

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