Skip to content

Commit

Permalink
[v2.x]Rebuild retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Mar 17, 2024
1 parent bb7ae5f commit 1ae3127
Show file tree
Hide file tree
Showing 16 changed files with 1,308 additions and 603 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/alibabacloud-go/tea
module github.com/alibabacloud-go/tea/v2

go 1.14

Expand Down
117 changes: 117 additions & 0 deletions tea/back_off_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package tea

import (
"math"
"math/rand"
)

type BackoffPolicy interface {
GetDelayTimeMillis(ctx *RetryPolicyContext) *int64
}

func NewBackoffPolicy(options map[string]interface{}) (backoffPolicy BackoffPolicy) {
policy := StringValue(TransInterfaceToString(options["policy"]))
switch policy {
case "Fixed":
backoffPolicy = &FixedBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
}
return
case "Random":
backoffPolicy = &RandomBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
Cap: TransInterfaceToInt64(options["cap"]),
}
return
case "Exponential":
backoffPolicy = &ExponentialBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
Cap: TransInterfaceToInt64(options["cap"]),
}
return
case "EqualJitter":
backoffPolicy = &EqualJitterBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
Cap: TransInterfaceToInt64(options["cap"]),
}
return
case "ExponentialWithEqualJitter":
backoffPolicy = &EqualJitterBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
Cap: TransInterfaceToInt64(options["cap"]),
}
return
case "FullJitter":
backoffPolicy = &FullJitterBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
Cap: TransInterfaceToInt64(options["cap"]),
}
return
case "ExponentialWithFullJitter":
backoffPolicy = &FullJitterBackoffPolicy{
Period: TransInterfaceToInt(options["period"]),
Cap: TransInterfaceToInt64(options["cap"]),
}
return
}
return nil

Check warning on line 57 in tea/back_off_policy.go

View check run for this annotation

Codecov / codecov/patch

tea/back_off_policy.go#L57

Added line #L57 was not covered by tests
}

type FixedBackoffPolicy struct {
Period *int
}

func (fixedBackoff *FixedBackoffPolicy) GetDelayTimeMillis(ctx *RetryPolicyContext) *int64 {
return Int64(int64(IntValue(fixedBackoff.Period)))
}

type RandomBackoffPolicy struct {
Period *int
Cap *int64
}

func (randomBackoff *RandomBackoffPolicy) GetDelayTimeMillis(ctx *RetryPolicyContext) *int64 {
if randomBackoff.Cap == nil {
randomBackoff.Cap = Int64(20 * 1000)

Check warning on line 75 in tea/back_off_policy.go

View check run for this annotation

Codecov / codecov/patch

tea/back_off_policy.go#L75

Added line #L75 was not covered by tests
}
ceil := math.Min(float64(*randomBackoff.Cap), float64(IntValue(randomBackoff.Period))*float64(IntValue(ctx.RetriesAttempted)))
return Int64(int64(rand.Float64() * ceil))
}

type ExponentialBackoffPolicy struct {
Period *int
Cap *int64
}

func (exponentialBackoff *ExponentialBackoffPolicy) GetDelayTimeMillis(ctx *RetryPolicyContext) *int64 {
if exponentialBackoff.Cap == nil {
exponentialBackoff.Cap = Int64(3 * 24 * 60 * 60 * 1000)

Check warning on line 88 in tea/back_off_policy.go

View check run for this annotation

Codecov / codecov/patch

tea/back_off_policy.go#L88

Added line #L88 was not covered by tests
}
return Int64(int64(math.Min(float64(*exponentialBackoff.Cap), float64(IntValue(exponentialBackoff.Period))*math.Pow(2.0, float64(IntValue(ctx.RetriesAttempted))))))
}

type EqualJitterBackoffPolicy struct {
Period *int
Cap *int64
}

func (equalJitterBackoff *EqualJitterBackoffPolicy) GetDelayTimeMillis(ctx *RetryPolicyContext) *int64 {
if equalJitterBackoff.Cap == nil {
equalJitterBackoff.Cap = Int64(3 * 24 * 60 * 60 * 1000)

Check warning on line 100 in tea/back_off_policy.go

View check run for this annotation

Codecov / codecov/patch

tea/back_off_policy.go#L100

Added line #L100 was not covered by tests
}
ceil := math.Min(float64(*equalJitterBackoff.Cap), float64(IntValue(equalJitterBackoff.Period))*math.Pow(2.0, float64(IntValue(ctx.RetriesAttempted))))
return Int64(int64(ceil/2 + rand.Float64()*(ceil/2+1)))
}

type FullJitterBackoffPolicy struct {
Period *int
Cap *int64
}

func (fullJitterBackof *FullJitterBackoffPolicy) GetDelayTimeMillis(ctx *RetryPolicyContext) *int64 {
if fullJitterBackof.Cap == nil {
fullJitterBackof.Cap = Int64(3 * 24 * 60 * 60 * 1000)

Check warning on line 113 in tea/back_off_policy.go

View check run for this annotation

Codecov / codecov/patch

tea/back_off_policy.go#L113

Added line #L113 was not covered by tests
}
ceil := math.Min(float64(*fullJitterBackof.Cap), float64(IntValue(fullJitterBackof.Period))*math.Pow(2.0, float64(IntValue(ctx.RetriesAttempted))))
return Int64(int64(rand.Float64() * ceil))
}
135 changes: 135 additions & 0 deletions tea/back_off_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package tea

import (
"reflect"
"testing"

"github.com/alibabacloud-go/tea/v2/utils"
)

func TestBackoffPolicy(t *testing.T) {
var backoffPolicy BackoffPolicy
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "Fixed",
"period": 1000,
})
typeOfPolicy := reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "FixedBackoffPolicy", typeOfPolicy.Elem().Name())
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "Random",
"period": 2,
"cap": int64(60 * 1000),
})
typeOfPolicy = reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "RandomBackoffPolicy", typeOfPolicy.Elem().Name())
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "Exponential",
"period": 2,
"cap": int64(60 * 1000),
})
typeOfPolicy = reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "ExponentialBackoffPolicy", typeOfPolicy.Elem().Name())
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "EqualJitter",
"period": 2,
"cap": int64(60 * 1000),
})
typeOfPolicy = reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "EqualJitterBackoffPolicy", typeOfPolicy.Elem().Name())
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "ExponentialWithEqualJitter",
"period": 2,
"cap": int64(60 * 1000),
})
typeOfPolicy = reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "EqualJitterBackoffPolicy", typeOfPolicy.Elem().Name())
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "FullJitter",
"period": 2,
"cap": int64(60 * 1000),
})
typeOfPolicy = reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "FullJitterBackoffPolicy", typeOfPolicy.Elem().Name())
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "ExponentialWithFullJitter",
"period": 2,
"cap": int64(60 * 1000),
})
typeOfPolicy = reflect.TypeOf(backoffPolicy)
utils.AssertEqual(t, "FullJitterBackoffPolicy", typeOfPolicy.Elem().Name())
}

func TestFixedBackoffPolicy(t *testing.T) {
backoffPolicy := FixedBackoffPolicy{
Period: Int(1000),
}
utils.AssertEqual(t, int64(1000), Int64Value(backoffPolicy.GetDelayTimeMillis(nil)))
retryPolicyContext := RetryPolicyContext{
RetriesAttempted: Int(1),
}
utils.AssertEqual(t, int64(1000), Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)))
retryPolicyContext = RetryPolicyContext{
RetriesAttempted: Int(2),
}
utils.AssertEqual(t, int64(1000), Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)))
}

func TestRandomBackoffPolicy(t *testing.T) {
backoffPolicy := RandomBackoffPolicy{
Period: Int(2),
Cap: Int64(60 * 1000),
}
retryPolicyContext := RetryPolicyContext{
RetriesAttempted: Int(1),
}
utils.AssertEqual(t, true, Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)) < 2)
retryPolicyContext = RetryPolicyContext{
RetriesAttempted: Int(2),
}
utils.AssertEqual(t, true, Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)) < 4)
}

func TestExponentialBackoffPolicy(t *testing.T) {
backoffPolicy := ExponentialBackoffPolicy{
Period: Int(2),
Cap: Int64(60 * 1000),
}
retryPolicyContext := RetryPolicyContext{
RetriesAttempted: Int(1),
}
utils.AssertEqual(t, int64(4), Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)))
retryPolicyContext = RetryPolicyContext{
RetriesAttempted: Int(2),
}
utils.AssertEqual(t, int64(8), Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)))
}

func TestEqualJitterBackoffPolicy(t *testing.T) {
backoffPolicy := EqualJitterBackoffPolicy{
Period: Int(2),
Cap: Int64(60 * 1000),
}
retryPolicyContext := RetryPolicyContext{
RetriesAttempted: Int(1),
}
utils.AssertEqual(t, true, Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)) < 5)
retryPolicyContext = RetryPolicyContext{
RetriesAttempted: Int(2),
}
utils.AssertEqual(t, true, Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)) < 9)
}

func TestFullJitterBackoffPolicy(t *testing.T) {
backoffPolicy := FullJitterBackoffPolicy{
Period: Int(2),
Cap: Int64(60 * 1000),
}
retryPolicyContext := RetryPolicyContext{
RetriesAttempted: Int(1),
}
utils.AssertEqual(t, true, Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)) < 4)
retryPolicyContext = RetryPolicyContext{
RetriesAttempted: Int(2),
}
utils.AssertEqual(t, true, Int64Value(backoffPolicy.GetDelayTimeMillis(&retryPolicyContext)) < 8)
}
Loading

0 comments on commit 1ae3127

Please sign in to comment.