优化缓存相关代码

This commit is contained in:
GoEdgeLab
2022-04-14 10:25:34 +08:00
parent 0649eb0efe
commit 2b76fd4463
3 changed files with 35 additions and 2 deletions

View File

@@ -27,6 +27,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
@@ -62,7 +63,16 @@ const (
var sharedWritingFileKeyMap = map[string]zero.Zero{} // key => bool
var sharedWritingFileKeyLocker = sync.Mutex{}
var maxOpenFiles = 2
var maxOpenFiles = 3
func init() {
if teaconst.DiskIsFast {
maxOpenFiles = runtime.NumCPU()
}
if maxOpenFiles < 3 {
maxOpenFiles = 3
}
}
// FileStorage 文件缓存
// 文件结构:
@@ -425,7 +435,7 @@ func (this *FileStorage) openWriter(key string, expiredAt int64, status int, siz
return nil, ErrFileIsWriting
}
if len(sharedWritingFileKeyMap) > maxOpenFiles {
if !isFlushing && len(sharedWritingFileKeyMap) >= maxOpenFiles {
sharedWritingFileKeyLocker.Unlock()
return nil, ErrTooManyOpenFiles
}