Skip to content

Commit 4b8ef13

Browse files
authored
perf(http): asynchronous retrieval of user keys (#8)
1 parent 472fef2 commit 4b8ef13

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed
File renamed without changes.
File renamed without changes.

main.go

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"net/http"
99
"os"
10+
"sync"
1011
)
1112

1213
const (
@@ -28,50 +29,62 @@ func main() {
2829
members := getMembers()
2930

3031
fmt.Println("getting keys")
32+
3133
getKeys(members)
3234
}
3335

3436
func getKeys(members []member) {
35-
client := &http.Client{}
36-
3737
var membersWithNoKey []member
38-
38+
var wg sync.WaitGroup
39+
client := &http.Client{}
3940
for _, member := range members {
40-
req, err := http.NewRequest(
41-
"GET",
42-
fmt.Sprintf("%s/%s.keys", githubURL, member.Login),
43-
nil,
44-
)
45-
if err != nil {
46-
log.Fatal(err)
47-
}
48-
req.Header.Add("authorization", fmt.Sprintf("token %s", githubPat))
49-
50-
res, err := client.Do(req)
51-
if err != nil {
52-
log.Fatal(err)
53-
}
54-
55-
defer res.Body.Close()
56-
57-
key, err := ioutil.ReadAll(res.Body)
58-
if err != nil {
59-
log.Fatal(err)
60-
}
61-
62-
if len(key) != 0 {
63-
fmt.Println(fmt.Sprintf("%s:\n%s", member.Login, key))
64-
fmt.Println("-------------------------------------------------------------------------------------")
65-
fmt.Println()
66-
} else {
67-
membersWithNoKey = append(membersWithNoKey, member)
68-
}
41+
wg.Add(1)
42+
member := member
43+
44+
go func() {
45+
defer wg.Done()
46+
47+
req, err := http.NewRequest(
48+
"GET",
49+
fmt.Sprintf("%s/%s.keys", githubURL, member.Login),
50+
nil,
51+
)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
req.Header.Add("authorization", fmt.Sprintf("token %s", githubPat))
56+
57+
res, err := client.Do(req)
58+
if err != nil {
59+
log.Fatal(err)
60+
}
61+
62+
defer res.Body.Close()
63+
64+
key, err := ioutil.ReadAll(res.Body)
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
69+
if len(key) != 0 {
70+
fmt.Println(fmt.Sprintf("%s:\n%s", member.Login, key))
71+
} else {
72+
membersWithNoKey = append(membersWithNoKey, member)
73+
}
74+
}()
6975
}
76+
wg.Wait()
7077

71-
fmt.Println(fmt.Sprintf("members with no keys (%d):", len(membersWithNoKey)))
78+
fmt.Println(fmt.Sprintf("users with no key (%d):", len(membersWithNoKey)))
7279
for _, member := range membersWithNoKey {
73-
fmt.Println(fmt.Sprintf("%s", member.Login))
80+
wg.Add(1)
81+
member := member
82+
go func() {
83+
defer wg.Done()
84+
fmt.Println(member.Login)
85+
}()
7486
}
87+
wg.Wait()
7588
}
7689

7790
func getMembers() []member {

0 commit comments

Comments
 (0)