From bc52b1a949d43b8c95b80ae57e8a4267539fbbcb Mon Sep 17 00:00:00 2001 From: Antoine Aflalo <197810+Belphemur@users.noreply.github.com> Date: Tue, 27 Aug 2024 09:54:47 -0400 Subject: [PATCH] perf: improve error handling by having error channel --- cmd/convert_cbz_command.go | 22 +++++++++++++++++----- converter/webp/webp_converter.go | 19 ++++++++++++++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/cmd/convert_cbz_command.go b/cmd/convert_cbz_command.go index 74ac422..d9f2818 100644 --- a/cmd/convert_cbz_command.go +++ b/cmd/convert_cbz_command.go @@ -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 } diff --git a/converter/webp/webp_converter.go b/converter/webp/webp_converter.go index 0455704..6fe0b12 100644 --- a/converter/webp/webp_converter.go +++ b/converter/webp/webp_converter.go @@ -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 {