diff options
| author | 2025-08-15 00:51:07 +0700 | |
|---|---|---|
| committer | 2025-08-15 00:51:07 +0700 | |
| commit | c9b2f8ca2239d64eaede7a0d755f038fe506bcca (patch) | |
| tree | c4249bc7b91967af9c1e0323aa5f347cb46a381e /main.go | |
| parent | ce7afeb5cb433a55fe2c30ab71cbeeb07c14b830 (diff) | |
| download | thumbnail-generator-c9b2f8ca2239d64eaede7a0d755f038fe506bcca.tar.gz thumbnail-generator-c9b2f8ca2239d64eaede7a0d755f038fe506bcca.zip | |
refactor: split semaphore into pre-process & process
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 12 |
1 files changed, 8 insertions, 4 deletions
@@ -86,11 +86,13 @@ func main() { default: } - semaphore := make(chan struct{}, cfg.MaxConcurrentJobs) + processSemaphore := make(chan struct{}, cfg.MaxProcessThreads) + queueSemaphore := make(chan struct{}, cfg.MaxPreProcessThreads) var wg sync.WaitGroup wg.Add(len(files)) for i, file := range files { + queueSemaphore <- struct{}{} go func(index int, inputName string) { outputName := conv.DeductOutputPath(inputName) fileLogger := generalLogger.With(slog.String("input_path", inputName), slog.String("output_path", outputName), slog.Int("file_index", index)) @@ -98,9 +100,8 @@ func main() { threadSigTermChannel := make(chan os.Signal, 1) signal.Notify(threadSigTermChannel, os.Interrupt, syscall.SIGTERM) - defer func() { <-semaphore }() - defer wg.Done() - semaphore <- struct{}{} + defer func() { <-queueSemaphore; wg.Done() }() + select { case <-threadSigTermChannel: fileLogger.Info("exiting due to termination signal") @@ -128,6 +129,9 @@ func main() { } } + processSemaphore <- struct{}{} + defer func() { <-processSemaphore }() + fileLogger.Info("start to process file", slog.String("input_hash", inputMetadata.Hash), slog.String("original_input_hash", originalInputHash)) reader, err := inputClient.GetReader(inputName) if err != nil { |