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

Commit f2813b4

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

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

chunks/chunks.go

Lines changed: 10 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,14 +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+
buf[0] = byte(cm.Chunk.Encoding())
69+
if _, err := h.Write(buf); err != nil {
6470
return err
6571
}
72+
metaWriteHashBufPool.Put(buf)
6673
if _, err := h.Write(cm.Chunk.Bytes()); err != nil {
6774
return err
6875
}

0 commit comments

Comments
 (0)