@ -81,8 +81,8 @@ type MConnection struct {
recvMonitor * flow . Monitor
send chan struct { }
pong chan struct { }
channels [ ] * C hannel
channelsIdx map [ ChannelID ] * C hannel
channels [ ] * c hannel
channelsIdx map [ ChannelID ] * c hannel
onReceive receiveCbFunc
onError errorCbFunc
errored uint32
@ -186,8 +186,8 @@ func NewMConnectionWithConfig(
}
// Create channels
var channelsIdx = map [ ChannelID ] * C hannel{ }
var channels = [ ] * C hannel{ }
var channelsIdx = map [ ChannelID ] * c hannel{ }
var channels = [ ] * c hannel{ }
for _ , desc := range chDescs {
channel := newChannel ( mconn , * desc )
@ -436,7 +436,7 @@ func (c *MConnection) sendPacketMsg() bool {
// Choose a channel to create a PacketMsg from.
// The chosen channel will be the one whose recentlySent/priority is the least.
var leastRatio float32 = math . MaxFloat32
var leastChannel * C hannel
var leastChannel * c hannel
for _ , channel := range c . channels {
// If nothing to send, skip this channel
if ! channel . isSendPending ( ) {
@ -639,9 +639,8 @@ func (chDesc ChannelDescriptor) FillDefaults() (filled ChannelDescriptor) {
return
}
// TODO: lowercase.
// NOTE: not goroutine-safe.
type C hannel struct {
type c hannel struct {
// Exponential moving average.
// This field must be accessed atomically.
// It is first in the struct to ensure correct alignment.
@ -660,12 +659,12 @@ type Channel struct {
Logger log . Logger
}
func newChannel ( conn * MConnection , desc ChannelDescriptor ) * C hannel {
func newChannel ( conn * MConnection , desc ChannelDescriptor ) * c hannel {
desc = desc . FillDefaults ( )
if desc . Priority <= 0 {
panic ( "Channel default priority must be a positive integer" )
}
return & C hannel{
return & c hannel{
conn : conn ,
desc : desc ,
sendQueue : make ( chan [ ] byte , desc . SendQueueCapacity ) ,
@ -674,14 +673,14 @@ func newChannel(conn *MConnection, desc ChannelDescriptor) *Channel {
}
}
func ( ch * C hannel) SetLogger ( l log . Logger ) {
func ( ch * c hannel) SetLogger ( l log . Logger ) {
ch . Logger = l
}
// Queues message to send to this channel.
// Goroutine-safe
// Times out (and returns false) after defaultSendTimeout
func ( ch * C hannel) sendBytes ( bytes [ ] byte ) bool {
func ( ch * c hannel) sendBytes ( bytes [ ] byte ) bool {
select {
case ch . sendQueue <- bytes :
atomic . AddInt32 ( & ch . sendQueueSize , 1 )
@ -694,7 +693,7 @@ func (ch *Channel) sendBytes(bytes []byte) bool {
// Returns true if any PacketMsgs are pending to be sent.
// Call before calling nextPacketMsg()
// Goroutine-safe
func ( ch * C hannel) isSendPending ( ) bool {
func ( ch * c hannel) isSendPending ( ) bool {
if len ( ch . sending ) == 0 {
if len ( ch . sendQueue ) == 0 {
return false
@ -706,7 +705,7 @@ func (ch *Channel) isSendPending() bool {
// Creates a new PacketMsg to send.
// Not goroutine-safe
func ( ch * C hannel) nextPacketMsg ( ) tmp2p . PacketMsg {
func ( ch * c hannel) nextPacketMsg ( ) tmp2p . PacketMsg {
packet := tmp2p . PacketMsg { ChannelID : int32 ( ch . desc . ID ) }
maxSize := ch . maxPacketMsgPayloadSize
packet . Data = ch . sending [ : tmmath . MinInt ( maxSize , len ( ch . sending ) ) ]
@ -723,7 +722,7 @@ func (ch *Channel) nextPacketMsg() tmp2p.PacketMsg {
// Writes next PacketMsg to w and updates c.recentlySent.
// Not goroutine-safe
func ( ch * C hannel) writePacketMsgTo ( w io . Writer ) ( n int , err error ) {
func ( ch * c hannel) writePacketMsgTo ( w io . Writer ) ( n int , err error ) {
packet := ch . nextPacketMsg ( )
n , err = protoio . NewDelimitedWriter ( w ) . WriteMsg ( mustWrapPacket ( & packet ) )
atomic . AddInt64 ( & ch . recentlySent , int64 ( n ) )
@ -733,7 +732,7 @@ func (ch *Channel) writePacketMsgTo(w io.Writer) (n int, err error) {
// Handles incoming PacketMsgs. It returns a message bytes if message is
// complete, which is owned by the caller and will not be modified.
// Not goroutine-safe
func ( ch * C hannel) recvPacketMsg ( packet tmp2p . PacketMsg ) ( [ ] byte , error ) {
func ( ch * c hannel) recvPacketMsg ( packet tmp2p . PacketMsg ) ( [ ] byte , error ) {
ch . Logger . Debug ( "Read PacketMsg" , "conn" , ch . conn , "packet" , packet )
var recvCap , recvReceived = ch . desc . RecvMessageCapacity , len ( ch . recving ) + len ( packet . Data )
if recvCap < recvReceived {
@ -750,7 +749,7 @@ func (ch *Channel) recvPacketMsg(packet tmp2p.PacketMsg) ([]byte, error) {
// Call this periodically to update stats for throttling purposes.
// Not goroutine-safe
func ( ch * C hannel) updateStats ( ) {
func ( ch * c hannel) updateStats ( ) {
// Exponential decay of stats.
// TODO: optimize.
atomic . StoreInt64 ( & ch . recentlySent , int64 ( float64 ( atomic . LoadInt64 ( & ch . recentlySent ) ) * 0.8 ) )