Skip to content

Commit

Permalink
fix: correctly exit download when meet errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
syhily committed Nov 16, 2022
1 parent 291dee4 commit fbf022f
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions internal/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type commonFetcher struct {
*Config
service service
progress progress.Progress
wait sync.WaitGroup
errs chan error
}

Expand Down Expand Up @@ -68,31 +67,32 @@ func (f *commonFetcher) Download() error {
f.errs = make(chan error, f.Thread)
defer close(f.errs)

var wait sync.WaitGroup
for i := 0; i < f.Thread; i++ {
f.wait.Add(1)
go f.startDownload()
wait.Add(1)
go func() {
defer wait.Done()
f.startDownload()
}()
}
wait.Wait()

f.wait.Wait()

// Acquire the download error.
// Acquire the download errors.
for e := range f.errs {
if e != nil {
return e
}
return e
}

return nil
}

// startDownload will start a download thread.
func (f *commonFetcher) startDownload() {
thread:
for {
bookID := f.progress.AcquireBookID()
if bookID == progress.NoBookToDownload {
// Finish this thread.
f.finishDownload(nil)
break
break thread
}

// Start download the given book ID.
Expand All @@ -101,8 +101,8 @@ func (f *commonFetcher) startDownload() {
// Acquire the available file formats
formats, err := f.service.formats(bookID)
if err != nil {
f.finishDownload(err)
break
f.errs <- err
break thread
}
log.Debugf("Book id %d formats: %v", bookID, formats)

Expand All @@ -116,16 +116,16 @@ func (f *commonFetcher) startDownload() {
for format, share := range formats {
err := f.downloadFile(bookID, format, share)
if err != nil {
f.finishDownload(err)
break
f.errs <- err
break thread
}
}

// Save the download progress
err = f.progress.SaveBookID(bookID)
if err != nil {
f.finishDownload(err)
break
f.errs <- err
break thread
}
}
}
Expand Down Expand Up @@ -192,11 +192,3 @@ func (f *commonFetcher) filterFormats(formats map[Format]driver.Share) map[Forma
}
return fs
}

// finishDownload will exist the download thread.
func (f *commonFetcher) finishDownload(err error) {
if err != nil {
f.errs <- err
}
f.wait.Done()
}

0 comments on commit fbf022f

Please sign in to comment.