tendermint/peer/connection.go

192 lines
4.7 KiB
Go
Raw Normal View History

2014-06-18 20:48:32 -07:00
package peer
import (
. "github.com/tendermint/tendermint/common"
. "github.com/tendermint/tendermint/binary"
2014-06-25 21:37:20 -07:00
"sync/atomic"
2014-06-18 20:48:32 -07:00
"net"
"time"
2014-06-29 00:35:16 -07:00
"fmt"
2014-06-18 20:48:32 -07:00
)
const (
OUT_QUEUE_SIZE = 50
IDLE_TIMEOUT_MINUTES = 5
PING_TIMEOUT_MINUTES = 2
)
/* Connnection */
type Connection struct {
ioStats IOStats
sendQueue chan Packet // never closes
2014-06-18 20:48:32 -07:00
conn net.Conn
quit chan struct{}
2014-06-25 21:37:20 -07:00
stopped uint32
2014-06-18 20:48:32 -07:00
pingDebouncer *Debouncer
pong chan struct{}
}
var (
PACKET_TYPE_PING = UInt8(0x00)
PACKET_TYPE_PONG = UInt8(0x01)
PACKET_TYPE_MSG = UInt8(0x10)
)
func NewConnection(conn net.Conn) *Connection {
return &Connection{
sendQueue: make(chan Packet, OUT_QUEUE_SIZE),
2014-06-18 20:48:32 -07:00
conn: conn,
quit: make(chan struct{}),
pingDebouncer: NewDebouncer(PING_TIMEOUT_MINUTES * time.Minute),
pong: make(chan struct{}),
}
}
2014-06-24 17:28:40 -07:00
// returns true if successfully queued,
// returns false if connection was closed.
// blocks.
func (c *Connection) Send(pkt Packet) bool {
2014-06-18 20:48:32 -07:00
select {
case c.sendQueue <- pkt:
2014-06-18 20:48:32 -07:00
return true
2014-06-24 17:28:40 -07:00
case <-c.quit:
2014-06-18 20:48:32 -07:00
return false
}
}
func (c *Connection) Start(channels map[String]*Channel) {
log.Debugf("Starting %v", c)
go c.sendHandler()
go c.recvHandler(channels)
2014-06-18 20:48:32 -07:00
}
2014-06-24 17:28:40 -07:00
func (c *Connection) Stop() {
2014-06-25 21:37:20 -07:00
if atomic.CompareAndSwapUint32(&c.stopped, 0, 1) {
log.Debugf("Stopping %v", c)
2014-06-24 17:28:40 -07:00
close(c.quit)
c.conn.Close()
c.pingDebouncer.Stop()
// We can't close pong safely here because
// recvHandler may write to it after we've stopped.
2014-06-24 17:28:40 -07:00
// Though it doesn't need to get closed at all,
// we close it @ recvHandler.
2014-06-24 17:28:40 -07:00
// close(c.pong)
}
}
func (c *Connection) LocalAddress() *NetAddress {
return NewNetAddress(c.conn.LocalAddr())
}
func (c *Connection) RemoteAddress() *NetAddress {
return NewNetAddress(c.conn.RemoteAddr())
2014-06-18 20:48:32 -07:00
}
2014-06-29 00:35:16 -07:00
func (c *Connection) String() string {
return fmt.Sprintf("Connection{%v}", c.conn.RemoteAddr())
}
2014-06-18 20:48:32 -07:00
func (c *Connection) flush() {
// TODO flush? (turn off nagel, turn back on, etc)
}
func (c *Connection) sendHandler() {
log.Tracef("%v sendHandler", c)
// TODO: catch panics & stop connection.
2014-06-18 20:48:32 -07:00
FOR_LOOP:
for {
2014-06-24 17:28:40 -07:00
var err error
2014-06-18 20:48:32 -07:00
select {
case <-c.pingDebouncer.Ch:
2014-06-24 17:28:40 -07:00
_, err = PACKET_TYPE_PING.WriteTo(c.conn)
case sendPkt := <-c.sendQueue:
log.Tracef("Found pkt from sendQueue. Writing pkt to underlying connection")
_, err = PACKET_TYPE_MSG.WriteTo(c.conn)
if err != nil { break }
_, err = sendPkt.WriteTo(c.conn)
2014-06-18 20:48:32 -07:00
case <-c.pong:
2014-06-24 17:28:40 -07:00
_, err = PACKET_TYPE_PONG.WriteTo(c.conn)
2014-06-18 20:48:32 -07:00
case <-c.quit:
break FOR_LOOP
}
2014-06-24 17:28:40 -07:00
if err != nil {
log.Infof("%v failed @ sendHandler:\n%v", c, err)
2014-06-24 17:28:40 -07:00
c.Stop()
break FOR_LOOP
}
2014-06-18 20:48:32 -07:00
c.flush()
}
log.Tracef("%v sendHandler done", c)
// cleanup
2014-06-18 20:48:32 -07:00
}
func (c *Connection) recvHandler(channels map[String]*Channel) {
log.Tracef("%v recvHandler with %v channels", c, len(channels))
// TODO: catch panics & stop connection.
2014-06-18 20:48:32 -07:00
2014-06-24 17:28:40 -07:00
FOR_LOOP:
2014-06-18 20:48:32 -07:00
for {
pktType, err := ReadUInt8Safe(c.conn)
2014-06-24 17:28:40 -07:00
if err != nil {
if atomic.LoadUint32(&c.stopped) != 1 {
log.Infof("%v failed @ recvHandler", c)
2014-06-24 17:28:40 -07:00
c.Stop()
}
break FOR_LOOP
} else {
log.Tracef("Found pktType %v", pktType)
2014-06-24 17:28:40 -07:00
}
2014-06-18 20:48:32 -07:00
switch pktType {
2014-06-18 20:48:32 -07:00
case PACKET_TYPE_PING:
c.pong <- struct{}{}
case PACKET_TYPE_PONG:
// do nothing
case PACKET_TYPE_MSG:
pkt, err := ReadPacketSafe(c.conn)
2014-06-24 17:28:40 -07:00
if err != nil {
if atomic.LoadUint32(&c.stopped) != 1 {
log.Infof("%v failed @ recvHandler", c)
2014-06-24 17:28:40 -07:00
c.Stop()
}
break FOR_LOOP
}
channel := channels[pkt.Channel]
if channel == nil {
Panicf("Unknown channel %v", pkt.Channel)
}
channel.recvQueue <- pkt
2014-06-18 20:48:32 -07:00
default:
Panicf("Unknown message type %v", pktType)
2014-06-18 20:48:32 -07:00
}
2014-06-24 17:28:40 -07:00
2014-06-18 20:48:32 -07:00
c.pingDebouncer.Reset()
}
2014-06-24 17:28:40 -07:00
log.Tracef("%v recvHandler done", c)
2014-06-24 17:28:40 -07:00
// cleanup
close(c.pong)
for _ = range c.pong {
// drain
}
2014-06-18 20:48:32 -07:00
}
/* IOStats */
type IOStats struct {
TimeConnected Time
LastSent Time
LastRecv Time
BytesRecv UInt64
BytesSent UInt64
PktsRecv UInt64
PktsSent UInt64
2014-06-18 20:48:32 -07:00
}