util: use context in backoff

This commit is contained in:
Simone Gotti 2019-07-25 15:55:52 +02:00
parent 335d1a2aad
commit bd035e9840
2 changed files with 11 additions and 5 deletions

View File

@ -437,7 +437,7 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e
cacheGroup = req.User.ID + "-" + req.UserRunRepoUUID cacheGroup = req.User.ID + "-" + req.UserRunRepoUUID
} }
data, filename, err := h.fetchConfigFiles(req.GitSource, req.RepoPath, req.CommitSHA) data, filename, err := h.fetchConfigFiles(ctx, req.GitSource, req.RepoPath, req.CommitSHA)
if err != nil { if err != nil {
return util.NewErrInternal(errors.Errorf("failed to fetch config file: %w", err)) return util.NewErrInternal(errors.Errorf("failed to fetch config file: %w", err))
} }
@ -498,10 +498,10 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e
return nil return nil
} }
func (h *ActionHandler) fetchConfigFiles(gitSource gitsource.GitSource, repopath, commitSHA string) ([]byte, string, error) { func (h *ActionHandler) fetchConfigFiles(ctx context.Context, gitSource gitsource.GitSource, repopath, commitSHA string) ([]byte, string, error) {
var data []byte var data []byte
var filename string var filename string
err := util.ExponentialBackoff(util.FetchFileBackoff, func() (bool, error) { err := util.ExponentialBackoff(ctx, util.FetchFileBackoff, func() (bool, error) {
for _, filename = range []string{agolaDefaultJsonnetConfigFile, agolaDefaultJsonConfigFile, agolaDefaultYamlConfigFile} { for _, filename = range []string{agolaDefaultJsonnetConfigFile, agolaDefaultJsonConfigFile, agolaDefaultYamlConfigFile} {
var err error var err error
data, err = gitSource.GetFile(repopath, commitSHA, path.Join(agolaDefaultConfigDir, filename)) data, err = gitSource.GetFile(repopath, commitSHA, path.Join(agolaDefaultConfigDir, filename))

View File

@ -15,6 +15,7 @@
package util package util
import ( import (
"context"
"errors" "errors"
"math/rand" "math/rand"
"time" "time"
@ -87,7 +88,7 @@ type Backoff struct {
// //
// If the condition never returns true, ErrWaitTimeout is returned. All other // If the condition never returns true, ErrWaitTimeout is returned. All other
// errors terminate immediately. // errors terminate immediately.
func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error { func ExponentialBackoff(ctx context.Context,backoff Backoff, condition ConditionFunc) error {
duration := backoff.Duration duration := backoff.Duration
for i := 0; i < backoff.Steps; i++ { for i := 0; i < backoff.Steps; i++ {
if i != 0 { if i != 0 {
@ -95,7 +96,12 @@ func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
if backoff.Jitter > 0.0 { if backoff.Jitter > 0.0 {
adjusted = Jitter(duration, backoff.Jitter) adjusted = Jitter(duration, backoff.Jitter)
} }
time.Sleep(adjusted) sleepCh := time.NewTimer(adjusted).C
select {
case <-ctx.Done():
return nil
case <-sleepCh:
}
duration = time.Duration(float64(duration) * backoff.Factor) duration = time.Duration(float64(duration) * backoff.Factor)
} }
if ok, err := condition(); err != nil || ok { if ok, err := condition(); err != nil || ok {