github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/p2p/conn/connection.go (about) 1 package conn 2 3 import ( 4 "bufio" 5 "errors" 6 "fmt" 7 "io" 8 "math" 9 "net" 10 "reflect" 11 "runtime/debug" 12 "sync/atomic" 13 "time" 14 15 "github.com/gogo/protobuf/proto" 16 17 flow "github.com/lazyledger/lazyledger-core/libs/flowrate" 18 "github.com/lazyledger/lazyledger-core/libs/log" 19 tmmath "github.com/lazyledger/lazyledger-core/libs/math" 20 "github.com/lazyledger/lazyledger-core/libs/protoio" 21 "github.com/lazyledger/lazyledger-core/libs/service" 22 tmsync "github.com/lazyledger/lazyledger-core/libs/sync" 23 "github.com/lazyledger/lazyledger-core/libs/timer" 24 tmp2p "github.com/lazyledger/lazyledger-core/proto/tendermint/p2p" 25 ) 26 27 const ( 28 defaultMaxPacketMsgPayloadSize = 1024 29 30 numBatchPacketMsgs = 10 31 minReadBufferSize = 1024 32 minWriteBufferSize = 65536 33 updateStats = 2 * time.Second 34 35 // some of these defaults are written in the user config 36 // flushThrottle, sendRate, recvRate 37 // TODO: remove values present in config 38 defaultFlushThrottle = 100 * time.Millisecond 39 40 defaultSendQueueCapacity = 1 41 defaultRecvBufferCapacity = 4096 42 defaultRecvMessageCapacity = 22020096 // 21MB 43 defaultSendRate = int64(512000) // 500KB/s 44 defaultRecvRate = int64(512000) // 500KB/s 45 defaultSendTimeout = 10 * time.Second 46 defaultPingInterval = 60 * time.Second 47 defaultPongTimeout = 45 * time.Second 48 ) 49 50 type receiveCbFunc func(chID byte, msgBytes []byte) 51 type errorCbFunc func(interface{}) 52 53 /* 54 Each peer has one `MConnection` (multiplex connection) instance. 55 56 __multiplex__ *noun* a system or signal involving simultaneous transmission of 57 several messages along a single channel of communication. 58 59 Each `MConnection` handles message transmission on multiple abstract communication 60 `Channel`s. Each channel has a globally unique byte id. 61 The byte id and the relative priorities of each `Channel` are configured upon 62 initialization of the connection. 63 64 There are two methods for sending messages: 65 func (m MConnection) Send(chID byte, msgBytes []byte) bool {} 66 func (m MConnection) TrySend(chID byte, msgBytes []byte}) bool {} 67 68 `Send(chID, msgBytes)` is a blocking call that waits until `msg` is 69 successfully queued for the channel with the given id byte `chID`, or until the 70 request times out. The message `msg` is serialized using Protobuf. 71 72 `TrySend(chID, msgBytes)` is a nonblocking call that returns false if the 73 channel's queue is full. 74 75 Inbound message bytes are handled with an onReceive callback function. 76 */ 77 type MConnection struct { 78 service.BaseService 79 80 conn net.Conn 81 bufConnReader *bufio.Reader 82 bufConnWriter *bufio.Writer 83 sendMonitor *flow.Monitor 84 recvMonitor *flow.Monitor 85 send chan struct{} 86 pong chan struct{} 87 channels []*Channel 88 channelsIdx map[byte]*Channel 89 onReceive receiveCbFunc 90 onError errorCbFunc 91 errored uint32 92 config MConnConfig 93 94 // Closing quitSendRoutine will cause the sendRoutine to eventually quit. 95 // doneSendRoutine is closed when the sendRoutine actually quits. 96 quitSendRoutine chan struct{} 97 doneSendRoutine chan struct{} 98 99 // Closing quitRecvRouting will cause the recvRouting to eventually quit. 100 quitRecvRoutine chan struct{} 101 102 // used to ensure FlushStop and OnStop 103 // are safe to call concurrently. 104 stopMtx tmsync.Mutex 105 106 flushTimer *timer.ThrottleTimer // flush writes as necessary but throttled. 107 pingTimer *time.Ticker // send pings periodically 108 109 // close conn if pong is not received in pongTimeout 110 pongTimer *time.Timer 111 pongTimeoutCh chan bool // true - timeout, false - peer sent pong 112 113 chStatsTimer *time.Ticker // update channel stats periodically 114 115 created time.Time // time of creation 116 117 _maxPacketMsgSize int 118 } 119 120 // MConnConfig is a MConnection configuration. 121 type MConnConfig struct { 122 SendRate int64 `mapstructure:"send_rate"` 123 RecvRate int64 `mapstructure:"recv_rate"` 124 125 // Maximum payload size 126 MaxPacketMsgPayloadSize int `mapstructure:"max_packet_msg_payload_size"` 127 128 // Interval to flush writes (throttled) 129 FlushThrottle time.Duration `mapstructure:"flush_throttle"` 130 131 // Interval to send pings 132 PingInterval time.Duration `mapstructure:"ping_interval"` 133 134 // Maximum wait time for pongs 135 PongTimeout time.Duration `mapstructure:"pong_timeout"` 136 } 137 138 // DefaultMConnConfig returns the default config. 139 func DefaultMConnConfig() MConnConfig { 140 return MConnConfig{ 141 SendRate: defaultSendRate, 142 RecvRate: defaultRecvRate, 143 MaxPacketMsgPayloadSize: defaultMaxPacketMsgPayloadSize, 144 FlushThrottle: defaultFlushThrottle, 145 PingInterval: defaultPingInterval, 146 PongTimeout: defaultPongTimeout, 147 } 148 } 149 150 // NewMConnection wraps net.Conn and creates multiplex connection 151 func NewMConnection( 152 conn net.Conn, 153 chDescs []*ChannelDescriptor, 154 onReceive receiveCbFunc, 155 onError errorCbFunc, 156 ) *MConnection { 157 return NewMConnectionWithConfig( 158 conn, 159 chDescs, 160 onReceive, 161 onError, 162 DefaultMConnConfig()) 163 } 164 165 // NewMConnectionWithConfig wraps net.Conn and creates multiplex connection with a config 166 func NewMConnectionWithConfig( 167 conn net.Conn, 168 chDescs []*ChannelDescriptor, 169 onReceive receiveCbFunc, 170 onError errorCbFunc, 171 config MConnConfig, 172 ) *MConnection { 173 if config.PongTimeout >= config.PingInterval { 174 panic("pongTimeout must be less than pingInterval (otherwise, next ping will reset pong timer)") 175 } 176 177 mconn := &MConnection{ 178 conn: conn, 179 bufConnReader: bufio.NewReaderSize(conn, minReadBufferSize), 180 bufConnWriter: bufio.NewWriterSize(conn, minWriteBufferSize), 181 sendMonitor: flow.New(0, 0), 182 recvMonitor: flow.New(0, 0), 183 send: make(chan struct{}, 1), 184 pong: make(chan struct{}, 1), 185 onReceive: onReceive, 186 onError: onError, 187 config: config, 188 created: time.Now(), 189 } 190 191 // Create channels 192 var channelsIdx = map[byte]*Channel{} 193 var channels = []*Channel{} 194 195 for _, desc := range chDescs { 196 channel := newChannel(mconn, *desc) 197 channelsIdx[channel.desc.ID] = channel 198 channels = append(channels, channel) 199 } 200 mconn.channels = channels 201 mconn.channelsIdx = channelsIdx 202 203 mconn.BaseService = *service.NewBaseService(nil, "MConnection", mconn) 204 205 // maxPacketMsgSize() is a bit heavy, so call just once 206 mconn._maxPacketMsgSize = mconn.maxPacketMsgSize() 207 208 return mconn 209 } 210 211 func (c *MConnection) SetLogger(l log.Logger) { 212 c.BaseService.SetLogger(l) 213 for _, ch := range c.channels { 214 ch.SetLogger(l) 215 } 216 } 217 218 // OnStart implements BaseService 219 func (c *MConnection) OnStart() error { 220 if err := c.BaseService.OnStart(); err != nil { 221 return err 222 } 223 c.flushTimer = timer.NewThrottleTimer("flush", c.config.FlushThrottle) 224 c.pingTimer = time.NewTicker(c.config.PingInterval) 225 c.pongTimeoutCh = make(chan bool, 1) 226 c.chStatsTimer = time.NewTicker(updateStats) 227 c.quitSendRoutine = make(chan struct{}) 228 c.doneSendRoutine = make(chan struct{}) 229 c.quitRecvRoutine = make(chan struct{}) 230 go c.sendRoutine() 231 go c.recvRoutine() 232 return nil 233 } 234 235 // stopServices stops the BaseService and timers and closes the quitSendRoutine. 236 // if the quitSendRoutine was already closed, it returns true, otherwise it returns false. 237 // It uses the stopMtx to ensure only one of FlushStop and OnStop can do this at a time. 238 func (c *MConnection) stopServices() (alreadyStopped bool) { 239 c.stopMtx.Lock() 240 defer c.stopMtx.Unlock() 241 242 select { 243 case <-c.quitSendRoutine: 244 // already quit 245 return true 246 default: 247 } 248 249 select { 250 case <-c.quitRecvRoutine: 251 // already quit 252 return true 253 default: 254 } 255 256 c.BaseService.OnStop() 257 c.flushTimer.Stop() 258 c.pingTimer.Stop() 259 c.chStatsTimer.Stop() 260 261 // inform the recvRouting that we are shutting down 262 close(c.quitRecvRoutine) 263 close(c.quitSendRoutine) 264 return false 265 } 266 267 // FlushStop replicates the logic of OnStop. 268 // It additionally ensures that all successful 269 // .Send() calls will get flushed before closing 270 // the connection. 271 func (c *MConnection) FlushStop() { 272 if c.stopServices() { 273 return 274 } 275 276 // this block is unique to FlushStop 277 { 278 // wait until the sendRoutine exits 279 // so we dont race on calling sendSomePacketMsgs 280 <-c.doneSendRoutine 281 282 // Send and flush all pending msgs. 283 // Since sendRoutine has exited, we can call this 284 // safely 285 eof := c.sendSomePacketMsgs() 286 for !eof { 287 eof = c.sendSomePacketMsgs() 288 } 289 c.flush() 290 291 // Now we can close the connection 292 } 293 294 c.conn.Close() 295 296 // We can't close pong safely here because 297 // recvRoutine may write to it after we've stopped. 298 // Though it doesn't need to get closed at all, 299 // we close it @ recvRoutine. 300 301 // c.Stop() 302 } 303 304 // OnStop implements BaseService 305 func (c *MConnection) OnStop() { 306 if c.stopServices() { 307 return 308 } 309 310 c.conn.Close() 311 312 // We can't close pong safely here because 313 // recvRoutine may write to it after we've stopped. 314 // Though it doesn't need to get closed at all, 315 // we close it @ recvRoutine. 316 } 317 318 func (c *MConnection) String() string { 319 return fmt.Sprintf("MConn{%v}", c.conn.RemoteAddr()) 320 } 321 322 func (c *MConnection) flush() { 323 c.Logger.Debug("Flush", "conn", c) 324 err := c.bufConnWriter.Flush() 325 if err != nil { 326 c.Logger.Debug("MConnection flush failed", "err", err) 327 } 328 } 329 330 // Catch panics, usually caused by remote disconnects. 331 func (c *MConnection) _recover() { 332 if r := recover(); r != nil { 333 c.Logger.Error("MConnection panicked", "err", r, "stack", string(debug.Stack())) 334 c.stopForError(fmt.Errorf("recovered from panic: %v", r)) 335 } 336 } 337 338 func (c *MConnection) stopForError(r interface{}) { 339 if err := c.Stop(); err != nil { 340 c.Logger.Error("Error stopping connection", "err", err) 341 } 342 if atomic.CompareAndSwapUint32(&c.errored, 0, 1) { 343 if c.onError != nil { 344 c.onError(r) 345 } 346 } 347 } 348 349 // Queues a message to be sent to channel. 350 func (c *MConnection) Send(chID byte, msgBytes []byte) bool { 351 if !c.IsRunning() { 352 return false 353 } 354 355 c.Logger.Debug("Send", "channel", chID, "conn", c, "msgBytes", fmt.Sprintf("%X", msgBytes)) 356 357 // Send message to channel. 358 channel, ok := c.channelsIdx[chID] 359 if !ok { 360 c.Logger.Error(fmt.Sprintf("Cannot send bytes, unknown channel %X", chID)) 361 return false 362 } 363 364 success := channel.sendBytes(msgBytes) 365 if success { 366 // Wake up sendRoutine if necessary 367 select { 368 case c.send <- struct{}{}: 369 default: 370 } 371 } else { 372 c.Logger.Debug("Send failed", "channel", chID, "conn", c, "msgBytes", fmt.Sprintf("%X", msgBytes)) 373 } 374 return success 375 } 376 377 // Queues a message to be sent to channel. 378 // Nonblocking, returns true if successful. 379 func (c *MConnection) TrySend(chID byte, msgBytes []byte) bool { 380 if !c.IsRunning() { 381 return false 382 } 383 384 c.Logger.Debug("TrySend", "channel", chID, "conn", c, "msgBytes", fmt.Sprintf("%X", msgBytes)) 385 386 // Send message to channel. 387 channel, ok := c.channelsIdx[chID] 388 if !ok { 389 c.Logger.Error(fmt.Sprintf("Cannot send bytes, unknown channel %X", chID)) 390 return false 391 } 392 393 ok = channel.trySendBytes(msgBytes) 394 if ok { 395 // Wake up sendRoutine if necessary 396 select { 397 case c.send <- struct{}{}: 398 default: 399 } 400 } 401 402 return ok 403 } 404 405 // CanSend returns true if you can send more data onto the chID, false 406 // otherwise. Use only as a heuristic. 407 func (c *MConnection) CanSend(chID byte) bool { 408 if !c.IsRunning() { 409 return false 410 } 411 412 channel, ok := c.channelsIdx[chID] 413 if !ok { 414 c.Logger.Error(fmt.Sprintf("Unknown channel %X", chID)) 415 return false 416 } 417 return channel.canSend() 418 } 419 420 // sendRoutine polls for packets to send from channels. 421 func (c *MConnection) sendRoutine() { 422 defer c._recover() 423 424 protoWriter := protoio.NewDelimitedWriter(c.bufConnWriter) 425 426 FOR_LOOP: 427 for { 428 var _n int 429 var err error 430 SELECTION: 431 select { 432 case <-c.flushTimer.Ch: 433 // NOTE: flushTimer.Set() must be called every time 434 // something is written to .bufConnWriter. 435 c.flush() 436 case <-c.chStatsTimer.C: 437 for _, channel := range c.channels { 438 channel.updateStats() 439 } 440 case <-c.pingTimer.C: 441 c.Logger.Debug("Send Ping") 442 _n, err = protoWriter.WriteMsg(mustWrapPacket(&tmp2p.PacketPing{})) 443 if err != nil { 444 c.Logger.Error("Failed to send PacketPing", "err", err) 445 break SELECTION 446 } 447 c.sendMonitor.Update(_n) 448 c.Logger.Debug("Starting pong timer", "dur", c.config.PongTimeout) 449 c.pongTimer = time.AfterFunc(c.config.PongTimeout, func() { 450 select { 451 case c.pongTimeoutCh <- true: 452 default: 453 } 454 }) 455 c.flush() 456 case timeout := <-c.pongTimeoutCh: 457 if timeout { 458 c.Logger.Debug("Pong timeout") 459 err = errors.New("pong timeout") 460 } else { 461 c.stopPongTimer() 462 } 463 case <-c.pong: 464 c.Logger.Debug("Send Pong") 465 _n, err = protoWriter.WriteMsg(mustWrapPacket(&tmp2p.PacketPong{})) 466 if err != nil { 467 c.Logger.Error("Failed to send PacketPong", "err", err) 468 break SELECTION 469 } 470 c.sendMonitor.Update(_n) 471 c.flush() 472 case <-c.quitSendRoutine: 473 break FOR_LOOP 474 case <-c.send: 475 // Send some PacketMsgs 476 eof := c.sendSomePacketMsgs() 477 if !eof { 478 // Keep sendRoutine awake. 479 select { 480 case c.send <- struct{}{}: 481 default: 482 } 483 } 484 } 485 486 if !c.IsRunning() { 487 break FOR_LOOP 488 } 489 if err != nil { 490 c.Logger.Error("Connection failed @ sendRoutine", "conn", c, "err", err) 491 c.stopForError(err) 492 break FOR_LOOP 493 } 494 } 495 496 // Cleanup 497 c.stopPongTimer() 498 close(c.doneSendRoutine) 499 } 500 501 // Returns true if messages from channels were exhausted. 502 // Blocks in accordance to .sendMonitor throttling. 503 func (c *MConnection) sendSomePacketMsgs() bool { 504 // Block until .sendMonitor says we can write. 505 // Once we're ready we send more than we asked for, 506 // but amortized it should even out. 507 c.sendMonitor.Limit(c._maxPacketMsgSize, atomic.LoadInt64(&c.config.SendRate), true) 508 509 // Now send some PacketMsgs. 510 for i := 0; i < numBatchPacketMsgs; i++ { 511 if c.sendPacketMsg() { 512 return true 513 } 514 } 515 return false 516 } 517 518 // Returns true if messages from channels were exhausted. 519 func (c *MConnection) sendPacketMsg() bool { 520 // Choose a channel to create a PacketMsg from. 521 // The chosen channel will be the one whose recentlySent/priority is the least. 522 var leastRatio float32 = math.MaxFloat32 523 var leastChannel *Channel 524 for _, channel := range c.channels { 525 // If nothing to send, skip this channel 526 if !channel.isSendPending() { 527 continue 528 } 529 // Get ratio, and keep track of lowest ratio. 530 ratio := float32(channel.recentlySent) / float32(channel.desc.Priority) 531 if ratio < leastRatio { 532 leastRatio = ratio 533 leastChannel = channel 534 } 535 } 536 537 // Nothing to send? 538 if leastChannel == nil { 539 return true 540 } 541 // c.Logger.Info("Found a msgPacket to send") 542 543 // Make & send a PacketMsg from this channel 544 _n, err := leastChannel.writePacketMsgTo(c.bufConnWriter) 545 if err != nil { 546 c.Logger.Error("Failed to write PacketMsg", "err", err) 547 c.stopForError(err) 548 return true 549 } 550 c.sendMonitor.Update(_n) 551 c.flushTimer.Set() 552 return false 553 } 554 555 // recvRoutine reads PacketMsgs and reconstructs the message using the channels' "recving" buffer. 556 // After a whole message has been assembled, it's pushed to onReceive(). 557 // Blocks depending on how the connection is throttled. 558 // Otherwise, it never blocks. 559 func (c *MConnection) recvRoutine() { 560 defer c._recover() 561 562 protoReader := protoio.NewDelimitedReader(c.bufConnReader, c._maxPacketMsgSize) 563 564 FOR_LOOP: 565 for { 566 // Block until .recvMonitor says we can read. 567 c.recvMonitor.Limit(c._maxPacketMsgSize, atomic.LoadInt64(&c.config.RecvRate), true) 568 569 // Peek into bufConnReader for debugging 570 /* 571 if numBytes := c.bufConnReader.Buffered(); numBytes > 0 { 572 bz, err := c.bufConnReader.Peek(tmmath.MinInt(numBytes, 100)) 573 if err == nil { 574 // return 575 } else { 576 c.Logger.Debug("Error peeking connection buffer", "err", err) 577 // return nil 578 } 579 c.Logger.Info("Peek connection buffer", "numBytes", numBytes, "bz", bz) 580 } 581 */ 582 583 // Read packet type 584 var packet tmp2p.Packet 585 586 err := protoReader.ReadMsg(&packet) 587 if err != nil { 588 // stopServices was invoked and we are shutting down 589 // receiving is excpected to fail since we will close the connection 590 select { 591 case <-c.quitRecvRoutine: 592 break FOR_LOOP 593 default: 594 } 595 596 if c.IsRunning() { 597 if err == io.EOF { 598 c.Logger.Info("Connection is closed @ recvRoutine (likely by the other side)", "conn", c) 599 } else { 600 c.Logger.Debug("Connection failed @ recvRoutine (reading byte)", "conn", c, "err", err) 601 } 602 c.stopForError(err) 603 } 604 break FOR_LOOP 605 } 606 607 // Read more depending on packet type. 608 switch pkt := packet.Sum.(type) { 609 case *tmp2p.Packet_PacketPing: 610 // TODO: prevent abuse, as they cause flush()'s. 611 // https://github.com/tendermint/tendermint/issues/1190 612 c.Logger.Debug("Receive Ping") 613 select { 614 case c.pong <- struct{}{}: 615 default: 616 // never block 617 } 618 case *tmp2p.Packet_PacketPong: 619 c.Logger.Debug("Receive Pong") 620 select { 621 case c.pongTimeoutCh <- false: 622 default: 623 // never block 624 } 625 case *tmp2p.Packet_PacketMsg: 626 channel, ok := c.channelsIdx[byte(pkt.PacketMsg.ChannelID)] 627 if !ok || channel == nil { 628 err := fmt.Errorf("unknown channel %X", pkt.PacketMsg.ChannelID) 629 c.Logger.Debug("Connection failed @ recvRoutine", "conn", c, "err", err) 630 c.stopForError(err) 631 break FOR_LOOP 632 } 633 634 msgBytes, err := channel.recvPacketMsg(*pkt.PacketMsg) 635 if err != nil { 636 if c.IsRunning() { 637 c.Logger.Debug("Connection failed @ recvRoutine", "conn", c, "err", err) 638 c.stopForError(err) 639 } 640 break FOR_LOOP 641 } 642 if msgBytes != nil { 643 c.Logger.Debug("Received bytes", "chID", pkt.PacketMsg.ChannelID, "msgBytes", fmt.Sprintf("%X", msgBytes)) 644 // NOTE: This means the reactor.Receive runs in the same thread as the p2p recv routine 645 c.onReceive(byte(pkt.PacketMsg.ChannelID), msgBytes) 646 } 647 default: 648 err := fmt.Errorf("unknown message type %v", reflect.TypeOf(packet)) 649 c.Logger.Error("Connection failed @ recvRoutine", "conn", c, "err", err) 650 c.stopForError(err) 651 break FOR_LOOP 652 } 653 } 654 655 // Cleanup 656 close(c.pong) 657 for range c.pong { 658 // Drain 659 } 660 } 661 662 // not goroutine-safe 663 func (c *MConnection) stopPongTimer() { 664 if c.pongTimer != nil { 665 _ = c.pongTimer.Stop() 666 c.pongTimer = nil 667 } 668 } 669 670 // maxPacketMsgSize returns a maximum size of PacketMsg 671 func (c *MConnection) maxPacketMsgSize() int { 672 bz, err := proto.Marshal(mustWrapPacket(&tmp2p.PacketMsg{ 673 ChannelID: 0x01, 674 EOF: true, 675 Data: make([]byte, c.config.MaxPacketMsgPayloadSize), 676 })) 677 if err != nil { 678 panic(err) 679 } 680 return len(bz) 681 } 682 683 type ConnectionStatus struct { 684 Duration time.Duration 685 SendMonitor flow.Status 686 RecvMonitor flow.Status 687 Channels []ChannelStatus 688 } 689 690 type ChannelStatus struct { 691 ID byte 692 SendQueueCapacity int 693 SendQueueSize int 694 Priority int 695 RecentlySent int64 696 } 697 698 func (c *MConnection) Status() ConnectionStatus { 699 var status ConnectionStatus 700 status.Duration = time.Since(c.created) 701 status.SendMonitor = c.sendMonitor.Status() 702 status.RecvMonitor = c.recvMonitor.Status() 703 status.Channels = make([]ChannelStatus, len(c.channels)) 704 for i, channel := range c.channels { 705 status.Channels[i] = ChannelStatus{ 706 ID: channel.desc.ID, 707 SendQueueCapacity: cap(channel.sendQueue), 708 SendQueueSize: int(atomic.LoadInt32(&channel.sendQueueSize)), 709 Priority: channel.desc.Priority, 710 RecentlySent: atomic.LoadInt64(&channel.recentlySent), 711 } 712 } 713 return status 714 } 715 716 //----------------------------------------------------------------------------- 717 718 type ChannelDescriptor struct { 719 ID byte 720 Priority int 721 SendQueueCapacity int 722 RecvBufferCapacity int 723 RecvMessageCapacity int 724 } 725 726 func (chDesc ChannelDescriptor) FillDefaults() (filled ChannelDescriptor) { 727 if chDesc.SendQueueCapacity == 0 { 728 chDesc.SendQueueCapacity = defaultSendQueueCapacity 729 } 730 if chDesc.RecvBufferCapacity == 0 { 731 chDesc.RecvBufferCapacity = defaultRecvBufferCapacity 732 } 733 if chDesc.RecvMessageCapacity == 0 { 734 chDesc.RecvMessageCapacity = defaultRecvMessageCapacity 735 } 736 filled = chDesc 737 return 738 } 739 740 // TODO: lowercase. 741 // NOTE: not goroutine-safe. 742 type Channel struct { 743 conn *MConnection 744 desc ChannelDescriptor 745 sendQueue chan []byte 746 sendQueueSize int32 // atomic. 747 recving []byte 748 sending []byte 749 recentlySent int64 // exponential moving average 750 751 maxPacketMsgPayloadSize int 752 753 Logger log.Logger 754 } 755 756 func newChannel(conn *MConnection, desc ChannelDescriptor) *Channel { 757 desc = desc.FillDefaults() 758 if desc.Priority <= 0 { 759 panic("Channel default priority must be a positive integer") 760 } 761 return &Channel{ 762 conn: conn, 763 desc: desc, 764 sendQueue: make(chan []byte, desc.SendQueueCapacity), 765 recving: make([]byte, 0, desc.RecvBufferCapacity), 766 maxPacketMsgPayloadSize: conn.config.MaxPacketMsgPayloadSize, 767 } 768 } 769 770 func (ch *Channel) SetLogger(l log.Logger) { 771 ch.Logger = l 772 } 773 774 // Queues message to send to this channel. 775 // Goroutine-safe 776 // Times out (and returns false) after defaultSendTimeout 777 func (ch *Channel) sendBytes(bytes []byte) bool { 778 select { 779 case ch.sendQueue <- bytes: 780 atomic.AddInt32(&ch.sendQueueSize, 1) 781 return true 782 case <-time.After(defaultSendTimeout): 783 return false 784 } 785 } 786 787 // Queues message to send to this channel. 788 // Nonblocking, returns true if successful. 789 // Goroutine-safe 790 func (ch *Channel) trySendBytes(bytes []byte) bool { 791 select { 792 case ch.sendQueue <- bytes: 793 atomic.AddInt32(&ch.sendQueueSize, 1) 794 return true 795 default: 796 return false 797 } 798 } 799 800 // Goroutine-safe 801 func (ch *Channel) loadSendQueueSize() (size int) { 802 return int(atomic.LoadInt32(&ch.sendQueueSize)) 803 } 804 805 // Goroutine-safe 806 // Use only as a heuristic. 807 func (ch *Channel) canSend() bool { 808 return ch.loadSendQueueSize() < defaultSendQueueCapacity 809 } 810 811 // Returns true if any PacketMsgs are pending to be sent. 812 // Call before calling nextPacketMsg() 813 // Goroutine-safe 814 func (ch *Channel) isSendPending() bool { 815 if len(ch.sending) == 0 { 816 if len(ch.sendQueue) == 0 { 817 return false 818 } 819 ch.sending = <-ch.sendQueue 820 } 821 return true 822 } 823 824 // Creates a new PacketMsg to send. 825 // Not goroutine-safe 826 func (ch *Channel) nextPacketMsg() tmp2p.PacketMsg { 827 packet := tmp2p.PacketMsg{ChannelID: int32(ch.desc.ID)} 828 maxSize := ch.maxPacketMsgPayloadSize 829 packet.Data = ch.sending[:tmmath.MinInt(maxSize, len(ch.sending))] 830 if len(ch.sending) <= maxSize { 831 packet.EOF = true 832 ch.sending = nil 833 atomic.AddInt32(&ch.sendQueueSize, -1) // decrement sendQueueSize 834 } else { 835 packet.EOF = false 836 ch.sending = ch.sending[tmmath.MinInt(maxSize, len(ch.sending)):] 837 } 838 return packet 839 } 840 841 // Writes next PacketMsg to w and updates c.recentlySent. 842 // Not goroutine-safe 843 func (ch *Channel) writePacketMsgTo(w io.Writer) (n int, err error) { 844 packet := ch.nextPacketMsg() 845 n, err = protoio.NewDelimitedWriter(w).WriteMsg(mustWrapPacket(&packet)) 846 atomic.AddInt64(&ch.recentlySent, int64(n)) 847 return 848 } 849 850 // Handles incoming PacketMsgs. It returns a message bytes if message is 851 // complete. NOTE message bytes may change on next call to recvPacketMsg. 852 // Not goroutine-safe 853 func (ch *Channel) recvPacketMsg(packet tmp2p.PacketMsg) ([]byte, error) { 854 ch.Logger.Debug("Read PacketMsg", "conn", ch.conn, "packet", packet) 855 var recvCap, recvReceived = ch.desc.RecvMessageCapacity, len(ch.recving) + len(packet.Data) 856 if recvCap < recvReceived { 857 return nil, fmt.Errorf("received message exceeds available capacity: %v < %v", recvCap, recvReceived) 858 } 859 ch.recving = append(ch.recving, packet.Data...) 860 if packet.EOF { 861 msgBytes := ch.recving 862 863 // clear the slice without re-allocating. 864 // http://stackoverflow.com/questions/16971741/how-do-you-clear-a-slice-in-go 865 // suggests this could be a memory leak, but we might as well keep the memory for the channel until it closes, 866 // at which point the recving slice stops being used and should be garbage collected 867 ch.recving = ch.recving[:0] // make([]byte, 0, ch.desc.RecvBufferCapacity) 868 return msgBytes, nil 869 } 870 return nil, nil 871 } 872 873 // Call this periodically to update stats for throttling purposes. 874 // Not goroutine-safe 875 func (ch *Channel) updateStats() { 876 // Exponential decay of stats. 877 // TODO: optimize. 878 atomic.StoreInt64(&ch.recentlySent, int64(float64(atomic.LoadInt64(&ch.recentlySent))*0.8)) 879 } 880 881 //---------------------------------------- 882 // Packet 883 884 // mustWrapPacket takes a packet kind (oneof) and wraps it in a tmp2p.Packet message. 885 func mustWrapPacket(pb proto.Message) *tmp2p.Packet { 886 var msg tmp2p.Packet 887 888 switch pb := pb.(type) { 889 case *tmp2p.Packet: // already a packet 890 msg = *pb 891 case *tmp2p.PacketPing: 892 msg = tmp2p.Packet{ 893 Sum: &tmp2p.Packet_PacketPing{ 894 PacketPing: pb, 895 }, 896 } 897 case *tmp2p.PacketPong: 898 msg = tmp2p.Packet{ 899 Sum: &tmp2p.Packet_PacketPong{ 900 PacketPong: pb, 901 }, 902 } 903 case *tmp2p.PacketMsg: 904 msg = tmp2p.Packet{ 905 Sum: &tmp2p.Packet_PacketMsg{ 906 PacketMsg: pb, 907 }, 908 } 909 default: 910 panic(fmt.Errorf("unknown packet type %T", pb)) 911 } 912 913 return &msg 914 }