Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retries when loading config #887

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions server/handler/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package handler

import (
"context"
"errors"
"os"
"time"

"github.com/google/go-github/v66/github"
"github.com/palantir/go-githubapp/appconfig"
Expand All @@ -37,26 +40,48 @@ type ConfigFetcher struct {
}

func (cf *ConfigFetcher) ConfigForRepositoryBranch(ctx context.Context, client *github.Client, owner, repository, branch string) FetchedConfig {
retries := 0
delay := 1 * time.Second
for {
c, err := cf.Loader.LoadConfig(ctx, client, owner, repository, branch)
fc := FetchedConfig{
Source: c.Source,
Path: c.Path,
}

c, err := cf.Loader.LoadConfig(ctx, client, owner, repository, branch)
fc := FetchedConfig{
Source: c.Source,
Path: c.Path,
}
if err != nil {
var ghErr *github.ErrorResponse
if !os.IsTimeout(err) && !errors.As(err, &ghErr) && ghErr.Response.StatusCode != 500 {
fc.LoadError = err
return fc
}

switch {
case err != nil:
fc.LoadError = err
return fc
case c.IsUndefined():
return fc
}
retries++
if retries > 3 {
fc.LoadError = err
return fc
}

var pc policy.Config
if err := yaml.UnmarshalStrict(c.Content, &pc); err != nil {
fc.ParseError = err
} else {
fc.Config = &pc
select {
case <-ctx.Done():
fc.LoadError = ctx.Err()
return fc
case <-time.After(delay):
delay *= 2
continue
}
}

if c.IsUndefined() {
return fc
}

var pc policy.Config
if err := yaml.UnmarshalStrict(c.Content, &pc); err != nil {
fc.ParseError = err
} else {
fc.Config = &pc
}
return fc
}
return fc
}
Loading