Cache file descriptors
This commit is contained in:
committed by
Audrius Butkevicius
parent
d80c40cfbf
commit
992ad97ad5
@@ -41,6 +41,8 @@ import (
|
||||
"github.com/syncthing/syncthing/internal/stats"
|
||||
"github.com/syncthing/syncthing/internal/symlinks"
|
||||
"github.com/syncthing/syncthing/internal/versioner"
|
||||
|
||||
"github.com/AudriusButkevicius/lrufdcache"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
)
|
||||
|
||||
@@ -86,6 +88,7 @@ type Model struct {
|
||||
db *leveldb.DB
|
||||
finder *files.BlockFinder
|
||||
progressEmitter *ProgressEmitter
|
||||
cache *lrufdcache.FileCache
|
||||
|
||||
deviceName string
|
||||
clientName string
|
||||
@@ -127,6 +130,7 @@ func NewModel(cfg *config.ConfigWrapper, deviceName, clientName, clientVersion s
|
||||
m := &Model{
|
||||
cfg: cfg,
|
||||
db: db,
|
||||
cache: lrufdcache.NewCache(25),
|
||||
deviceName: deviceName,
|
||||
clientName: clientName,
|
||||
clientVersion: clientVersion,
|
||||
@@ -695,12 +699,11 @@ func (m *Model) Request(deviceID protocol.DeviceID, folder, name string, offset
|
||||
}
|
||||
reader = strings.NewReader(target)
|
||||
} else {
|
||||
reader, err = os.Open(fn) // XXX: Inefficient, should cache fd?
|
||||
reader, err = m.cache.Open(fn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer reader.(*os.File).Close()
|
||||
defer reader.(*lrufdcache.CachedFile).Close()
|
||||
}
|
||||
|
||||
buf := make([]byte, size)
|
||||
|
||||
@@ -26,8 +26,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/AudriusButkevicius/lfu-go"
|
||||
|
||||
"github.com/syncthing/syncthing/internal/config"
|
||||
"github.com/syncthing/syncthing/internal/events"
|
||||
"github.com/syncthing/syncthing/internal/osutil"
|
||||
@@ -603,19 +601,6 @@ nextFile:
|
||||
p.progressEmitter.Register(state.sharedPullerState)
|
||||
}
|
||||
|
||||
evictionChan := make(chan lfu.Eviction)
|
||||
|
||||
fdCache := lfu.New()
|
||||
fdCache.UpperBound = 50
|
||||
fdCache.LowerBound = 20
|
||||
fdCache.EvictionChannel = evictionChan
|
||||
|
||||
go func() {
|
||||
for item := range evictionChan {
|
||||
item.Value.(*os.File).Close()
|
||||
}
|
||||
}()
|
||||
|
||||
folderRoots := make(map[string]string)
|
||||
p.model.fmut.RLock()
|
||||
for folder, cfg := range p.model.folderCfgs {
|
||||
@@ -629,18 +614,11 @@ nextFile:
|
||||
found := p.model.finder.Iterate(block.Hash, func(folder, file string, index uint32) bool {
|
||||
path := filepath.Join(folderRoots[folder], file)
|
||||
|
||||
var fd *os.File
|
||||
|
||||
fdi := fdCache.Get(path)
|
||||
if fdi != nil {
|
||||
fd = fdi.(*os.File)
|
||||
} else {
|
||||
fd, err = os.Open(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
fdCache.Set(path, fd)
|
||||
fd, err := p.model.cache.Open(path)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
_, err = fd.ReadAt(buf, protocol.BlockSize*int64(index))
|
||||
if err != nil {
|
||||
@@ -689,8 +667,6 @@ nextFile:
|
||||
state.copyDone()
|
||||
}
|
||||
}
|
||||
fdCache.Evict(fdCache.Len())
|
||||
close(evictionChan)
|
||||
out <- state.sharedPullerState
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user