Skip to content

Commit 489f603

Browse files
author
Lauri Piispanen
committed
add (simple) retry logic
1 parent 1d601df commit 489f603

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

github/github.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"regexp"
99
"strings"
10+
"time"
1011

1112
"github.com/lauripiispanen/most-active-github-users-counter/net"
1213
)
@@ -70,6 +71,9 @@ func (client HTTPGithubClient) SearchUsers(query UserSearchQuery) (GithubSearchR
7071
perPage := 5
7172
totalUsersCount := 0
7273

74+
retryCount := 0
75+
maxRetryCount := 10
76+
7377
Pages:
7478
for totalCount < query.MaxUsers {
7579
previousCursor := ""
@@ -119,16 +123,37 @@ Pages:
119123

120124
body, err := client.Request("https://api.github.com/graphql", graphQlString)
121125
if err != nil {
122-
log.Fatal(err)
126+
retryCount++
127+
if retryCount < maxRetryCount {
128+
log.Println("error making graphql request... retrying")
129+
time.Sleep(10 * time.Second)
130+
continue Pages
131+
} else {
132+
log.Fatalln("Too many errors received. Quitting.")
133+
}
123134
}
124135

125136
var response interface{}
126137
if err := json.Unmarshal(body, &response); err != nil {
127-
log.Fatal(err)
138+
retryCount++
139+
if retryCount < maxRetryCount {
140+
log.Println("error unmarshalling JSON response... retrying")
141+
time.Sleep(10 * time.Second)
142+
continue Pages
143+
} else {
144+
log.Fatalln("Too many errors received. Quitting.")
145+
}
128146
}
129147
rootNode := response.(map[string]interface{})
130148
if val, ok := rootNode["errors"]; ok {
131-
log.Fatalf("%s", val)
149+
retryCount++
150+
if retryCount < maxRetryCount {
151+
log.Printf("Received error response (retrying): %+v", val)
152+
time.Sleep(10 * time.Second)
153+
continue Pages
154+
} else {
155+
log.Fatalln("Too many errors received. Quitting.")
156+
}
132157
}
133158
dataNode := rootNode["data"].(map[string]interface{})
134159
searchNode := dataNode["search"].(map[string]interface{})

0 commit comments

Comments
 (0)