Skip to content

Commit e175825

Browse files
andreykuchinjirfag
authored andcommitted
fix possible race in line cache
1 parent c5b0f95 commit e175825

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

pkg/fsutils/linecache.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package fsutils
33
import (
44
"bytes"
55
"fmt"
6+
"sync"
67

78
"github.com/pkg/errors"
89
)
910

1011
type fileLinesCache [][]byte
1112

1213
type LineCache struct {
13-
files map[string]fileLinesCache
14+
files sync.Map
1415
fileCache *FileCache
1516
}
1617

1718
func NewLineCache(fc *FileCache) *LineCache {
1819
return &LineCache{
19-
files: map[string]fileLinesCache{},
2020
fileCache: fc,
2121
}
2222
}
@@ -53,17 +53,17 @@ func (lc *LineCache) getRawLine(filePath string, index0 int) ([]byte, error) {
5353
}
5454

5555
func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) {
56-
fc := lc.files[filePath]
57-
if fc != nil {
58-
return fc, nil
56+
loadedFc, ok := lc.files.Load(filePath)
57+
if ok {
58+
return loadedFc.(fileLinesCache), nil
5959
}
6060

6161
fileBytes, err := lc.fileCache.GetFileBytes(filePath)
6262
if err != nil {
6363
return nil, errors.Wrapf(err, "can't get file %s bytes from cache", filePath)
6464
}
6565

66-
fc = bytes.Split(fileBytes, []byte("\n"))
67-
lc.files[filePath] = fc
66+
fc := bytes.Split(fileBytes, []byte("\n"))
67+
lc.files.Store(filePath, fileLinesCache(fc))
6868
return fc, nil
6969
}

0 commit comments

Comments
 (0)