Skip to content

Commit

Permalink
feat: retry context
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwalt committed Jan 7, 2024
1 parent 3108c47 commit a0b0172
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions runtime/retry.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package runtime

import (
"context"
"errors"
"sync/atomic"
"time"

retry "github.com/avast/retry-go/v4"
"github.com/hjwalt/runway/logger"
"github.com/hjwalt/runway/reflect"
)

// constructor
Expand Down Expand Up @@ -94,6 +96,8 @@ func (c *Retry) Do(fnToDo func(int64) error) error {
return err
}

// utils

var ErrRetryStopped = errors.New("retry stopped")

func AlwaysTry(err error) bool {
Expand All @@ -102,3 +106,11 @@ func AlwaysTry(err error) bool {
}
return true
}

func SetRetryCount(ctx context.Context, trycount int64) context.Context {
return context.WithValue(ctx, Context("RetryCount"), trycount)
}

func GetRetryCount(ctx context.Context) int64 {
return reflect.GetInt64(ctx.Value(Context("RetryCount")))
}
13 changes: 13 additions & 0 deletions runtime/retry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"context"
"errors"
"testing"
"time"
Expand Down Expand Up @@ -99,3 +100,15 @@ func TestRetryAbsorb(t *testing.T) {
assert.Equal(lastiteration, int64(100))
assert.NoError(err)
}

func TestRetryContext(t *testing.T) {
assert := assert.New(t)

ctx := context.Background()
zeroCount := GetRetryCount(ctx)
assert.Equal(int64(0), zeroCount)

withTryCount := SetRetryCount(ctx, 100)
retrieved := GetRetryCount(withTryCount)
assert.Equal(int64(100), retrieved)
}

0 comments on commit a0b0172

Please sign in to comment.