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()