Merge pull request #66 from sgotti/util_backoff_use_context
util: use context in backoff
This commit is contained in:
commit
e2526a6399
|
@ -437,7 +437,7 @@ func (h *ActionHandler) CreateRuns(ctx context.Context, req *CreateRunRequest) e
|
|||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
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 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} {
|
||||
var err error
|
||||
data, err = gitSource.GetFile(repopath, commitSHA, path.Join(agolaDefaultConfigDir, filename))
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
@ -87,7 +88,7 @@ type Backoff struct {
|
|||
//
|
||||
// If the condition never returns true, ErrWaitTimeout is returned. All other
|
||||
// errors terminate immediately.
|
||||
func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
|
||||
func ExponentialBackoff(ctx context.Context,backoff Backoff, condition ConditionFunc) error {
|
||||
duration := backoff.Duration
|
||||
for i := 0; i < backoff.Steps; i++ {
|
||||
if i != 0 {
|
||||
|
@ -95,7 +96,12 @@ func ExponentialBackoff(backoff Backoff, condition ConditionFunc) error {
|
|||
if backoff.Jitter > 0.0 {
|
||||
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)
|
||||
}
|
||||
if ok, err := condition(); err != nil || ok {
|
||||
|
|
Loading…
Reference in New Issue