Skip to content

Commit

Permalink
Fix for sliplined acks. (#5) Memory pooling tracing and debugging. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed May 28, 2020
1 parent 64c50cd commit 0e46c56
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
6 changes: 3 additions & 3 deletions protocol/blaster/pb/wire.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocol/westworld/txwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (self *txWindow) txer() {
select {
case ackSequence := <-self.ackQueue:
if err := wm.RewriteAck(ackSequence); err == nil {
logrus.Warnf("sliplined ack {@%d}", ackSequence)
//logrus.Warnf("sliplined ack {@%d}", ackSequence)
} else {
logrus.Errorf("error rewriting ack (%v)", err)
}
Expand Down
9 changes: 8 additions & 1 deletion protocol/westworld/wb/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net"
"runtime"
)

type Type uint8
Expand Down Expand Up @@ -36,6 +37,7 @@ func NewHello(sequence int32, pool *BufferPool) (*WireMessage, error) {
buffer: pool.Get().([]byte),
pool: pool,
}
runtime.SetFinalizer(wm, finalizer)
return wm.encode()
}

Expand Down Expand Up @@ -163,7 +165,7 @@ func (self *WireMessage) WriteMessage(conn *net.UDPConn, peer *net.UDPAddr) erro

func (self *WireMessage) RewriteAck(ack int32) error {
buffer := util.NewByteWriter(self.buffer[5:9])
if err := binary.Write(buffer, binary.LittleEndian, self.Ack); err != nil {
if err := binary.Write(buffer, binary.LittleEndian, ack); err != nil {
return err
}
self.Ack = ack
Expand All @@ -175,7 +177,12 @@ func (self *WireMessage) Free() {
self.pool.Put(self.buffer)
self.pool = nil
self.buffer = nil
//runtime.GC()
} else {
logrus.Warnf("double-free")
}
}

func finalizer(wm *WireMessage) {
logrus.Errorf("finalizing [%d] [%p]", wm.Sequence, wm)
}
36 changes: 36 additions & 0 deletions protocol/westworld/wb/message_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package wb

import (
"encoding/binary"
"github.com/michaelquigley/dilithium/util"
"github.com/stretchr/testify/assert"
"testing"
)
Expand All @@ -22,3 +24,37 @@ func TestReadWrite(t *testing.T) {
assert.Equal(t, wm.Ack, wm2.Ack)
assert.Equal(t, wm.Data, wm2.Data)
}

func TestRewriteAck(t *testing.T) {
pool := NewBufferPool("test")
data := make([]byte, 128)
for i := 0; i < len(data); i++ {
data[i] = uint8(i)
}

ackData := make([]byte, 4)
writer := util.NewByteWriter(ackData)
err := binary.Write(writer, binary.LittleEndian, int32(-1))
assert.Nil(t, err)

wm, err := NewData(1, data, pool)
assert.Nil(t, err)
assert.Equal(t, ackData, wm.buffer[5:9])

err = wm.RewriteAck(99)
assert.Nil(t, err)
assert.Equal(t, int32(99), wm.Ack)

writer = util.NewByteWriter(ackData)
err = binary.Write(writer, binary.LittleEndian, int32(99))
assert.Nil(t, err)

assert.Equal(t, ackData, wm.buffer[5:9])

wm2, err := FromBuffer(wm.buffer, pool)
assert.Nil(t, err)
assert.Equal(t, wm.Sequence, wm2.Sequence)
assert.Equal(t, wm.Type, wm2.Type)
assert.Equal(t, wm.Ack, wm2.Ack)
assert.Equal(t, wm.Data, wm2.Data)
}
19 changes: 17 additions & 2 deletions util/bytewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package util

import (
"github.com/stretchr/testify/assert"
"log"
"testing"
)

func TestMultipleWrites(t *testing.T) {
s0 := []byte{ 0x01, 0x02, 0x03, 0x04 }
s1 := []byte{ 0x0a, 0x0b, 0x0c, 0x0d }
s0 := []byte{0x01, 0x02, 0x03, 0x04}
s1 := []byte{0x0a, 0x0b, 0x0c, 0x0d}
trg := make([]byte, 8)
bw := NewByteWriter(trg)

Expand All @@ -22,3 +23,17 @@ func TestMultipleWrites(t *testing.T) {
assert.Equal(t, s0, trg[0:4])
assert.Equal(t, s1, trg[4:8])
}

func TestOverwrite(t *testing.T) {
s0 := []byte{0x01, 0x02, 0x03, 0x04}
log.Printf("s0 = %v\n", s0)

bw := NewByteWriter(s0[1:3])
n, err := bw.Write([]byte{0xff, 0xfe})
assert.Nil(t, err)
assert.Equal(t, 2, n)
assert.Equal(t, uint8(0xff), s0[1])
assert.Equal(t, uint8(0xfe), s0[2])

log.Printf("s0 = %v\n", s0)
}

0 comments on commit 0e46c56

Please sign in to comment.