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

Commit 9fab207

Browse files
author
Ganesh Vernekar
committed
Fix race in writeHash
Signed-off-by: Ganesh Vernekar <[email protected]>
1 parent f344fa2 commit 9fab207

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

chunks/chunks.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"path/filepath"
2626
"strconv"
27+
"sync"
2728

2829
"github.com/pkg/errors"
2930
"github.com/prometheus/tsdb/chunkenc"
@@ -55,12 +56,20 @@ type Meta struct {
5556
}
5657

5758
// metaWriteHashBuf is the byte slice buffer used to write the hash.
58-
var metaWriteHashBuf = make([]byte, 1)
59+
var metaWriteHashBufPool = &sync.Pool{
60+
New: func() interface{} {
61+
return make([]byte, 1)
62+
},
63+
}
5964

6065
// writeHash writes the chunk encoding and raw data into the provided hash.
6166
func (cm *Meta) writeHash(h hash.Hash) error {
62-
metaWriteHashBuf[0] = byte(cm.Chunk.Encoding())
63-
if _, err := h.Write(metaWriteHashBuf); err != nil {
67+
buf := metaWriteHashBufPool.Get().([]byte)
68+
defer func() {
69+
metaWriteHashBufPool.Put(buf)
70+
}()
71+
buf[0] = byte(cm.Chunk.Encoding())
72+
if _, err := h.Write(buf); err != nil {
6473
return err
6574
}
6675
if _, err := h.Write(cm.Chunk.Bytes()); err != nil {

0 commit comments

Comments
 (0)