Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Fix race in writeHash
Browse files Browse the repository at this point in the history
Signed-off-by: Ganesh Vernekar <cs15btech11018@iith.ac.in>
  • Loading branch information
codesome committed Jun 18, 2019
1 parent f344fa2 commit f2813b4
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions chunks/chunks.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"os"
"path/filepath"
"strconv"
"sync"

"github.com/pkg/errors"
"github.com/prometheus/tsdb/chunkenc"
Expand Down Expand Up @@ -55,14 +56,20 @@ type Meta struct {
}

// metaWriteHashBuf is the byte slice buffer used to write the hash.
var metaWriteHashBuf = make([]byte, 1)
var metaWriteHashBufPool = &sync.Pool{
New: func() interface{} {
return make([]byte, 1)
},
}

// writeHash writes the chunk encoding and raw data into the provided hash.
func (cm *Meta) writeHash(h hash.Hash) error {
metaWriteHashBuf[0] = byte(cm.Chunk.Encoding())
if _, err := h.Write(metaWriteHashBuf); err != nil {
buf := metaWriteHashBufPool.Get().([]byte)
buf[0] = byte(cm.Chunk.Encoding())
if _, err := h.Write(buf); err != nil {
return err
}
metaWriteHashBufPool.Put(buf)
if _, err := h.Write(cm.Chunk.Bytes()); err != nil {
return err
}
Expand Down

0 comments on commit f2813b4

Please sign in to comment.