diff options
| author | 2025-09-03 10:54:34 +0700 | |
|---|---|---|
| committer | 2025-09-03 10:54:34 +0700 | |
| commit | da672112ebbc25a3fa9f2cf8e37994a7bd1d0e2f (patch) | |
| tree | b87511db622413eb652ad0555960f1581e40cf17 /main.go | |
| parent | 8e8b3cc5238693151978f5eefa2b6fac63adc50a (diff) | |
| download | thumbnail-generator-da672112ebbc25a3fa9f2cf8e37994a7bd1d0e2f.tar.gz thumbnail-generator-da672112ebbc25a3fa9f2cf8e37994a7bd1d0e2f.zip | |
feat: add cache map rwmutex
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 16 |
1 files changed, 13 insertions, 3 deletions
@@ -21,9 +21,10 @@ import ( ) var ( - configPath = flag.String("c", "config.json", "Path to the configuration file") - sigTermChan = make(chan os.Signal, 1) - cacheMap = make(map[string]map[uint32]struct{}) + configPath = flag.String("c", "config.json", "Path to the configuration file") + sigTermChan = make(chan os.Signal, 1) + cacheMapMutex = &sync.RWMutex{} + cacheMap = make(map[string]map[uint32]struct{}) ) func main() { @@ -153,19 +154,24 @@ func main() { id := inputClient.ID(file) if _, ok := cacheMap[id]; !ok { + cacheMapMutex.Lock() cacheMap[id] = make(map[uint32]struct{}) + cacheMapMutex.Unlock() } convertersToLaunch := []int{} for j, conv := range converters { if cfg.Input.CacheProcessed { + cacheMapMutex.RLock() if _, ok := cacheMap[id][converterHashes[j]]; ok { + cacheMapMutex.RUnlock() fileLogger.Info("skip already processed file (based on cache file containing it and processor)", slog.String("file_id", id), slog.Uint64("conv_hash", uint64(converterHashes[j])), slog.Int("conv_index", j)) continue } + cacheMapMutex.RUnlock() } outputName := conv.DeductOutputPath(inputName) originalInputHash := "" @@ -188,7 +194,9 @@ func main() { originalInputHash = outputMetadata.HashOriginal if inputMetadata.Hash == originalInputHash { convLogger.Info("skip already processed file (based on equal hash)", slog.String("input_hash", inputMetadata.Hash)) + cacheMapMutex.Lock() cacheMap[id][converterHashes[j]] = struct{}{} + cacheMapMutex.Unlock() continue } } @@ -227,7 +235,9 @@ func main() { } convLogger.Info("successfully processed file", slog.String("input_hash", inputMetadata.Hash)) + cacheMapMutex.Lock() cacheMap[id][converterHashes[convIndex]] = struct{}{} + cacheMapMutex.Unlock() } }(i, file) } |