Skip to content

Commit

Permalink
Fix an alignment bug causing us to send very small packets
Browse files Browse the repository at this point in the history
The faulty math in block_write_stream was causing us to always send
512 byte packets.
  • Loading branch information
adamfaulkner authored and colinmarc committed Aug 22, 2017
1 parent d961456 commit cdda132
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions rpc/block_write_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ func (s *blockWriteStream) makePacket() outboundPacket {
// gets unhappy unless we first align to a chunk boundary with a small packet.
// Otherwise it yells at us with "a partial chunk must be sent in an
// individual packet" or just complains about a corrupted block.
alignment := outboundChunkSize - (int(s.offset) % outboundChunkSize)
if alignment > 0 && alignment < packetLength {
packetLength = alignment
alignment := int(s.offset) % outboundChunkSize
if alignment > 0 && packetLength > (outboundChunkSize-alignment) {
packetLength = outboundChunkSize - alignment
}

numChunks := int(math.Ceil(float64(packetLength) / float64(outboundChunkSize)))
Expand Down
26 changes: 26 additions & 0 deletions rpc/block_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,29 @@ func TestWriteFailsOver(t *testing.T) {
assert.EqualValues(t, 1048576, n)
assert.EqualValues(t, 0xb35a6a0e, hash.Sum32())
}

func TestPacketSize(t *testing.T) {
bws := &blockWriteStream{}
bws.buf.Write(make([]byte, outboundPacketSize*3))
packet := bws.makePacket()

assert.EqualValues(t, outboundPacketSize, len(packet.data))
}

func TestPacketSizeUndersize(t *testing.T) {
bws := &blockWriteStream{}
bws.buf.Write(make([]byte, outboundPacketSize-5))
packet := bws.makePacket()

assert.EqualValues(t, outboundPacketSize-5, len(packet.data))
}

func TestPacketSizeAlignment(t *testing.T) {
bws := &blockWriteStream{}
bws.buf.Write(make([]byte, outboundPacketSize*3))

bws.offset = 5
packet := bws.makePacket()

assert.EqualValues(t, outboundChunkSize-5, len(packet.data))
}

0 comments on commit cdda132

Please sign in to comment.