stores: Deduplicate parallel stat requests

This commit is contained in:
Łukasz Magiera 2022-05-03 15:47:14 -04:00
parent e793d7e128
commit 1249809283

View File

@ -85,9 +85,58 @@ type path struct {
reserved int64
reservations map[abi.SectorID]storiface.SectorFileType
statLk sync.Mutex
statDone chan struct{}
lastStat *fsutil.FsStat // nil if no stat / was error
}
func (p *path) stat(ls LocalStorage) (fsutil.FsStat, error) {
for {
p.statLk.Lock()
if p.statDone == nil {
p.statDone = make(chan struct{})
p.statLk.Unlock()
st, err := p.doStat(ls)
p.statLk.Lock()
p.lastStat = nil
if err == nil {
p.lastStat = &st
}
close(p.statDone)
p.statDone = nil
p.statLk.Unlock()
return st, err
}
doneCh := p.statDone
p.statLk.Unlock()
select {
case <-doneCh:
// todo context?
}
p.statLk.Lock()
if p.lastStat == nil {
p.statLk.Unlock()
continue
}
st := *p.lastStat
p.statLk.Unlock()
return st, nil
}
}
func (p *path) doStat(ls LocalStorage) (fsutil.FsStat, error) {
start := time.Now()
stat, err := ls.Stat(p.local)
if err != nil {
return fsutil.FsStat{}, xerrors.Errorf("stat %s: %w", p.local, err)
@ -155,6 +204,8 @@ func (p *path) stat(ls LocalStorage) (fsutil.FsStat, error) {
}
}
log.Infow("storage stat", "took", time.Now().Sub(start), "reservations", len(p.reservations))
return stat, err
}