From da672112ebbc25a3fa9f2cf8e37994a7bd1d0e2f Mon Sep 17 00:00:00 2001 From: SayaAndy Date: Wed, 3 Sep 2025 10:54:34 +0700 Subject: feat: add cache map rwmutex --- main.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index c3f03e7..521960e 100644 --- a/main.go +++ b/main.go @@ -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) } -- cgit v1.3.1+13