don't reallocate on recvMsgPacket

This commit is contained in:
Ethan Buchman 2016-03-12 01:04:27 -05:00
parent 10619248c6
commit 114d90bec8
1 changed files with 6 additions and 2 deletions

View File

@ -18,7 +18,7 @@ import (
const ( const (
numBatchMsgPackets = 10 numBatchMsgPackets = 10
minReadBufferSize = 1024 minReadBufferSize = 1024
minWriteBufferSize = 1024 minWriteBufferSize = 65536
idleTimeoutMinutes = 5 idleTimeoutMinutes = 5
updateStatsSeconds = 2 updateStatsSeconds = 2
pingTimeoutSeconds = 40 pingTimeoutSeconds = 40
@ -634,7 +634,11 @@ func (ch *Channel) recvMsgPacket(packet msgPacket) ([]byte, error) {
ch.recving = append(ch.recving, packet.Bytes...) ch.recving = append(ch.recving, packet.Bytes...)
if packet.EOF == byte(0x01) { if packet.EOF == byte(0x01) {
msgBytes := ch.recving msgBytes := ch.recving
ch.recving = make([]byte, 0, defaultRecvBufferCapacity) // clear the slice without re-allocating.
// http://stackoverflow.com/questions/16971741/how-do-you-clear-a-slice-in-go
// suggests this could be a memory leak, but we might as well keep the memory for the channel until it closes,
// at which point the recving slice stops being used and should be garbage collected
ch.recving = ch.recving[:0] // make([]byte, 0, ch.desc.RecvBufferCapacity)
return msgBytes, nil return msgBytes, nil
} }
return nil, nil return nil, nil