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
Expand Up @@ -6,8 +6,6 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"

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


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


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

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


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


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


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

0 comments on commit 2620523

Please sign in to comment.