Skip to content

Commit

Permalink
return timeout for retry interval
Browse files Browse the repository at this point in the history
  • Loading branch information
amirylm committed Feb 14, 2024
1 parent 18b185f commit bb12ee9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const (
BlockNumber = "blockNumber" // valid for v0.2
Timestamp = "timestamp" // valid for v0.3
totalFastPluginRetries = 5
totalMediumPluginRetries = 10
totalMediumPluginRetries = totalFastPluginRetries + 1
RetryIntervalTimeout = time.Duration(-1)
)

var GenerateHMACFn = func(method string, path string, body []byte, clientId string, secret string, ts int64) string {
Expand All @@ -45,8 +46,7 @@ var GenerateHMACFn = func(method string, path string, body []byte, clientId stri
}

// CalculateRetryConfig returns plugin retry interval based on how many times plugin has retried this work
var CalculateRetryConfigFn = func(prk string, mercuryConfig MercuryConfigProvider) time.Duration {
var retryInterval time.Duration
var CalculateRetryConfigFn = func(prk string, mercuryConfig MercuryConfigProvider) (retryInterval time.Duration) {
var retries int
totalAttempts, ok := mercuryConfig.GetPluginRetry(prk)
if ok {
Expand All @@ -55,9 +55,9 @@ var CalculateRetryConfigFn = func(prk string, mercuryConfig MercuryConfigProvide
retryInterval = 1 * time.Second
} else if retries < totalMediumPluginRetries {
retryInterval = 5 * time.Second
} else {
retryInterval = RetryIntervalTimeout
}
// if the core node has retried totalMediumPluginRetries times, do not set retry interval and plugin will use
// the default interval
} else {
retryInterval = 1 * time.Second
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"errors"
"math/big"
"testing"
"time"

"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/require"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/stretchr/testify/assert"

"github.com/smartcontractkit/chainlink-common/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ocr2keeper/evmregistry/v21/encoding"
)

Expand Down Expand Up @@ -240,3 +243,77 @@ func TestPacker_UnpackCheckCallbackResult(t *testing.T) {
})
}
}

func Test_CalculateRetryConfigFn(t *testing.T) {
tests := []struct {
name string
times int
expected time.Duration
}{
{
name: "first retry",
times: 1,
expected: 1 * time.Second,
},
{
name: "second retry",
times: 2,
expected: 1 * time.Second,
},
{
name: "fifth retry",
times: 5,
expected: 1 * time.Second,
},
{
name: "sixth retry",
times: 6,
expected: 5 * time.Second,
},
{
name: "timeout",
times: totalMediumPluginRetries + 1,
expected: RetryIntervalTimeout,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg := newMercuryConfigMock()
var result time.Duration
for i := 0; i < tc.times; i++ {
result = CalculateRetryConfigFn("prk", cfg)
}
assert.Equal(t, tc.expected, result)
})
}
}

type mercuryConfigMock struct {
pluginRetryCache *cache.Cache
}

func newMercuryConfigMock() *mercuryConfigMock {
return &mercuryConfigMock{
pluginRetryCache: cache.New(10*time.Second, time.Minute),
}
}

func (c *mercuryConfigMock) Credentials() *types.MercuryCredentials {
return nil
}

func (c *mercuryConfigMock) IsUpkeepAllowed(k string) (interface{}, bool) {
return nil, false
}

func (c *mercuryConfigMock) SetUpkeepAllowed(k string, v interface{}, d time.Duration) {
}

func (c *mercuryConfigMock) GetPluginRetry(k string) (interface{}, bool) {
return c.pluginRetryCache.Get(k)
}

func (c *mercuryConfigMock) SetPluginRetry(k string, v interface{}, d time.Duration) {
c.pluginRetryCache.Set(k, v, d)
}

0 comments on commit bb12ee9

Please sign in to comment.