Skip to content

Commit

Permalink
[v2.x]Rebuild retry policy and support HttpClient interface
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Jun 5, 2024
1 parent bb7ae5f commit e6e537a
Show file tree
Hide file tree
Showing 18 changed files with 2,038 additions and 651 deletions.
35 changes: 35 additions & 0 deletions darafile/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"scope": "darabonba",
"name": "HttpClient",
"version": "0.0.1",
"main": "./main.dara",
"maintainers": [
{
"name": "Alibaba Cloud SDK",
"email": "[email protected]"
}
],
"libraries": {
},
"releases": {
"go": "github.com/alibabacloud-go/tea/tea:v1.2.3-0.20240528133401-8788f78f9c46"
},
"go": {
"interface": true,
"clientName": "HttpClient",
"typedef": {
"HttpRequest": {
"import": "net/http",
"type": "http.Request"
},
"HttpResponse": {
"import": "net/http",
"type": "http.Response"
},
"HttpTransport": {
"import": "net/http",
"type": "http.Transport"
}
}
}
}
8 changes: 8 additions & 0 deletions darafile/main.dara
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
typedef HttpRequest;
typedef HttpResponse;
typedef HttpTransport;

init(){
}

async function call(request: HttpRequest, transport: HttpTransport): HttpResponse;
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ require (
github.com/alibabacloud-go/debug v1.0.0
github.com/json-iterator/go v1.1.12
github.com/modern-go/reflect2 v1.0.2
golang.org/x/net v0.20.0
golang.org/x/net v0.22.0
)
17 changes: 9 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
github.com/alibabacloud-go/debug v1.0.0 h1:3eIEQWfay1fB24PQIEzXAswlVJtdQok8f3EVN5VrBnA=
github.com/alibabacloud-go/debug v1.0.0/go.mod h1:8gfgZCCAC3+SCzjWtY053FrOcd4/qlH6IHTI4QyICOc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -10,24 +9,24 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -38,12 +37,14 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
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
}

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))
}
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/utils"
)

func TestBackoffPolicy(t *testing.T) {
var backoffPolicy BackoffPolicy
backoffPolicy = NewBackoffPolicy(map[string]interface{}{
"policy": "Any",
})
utils.AssertEqual(t, nil, 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),
}
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),
}
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),
}
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),
}
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 e6e537a

Please sign in to comment.