From b84b7bdb4ef3b33ffe47e03ef62e0cafc21bdcf3 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 31 Dec 2022 14:55:00 +1300 Subject: [PATCH] sync: idx values from reposAlphabeticalOrder do not match reposCache indexes --- main.go | 1 + sync.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index f116f3f..7967068 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ type Application struct { reposMut sync.RWMutex reposCache []gitea.Repo // Sorted by recently-created-first + reposCacheByName map[string]int reposAlphabeticalOrder map[string]int } diff --git a/sync.go b/sync.go index a984828..dd54f19 100644 --- a/sync.go +++ b/sync.go @@ -94,7 +94,7 @@ func (this *Application) sync(ctx context.Context) (bool, error) { } for i, rr := range repos { - if idx, ok := this.reposAlphabeticalOrder[rr.Name]; ok && this.reposCache[idx].GiteaUpdated == rr.GiteaUpdated { + if idx, ok := this.reposCacheByName[rr.Name]; ok && this.reposCache[idx].GiteaUpdated == rr.GiteaUpdated { // Already exists in cache with same Gitea update time // Copy timestamps repos[i] = this.reposCache[idx] @@ -156,9 +156,15 @@ func (this *Application) sync(ctx context.Context) (bool, error) { repos = reordered } + reposCacheByName := make(map[string]int, len(repos)) + for idx, repo := range repos { + reposCacheByName[repo.Name] = idx + } + // Commit our changes for the other threads to look at this.reposMut.Lock() this.reposCache = repos + this.reposCacheByName = reposCacheByName this.reposAlphabeticalOrder = alphabeticalOrderIndexes this.reposMut.Unlock()