Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(imagor): fanout seeker improvement #206

Merged
merged 2 commits into from
Oct 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions fanout.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func fanoutReader(source io.ReadCloser, size int) func() (io.Reader, io.Seeker,
var lock sync.RWMutex
var once sync.Once
var consumers []chan []byte
var done = make(chan struct{})
var fullBufReady = make(chan struct{})
var closed []bool
var err error
var buf = make([]byte, size)
Expand Down Expand Up @@ -55,7 +55,7 @@ func fanoutReader(source io.ReadCloser, size int) func() (io.Reader, io.Seeker,
}
lock.RUnlock()
if currentSize >= size {
close(done)
close(fullBufReady)
}
if e != nil || currentSize >= size {
return
Expand Down Expand Up @@ -101,7 +101,7 @@ func fanoutReader(source io.ReadCloser, size int) func() (io.Reader, io.Seeker,
if readerClosed {
return 0, io.ErrClosedPipe
}
if fullBufReader != nil && !readerClosed {
if fullBufReader != nil {
// proxy to full buf if ready
return fullBufReader.Read(p)
}
Expand Down Expand Up @@ -153,17 +153,21 @@ func fanoutReader(source io.ReadCloser, size int) func() (io.Reader, io.Seeker,
once.Do(func() {
go init()
})

if fullBufReader != nil && !readerClosed {
return fullBufReader.Seek(offset, whence)
} else if fullBufReader == nil && !readerClosed {
<-done
fullBufReader = bytes.NewReader(buf)
_ = closeCh(false)
return fullBufReader.Seek(offset, whence)
} else {
if readerClosed {
return 0, io.ErrClosedPipe
}
if bufReader != nil &&
((whence == io.SeekStart && offset < bufReader.Size()) ||
(whence == io.SeekCurrent && offset < int64(bufReader.Len()))) {
return bufReader.Seek(offset, whence)
}
if fullBufReader != nil {
return fullBufReader.Seek(offset, whence)
}
<-fullBufReady
fullBufReader = bytes.NewReader(buf)
_ = closeCh(false)
return fullBufReader.Seek(offset, whence)
})
return
}
Expand Down