Skip to content

Commit

Permalink
feat: avoid having duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 22, 2018
1 parent af18a79 commit 38d3c5e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (s *Server) ListReleaseIOSJson(c echo.Context) error {
ManifestURL: manifestURL,
}
}
for _, build := range s.cache.builds {
for _, build := range s.cache.builds.Sorted() {
if build.BuildParameters["CIRCLE_JOB"] != "client.rn.ios" {
continue
}
Expand Down Expand Up @@ -173,7 +173,7 @@ func (s *Server) ListReleaseIOS(c echo.Context) error {
oncePerBranch := map[string]bool{}
previousDate := ""
now := time.Now().Truncate(time.Hour * 24)
for _, build := range s.cache.builds {
for _, build := range s.cache.builds.Sorted() {
if build.BuildParameters["CIRCLE_JOB"] != "client.rn.ios" {
continue
}
Expand Down
34 changes: 29 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"math/rand"
"net/http"
"regexp"
"sort"
"sync"
"time"

"github.com/berty/staff/tools/release/pkg/circle"
Expand All @@ -30,8 +32,25 @@ type ServerConfig struct {
Password string
}

type buildMap map[int]*circleci.Build

func (m buildMap) Sorted() []*circleci.Build {
buildMapMutex.Lock()
defer buildMapMutex.Unlock()
keys := []int{}
for k := range m {
keys = append(keys, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(keys)))
slice := []*circleci.Build{}
for _, k := range keys {
slice = append(slice, m[k])
}
return slice
}

type Cache struct {
builds []*circleci.Build
builds buildMap
mostRecentBuild time.Time
}

Expand Down Expand Up @@ -150,7 +169,7 @@ func (s *Server) Start() error {

func (s *Server) refreshCache() error {
var (
allBuilds []*circleci.Build
allBuilds = make(buildMap, 0)
mostRecentBuild = s.cache.mostRecentBuild
)

Expand All @@ -166,7 +185,9 @@ func (s *Server) refreshCache() error {
mostRecentBuild = *build.StopTime
}
}
allBuilds = append(allBuilds, builds...)
for _, build := range builds {
allBuilds[build.BuildNum] = build
}
if len(builds) < 100 {
break
}
Expand All @@ -193,7 +214,7 @@ func (s *Server) refreshCache() error {
mostRecentBuild = *updateTime
}
if updateTime.After(previousMostRecentBuild) {
allBuilds = append([]*circleci.Build{build}, allBuilds...)
allBuilds[build.BuildNum] = build
hasChanged = true
}
}
Expand All @@ -202,8 +223,11 @@ func (s *Server) refreshCache() error {
}
}

// FIXME: lock
buildMapMutex.Lock()
defer buildMapMutex.Unlock()
s.cache.builds = allBuilds
s.cache.mostRecentBuild = mostRecentBuild
return nil
}

var buildMapMutex sync.Mutex

0 comments on commit 38d3c5e

Please sign in to comment.