added custom buffer size error handling

This commit is contained in:
ston1th 2022-03-30 17:06:56 +02:00
commit 711f020c18
4 changed files with 123 additions and 10 deletions

View file

@ -41,31 +41,33 @@ func (p *preload) Read(data []byte) (n int, err error) {
_, err = p.f.Seek(int64(n), io.SeekCurrent)
return
}
n, err = p.f.readWithoutCache(data)
n, err = p.f.readToCache(data)
p.written++
return
}
func (f *File) Preload(ctx context.Context, unlock func()) {
log := f.log
defer unlock()
if f.offline {
log.V(2).Error(errors.New("no preload in offline mode"), "error preloading file")
unlock()
return
}
if f.md.FullyCached() {
log.V(2).Info("skipped preload for fully cached file")
return
}
log.V(2).Info("preload started")
p := &preload{f: f, ctx: ctx}
_, err := io.Copy(io.Discard, p)
_, err := io.Copy(Discard, p)
if err == context.Canceled {
log.V(2).Info("preload canceled", "skipped", p.skipped, "written", p.written)
unlock()
return
}
if err != nil && err != io.EOF {
log.Error(err, "error preloading file")
}
log.V(2).Info("preload finished", "skipped", p.skipped, "written", p.written)
unlock()
}
func (f *File) Read(p []byte) (n int, err error) {
@ -73,8 +75,15 @@ func (f *File) Read(p []byte) (n int, err error) {
if f.hasChunk(len(p)) {
n, err = f.md.ReadAt(p, f.offset)
if err != nil {
log.Error(err, "error reading cache file")
return
if !IsIOErr(err) {
log.Error(err, "error reading cache file")
return
}
n, err = f.readSource(p)
if err != nil {
log.Error(err, "error reading source file")
return
}
}
_, err = f.Seek(int64(n), io.SeekCurrent)
if err != nil {
@ -82,7 +91,7 @@ func (f *File) Read(p []byte) (n int, err error) {
}
return
}
return f.readWithoutCache(p)
return f.readToCache(p)
}
func (f *File) size() int64 {
@ -93,12 +102,16 @@ func (f *File) hasChunk(n int) bool {
return f.md.HasChunk(f.offset, n)
}
func (f *File) readWithoutCache(p []byte) (n int, err error) {
func (f *File) readSource(p []byte) (n int, err error) {
if f.offline {
return 0, io.EOF
}
return f.f.Read(p)
}
func (f *File) readToCache(p []byte) (n int, err error) {
log := f.log
n, err = f.f.Read(p)
n, err = f.readSource(p)
if n > 0 {
if n, err := f.md.WriteAt(p[:n], f.offset); err != nil {
log.Error(err, "error writing cache file")