Skip to content

Commit

Permalink
simplify synchronization (fixes lauripiispanen#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauri Piispanen committed Dec 5, 2017
1 parent 7900bb5 commit a7bf967
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions top/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"regexp"
"strings"
"sync"
"log"
"time"
"github.com/lauripiispanen/most-active-github-users-counter/github"
Expand Down Expand Up @@ -42,14 +41,10 @@ func GithubTop(options TopOptions) (GithubDataPieces, error) {
userContributions := make(UserContributions, 0)
userContribChan := make(chan UserContribution)

var wg sync.WaitGroup
wg.Add(len(users))

throttle := time.Tick(time.Second / 10)

for _, username := range users {
go func(username string) {
defer wg.Done()
count, err := client.NumContributions(username)
if err != nil {
log.Fatal(err)
Expand All @@ -61,30 +56,25 @@ func GithubTop(options TopOptions) (GithubDataPieces, error) {
<- throttle
}

go func() {
for userContrib := range userContribChan {
userContributions = append(userContributions, userContrib)
}
}()
for userContrib := range userContribChan {
userContributions = append(userContributions, userContrib)
if (len(userContributions) >= len(users)) {
close(userContribChan)
}
}

wg.Wait()

sort.Sort(userContributions)
if (len(userContributions) < num_top) {
num_top = len(userContributions)
}

userContributions = userContributions[:num_top]


pieces := make(chan GithubDataPiece)
wg.Add(len(userContributions))

throttle = time.Tick(time.Second / 10)

for _, user := range userContributions {
go func(user UserContribution) {
defer wg.Done()
u, err := client.User(user.Username)
if err != nil {
log.Fatal(err)
Expand All @@ -101,18 +91,15 @@ func GithubTop(options TopOptions) (GithubDataPieces, error) {
<- throttle
}

go func() {
for piece := range pieces {
data = append(data, piece)
}
}()

wg.Wait()
for piece := range pieces {
data = append(data, piece)
if (len(data) >= len(userContributions)) {
close(pieces)
}
}

sort.Sort(data)

data = data[:num_top]

return data, nil
}

Expand Down

0 comments on commit a7bf967

Please sign in to comment.