fix concurrency race condition resulting in null json file
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-26 10:34:18 -06:00
parent 3c5c99e340
commit aaf0abd08e

11
main.go
View File

@ -49,7 +49,7 @@ var ( // evil global variable
func init() { func init() {
// intialize and load ROMCache from file - so we don't have to rehash all the big files again // intialize and load ROMCache from file - so we don't have to rehash all the big files again
romCache = ROMCache{} romCache = ROMCache{}
romCache.Cached = make(map[string]bool)
romCacheJson, err := os.ReadFile(cacheFile) romCacheJson, err := os.ReadFile(cacheFile)
if err != nil { if err != nil {
if err != os.ErrNotExist { // don't care if it doesn't exist, just skip it if err != os.ErrNotExist { // don't care if it doesn't exist, just skip it
@ -60,8 +60,11 @@ func init() {
if err != nil { if err != nil {
log.Printf("failed to unmarshal romCacheJson to romCache struct: %v", err) log.Printf("failed to unmarshal romCacheJson to romCache struct: %v", err)
} }
} }
romCache.Cached = make(map[string]bool)
for _, rom := range romCache.ROMs { for _, rom := range romCache.ROMs {
romCache.Cached[rom.ID] = true romCache.Cached[rom.ID] = true
log.Printf("loaded cached file: %s", rom.Filename) log.Printf("loaded cached file: %s", rom.Filename)
@ -102,6 +105,8 @@ func updateROMCache() {
return return
} }
wg := sync.WaitGroup{}
for i, v := range files { for i, v := range files {
isLineageROM, splitName := isLineageROMZip(v) isLineageROM, splitName := isLineageROMZip(v)
@ -117,7 +122,9 @@ func updateROMCache() {
} }
romCache.Unlock() romCache.Unlock()
wg.Add(1)
go func(v fs.DirEntry) { go func(v fs.DirEntry) {
defer wg.Done()
fInfo, err := v.Info() fInfo, err := v.Info()
if err != nil { if err != nil {
log.Printf("failed to get file info '%s': %v", v.Name(), err) log.Printf("failed to get file info '%s': %v", v.Name(), err)
@ -148,10 +155,12 @@ func updateROMCache() {
romCache.ROMs = append(romCache.ROMs, lineageOSROM) romCache.ROMs = append(romCache.ROMs, lineageOSROM)
romCache.Cached[v.Name()] = true romCache.Cached[v.Name()] = true
romCache.Unlock() romCache.Unlock()
}(files[i]) }(files[i])
} }
// save file to disk for next startup so we don't have to rehash all the files again // save file to disk for next startup so we don't have to rehash all the files again
wg.Wait()
romCache.Lock() romCache.Lock()
romCacheJson, err := json.Marshal(romCache) romCacheJson, err := json.Marshal(romCache)
romCache.Unlock() romCache.Unlock()