Skip to content

Commit

Permalink
fix possible race in line cache
Browse files Browse the repository at this point in the history
  • Loading branch information
andreykuchin authored and jirfag committed Jul 14, 2019
1 parent c5b0f95 commit e175825
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions pkg/fsutils/linecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package fsutils
import (
"bytes"
"fmt"
"sync"

"github.com/pkg/errors"
)

type fileLinesCache [][]byte

type LineCache struct {
files map[string]fileLinesCache
files sync.Map
fileCache *FileCache
}

func NewLineCache(fc *FileCache) *LineCache {
return &LineCache{
files: map[string]fileLinesCache{},
fileCache: fc,
}
}
Expand Down Expand Up @@ -53,17 +53,17 @@ func (lc *LineCache) getRawLine(filePath string, index0 int) ([]byte, error) {
}

func (lc *LineCache) getFileCache(filePath string) (fileLinesCache, error) {
fc := lc.files[filePath]
if fc != nil {
return fc, nil
loadedFc, ok := lc.files.Load(filePath)
if ok {
return loadedFc.(fileLinesCache), nil
}

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

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

0 comments on commit e175825

Please sign in to comment.