Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on the pool put/get operations #54

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions adapter/socketclient/socketclient.go
Expand Up @@ -375,12 +375,15 @@ func (c *Client) writeMsg(msg []byte) error {
c.writeMu.Lock()
defer c.writeMu.Unlock()

header := c.headerPool.Get().(*[]byte)
header, ok := c.headerPool.Get().(*[]byte)
if !ok {
return fmt.Errorf("failed to get header from pool")
}
err := writeMsgHeader(c.writer, *header, len(msg))
if err != nil {
return err
}
c.headerPool.Put(&header)
c.headerPool.Put(header)

if err := writeMsgData(c.writer, msg, c.writer.Size()); err != nil {
return err
Expand Down Expand Up @@ -498,12 +501,15 @@ func (c *Client) readMsgTimeout(buf []byte, timeout time.Duration) ([]byte, erro
func (c *Client) readMsg(buf []byte) ([]byte, error) {
log.Debug("reading msg..")

header := c.headerPool.Get().(*[]byte)
header, ok := c.headerPool.Get().(*[]byte)
if !ok {
return nil, fmt.Errorf("failed to get header from pool")
}
msgLen, err := readMsgHeader(c.reader, *header)
if err != nil {
return nil, err
}
c.headerPool.Put(&header)
c.headerPool.Put(header)

msg, err := readMsgData(c.reader, buf, msgLen)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/channel_test.go
Expand Up @@ -190,7 +190,7 @@ func TestSetReplyTimeout(t *testing.T) {
ctx := setupTest(t)
defer ctx.teardownTest()

ctx.ch.SetReplyTimeout(time.Millisecond)
ctx.ch.SetReplyTimeout(time.Millisecond * 10)

// mock reply
ctx.mockVpp.MockReply(&ControlPingReply{})
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestReceiveReplyAfterTimeout(t *testing.T) {
ctx := setupTest(t)
defer ctx.teardownTest()

ctx.ch.SetReplyTimeout(time.Millisecond)
ctx.ch.SetReplyTimeout(time.Millisecond * 10)

// mock reply
ctx.mockVpp.MockReplyWithContext(mock.MsgWithContext{Msg: &ControlPingReply{}, SeqNum: 1})
Expand Down