more async improvements and crypto bugfix

This commit is contained in:
ston1th 2022-08-17 23:58:46 +02:00
commit 304135be2e
7 changed files with 161 additions and 79 deletions

View file

@ -48,17 +48,17 @@ func (p *preload) Read(data []byte) (n int, err error) {
return
}
func (f *File) Preload(ctx context.Context, unlock func()) {
func (f *File) Preload(ctx context.Context, fin func(), errf func(), stop func()) {
log := f.log
defer f.Close()
if f.offline {
log.V(2).Error(errors.New("no preload in offline mode"), "error preloading file")
unlock()
fin()
return
}
if f.md.FullyCached() {
log.V(2).Info("skipped preload for fully cached file")
unlock()
fin()
return
}
@ -67,18 +67,24 @@ func (f *File) Preload(ctx context.Context, unlock func()) {
_, err := io.Copy(Discard, p)
if err == context.Canceled {
log.V(2).Info("preload canceled", "skipped", p.skipped, "written", p.written)
// do not call unlock() to keep preload in list
stop()
return
}
perr := false
if err != nil && err != io.EOF {
log.Error(err, "error preloading file")
perr = true
}
err = f.md.Close()
if err != nil {
log.Error(err, "error closing cache file")
}
if perr {
errf()
return
}
log.V(2).Info("preload finished", "skipped", p.skipped, "written", p.written)
unlock()
fin()
}
func (f *File) Read(p []byte) (n int, err error) {
@ -130,13 +136,10 @@ func (f *File) readToCache(p []byte) (n int, err error) {
// async cache write
buf := make([]byte, n)
copy(buf, p[:n])
go func(p []byte, offset int64, n int) {
if n, err = f.md.WriteAt(p, offset); err != nil {
log.Error(err, "error writing cache file")
return
}
f.md.AddChunk(offset, n)
}(buf, f.offset, n)
if _, err = f.md.WriteAt(buf, f.offset); err != nil {
log.Error(err, "error writing cache file")
return
}
}
if err != nil && err != io.EOF {
log.Error(err, "error reading source file")