fix cuncurrent [read/]write panic
This commit is contained in:
parent
360a58dca9
commit
c5b0f95dac
@ -3,6 +3,7 @@ package fsutils
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||||
|
|
||||||
@ -10,19 +11,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type FileCache struct {
|
type FileCache struct {
|
||||||
files map[string][]byte
|
files sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileCache() *FileCache {
|
func NewFileCache() *FileCache {
|
||||||
return &FileCache{
|
return &FileCache{}
|
||||||
files: map[string][]byte{},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
|
func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
|
||||||
cachedBytes := fc.files[filePath]
|
cachedBytes, ok := fc.files.Load(filePath)
|
||||||
if cachedBytes != nil {
|
if ok {
|
||||||
return cachedBytes, nil
|
return cachedBytes.([]byte), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fileBytes, err := ioutil.ReadFile(filePath)
|
fileBytes, err := ioutil.ReadFile(filePath)
|
||||||
@ -30,7 +29,7 @@ func (fc *FileCache) GetFileBytes(filePath string) ([]byte, error) {
|
|||||||
return nil, errors.Wrapf(err, "can't read file %s", filePath)
|
return nil, errors.Wrapf(err, "can't read file %s", filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
fc.files[filePath] = fileBytes
|
fc.files.Store(filePath, fileBytes)
|
||||||
return fileBytes, nil
|
return fileBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,9 +55,13 @@ func prettifyBytesCount(n int) string {
|
|||||||
|
|
||||||
func (fc *FileCache) PrintStats(log logutils.Log) {
|
func (fc *FileCache) PrintStats(log logutils.Log) {
|
||||||
var size int
|
var size int
|
||||||
for _, fileBytes := range fc.files {
|
var mapLen int
|
||||||
size += len(fileBytes)
|
fc.files.Range(func(_, fileBytes interface{}) bool {
|
||||||
}
|
mapLen++
|
||||||
|
size += len(fileBytes.([]byte))
|
||||||
|
|
||||||
log.Infof("File cache stats: %d entries of total size %s", len(fc.files), prettifyBytesCount(size))
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Infof("File cache stats: %d entries of total size %s", mapLen, prettifyBytesCount(size))
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user