-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,308 additions
and
603 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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) | ||
} | ||
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) | ||
} | ||
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) | ||
} | ||
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) | ||
} | ||
ceil := math.Min(float64(*fullJitterBackof.Cap), float64(IntValue(fullJitterBackof.Period))*math.Pow(2.0, float64(IntValue(ctx.RetriesAttempted)))) | ||
return Int64(int64(rand.Float64() * ceil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.