Skip to content

Commit

Permalink
Fix encoder of messagepack to keep in pool
Browse files Browse the repository at this point in the history
  • Loading branch information
daichirata committed Dec 15, 2017
1 parent d0cdf96 commit 2620523
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 1 addition & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"strings"
"sync"
"time"

"gopkg.in/vmihailenco/msgpack.v2"
)

var (
Expand Down Expand Up @@ -65,8 +63,7 @@ func (logger *Logger) PostWithTime(tag string, t time.Time, obj interface{}) err
}

m := getMessage()
enc := msgpack.NewEncoder(m.buf)
if err := enc.Encode(record); err != nil {
if err := m.enc.Encode(record); err != nil {
return err
}
logger.buf.Add(m)
Expand Down
7 changes: 6 additions & 1 deletion message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package fluent
import (
"bytes"
"sync"

"gopkg.in/vmihailenco/msgpack.v2"
)

type Message struct {
buf *bytes.Buffer
enc *msgpack.Encoder
}

var messagePool = sync.Pool{
Expand All @@ -26,7 +29,9 @@ func putMessage(message *Message) {
}

func newMessage() *Message {
buf := bytes.NewBuffer([]byte{})
return &Message{
buf: bytes.NewBuffer([]byte{}),
buf: buf,
enc: msgpack.NewEncoder(buf),
}
}

0 comments on commit 2620523

Please sign in to comment.