mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-13 20:18:52 +02:00
perf: improve error handling by having error channel
This commit is contained in:
@@ -83,6 +83,8 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
// Channel to manage the files to process
|
||||
fileChan := make(chan string)
|
||||
// Channel to collect errors
|
||||
errorChan := make(chan error, parallelism)
|
||||
|
||||
// WaitGroup to wait for all goroutines to finish
|
||||
var wg sync.WaitGroup
|
||||
@@ -98,7 +100,7 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
||||
// Load the chapter
|
||||
chapter, err := cbz.LoadChapter(path)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to load chapter: %v\n", err)
|
||||
errorChan <- fmt.Errorf("failed to load chapter: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -112,7 +114,7 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
||||
fmt.Println(msg)
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to convert chapter: %v\n", err)
|
||||
errorChan <- fmt.Errorf("failed to convert chapter: %v", err)
|
||||
continue
|
||||
}
|
||||
convertedChapter.SetConverted()
|
||||
@@ -124,7 +126,7 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
err = cbz.WriteChapterToCBZ(convertedChapter, outputPath)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to write converted chapter: %v\n", err)
|
||||
errorChan <- fmt.Errorf("failed to write converted chapter: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -150,8 +152,18 @@ func ConvertCbzCommand(cmd *cobra.Command, args []string) error {
|
||||
return fmt.Errorf("error walking the path: %w", err)
|
||||
}
|
||||
|
||||
close(fileChan) // Close the channel to signal workers to stop
|
||||
wg.Wait() // Wait for all workers to finish
|
||||
close(fileChan) // Close the channel to signal workers to stop
|
||||
wg.Wait() // Wait for all workers to finish
|
||||
close(errorChan) // Close the error channel
|
||||
|
||||
var errs []error
|
||||
for err := range errorChan {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("encountered errors: %v", errs)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -12,7 +12,6 @@ import (
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"log"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -59,6 +58,7 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
maxGoroutines := runtime.NumCPU()
|
||||
|
||||
pagesChan := make(chan *packer2.PageContainer, maxGoroutines)
|
||||
errChan := make(chan error, maxGoroutines)
|
||||
|
||||
var wgPages sync.WaitGroup
|
||||
wgPages.Add(len(chapter.Pages))
|
||||
@@ -78,6 +78,7 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
buffer := new(bytes.Buffer)
|
||||
err := png.Encode(buffer, convertedPage.Image)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
<-guard
|
||||
return
|
||||
}
|
||||
@@ -91,7 +92,6 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
pagesMutex.Unlock()
|
||||
<-guard
|
||||
}(page)
|
||||
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -101,7 +101,7 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
|
||||
splitNeeded, img, format, err := converter.checkPageNeedsSplit(page)
|
||||
if err != nil {
|
||||
log.Fatalf("error checking if page %d d of chapter %s needs split: %v", page.Index, chapter.FilePath, err)
|
||||
errChan <- fmt.Errorf("error checking if page %d of chapter %s needs split: %v", page.Index, chapter.FilePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
}
|
||||
images, err := converter.cropImage(img)
|
||||
if err != nil {
|
||||
log.Fatalf("error converting page %d of chapter %s to webp: %v", page.Index, chapter.FilePath, err)
|
||||
errChan <- fmt.Errorf("error converting page %d of chapter %s to webp: %v", page.Index, chapter.FilePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -123,12 +123,21 @@ func (converter *Converter) ConvertChapter(chapter *packer2.Chapter, quality uin
|
||||
pagesChan <- packer2.NewContainer(page, img, "N/A")
|
||||
}
|
||||
}(page)
|
||||
|
||||
}
|
||||
|
||||
wgPages.Wait()
|
||||
wgConvertedPages.Wait()
|
||||
close(pagesChan)
|
||||
close(errChan)
|
||||
|
||||
var errList []error
|
||||
for err := range errChan {
|
||||
errList = append(errList, err)
|
||||
}
|
||||
|
||||
if len(errList) > 0 {
|
||||
return nil, fmt.Errorf("encountered errors: %v", errList)
|
||||
}
|
||||
|
||||
slices.SortFunc(pages, func(a, b *packer2.Page) int {
|
||||
if a.Index == b.Index {
|
||||
|
Reference in New Issue
Block a user