Skip to content

Commit 0510b7b

Browse files
aristocrateskrrrr38
authored andcommitted
Improve github pull request call retries (runatlantis#1810)
* Improve github pull request call retries Retry with fixed 1 second backoff up to 3 retries was added by runatlantis#1131 to address runatlantis#1019, but the issue continued to show up (runatlantis#1453). Increase max attempts to 5 and use exponential backoff for a maximum total retry time of (2^n - n - 1) seconds, which is roughly 30 seconds for current max attempts n = 5. Also move the sleep to the top of the loop so that we never sleep without sending the request again on the last iteration. * Fix style with gofmt -s
1 parent d6b54ec commit 0510b7b

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

server/events/vcs/github_client.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,14 @@ func (g *GithubClient) GetPullRequest(repo models.Repo, num int) (*github.PullRe
292292

293293
// GitHub has started to return 404's here (#1019) even after they send the webhook.
294294
// They've got some eventual consistency issues going on so we're just going
295-
// to retry up to 3 times with a 1s sleep.
296-
numRetries := 3
297-
retryDelay := 1 * time.Second
298-
for i := 0; i < numRetries; i++ {
295+
// to attempt up to 5 times with exponential backoff.
296+
maxAttempts := 5
297+
attemptDelay := 0 * time.Second
298+
for i := 0; i < maxAttempts; i++ {
299+
// First don't sleep, then sleep 1, 3, 7, etc.
300+
time.Sleep(attemptDelay)
301+
attemptDelay = 2*attemptDelay + 1*time.Second
302+
299303
pull, _, err = g.client.PullRequests.Get(g.ctx, repo.Owner, repo.Name, num)
300304
if err == nil {
301305
return pull, nil
@@ -304,7 +308,6 @@ func (g *GithubClient) GetPullRequest(repo models.Repo, num int) (*github.PullRe
304308
if !ok || ghErr.Response.StatusCode != 404 {
305309
return pull, err
306310
}
307-
time.Sleep(retryDelay)
308311
}
309312
return pull, err
310313
}

0 commit comments

Comments
 (0)