Skip to content

Commit

Permalink
config: introduce the retry func (honor 429 http code) (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiChangkuo authored Jul 8, 2021
1 parent 38f88d8 commit f742753
Show file tree
Hide file tree
Showing 23 changed files with 326 additions and 135 deletions.
30 changes: 30 additions & 0 deletions flexibleengine/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package flexibleengine

import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"math"
"net/http"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand Down Expand Up @@ -109,6 +112,28 @@ func generateTLSConfig(c *Config) (*tls.Config, error) {
return config, nil
}

func retryBackoffFunc(ctx context.Context, respErr *golangsdk.ErrUnexpectedResponseCode, e error, retries uint) error {
minutes := int(math.Pow(2, float64(retries)))
if minutes > 30 { // won't wait more than 30 minutes
minutes = 30
}

log.Printf("[WARN] Received StatusTooManyRequests response code, try to sleep %d minutes", minutes)
sleep := time.Duration(minutes) * time.Minute

if ctx != nil {
select {
case <-time.After(sleep):
case <-ctx.Done():
return e
}
} else {
time.Sleep(sleep)
}

return nil
}

func genClient(c *Config, ao golangsdk.AuthOptionsProvider) (*golangsdk.ProviderClient, error) {
client, err := huaweisdk.NewClient(ao.GetIdentityEndpoint())
if err != nil {
Expand Down Expand Up @@ -141,6 +166,11 @@ func genClient(c *Config, ao golangsdk.AuthOptionsProvider) (*golangsdk.Provider
},
}

if c.MaxRetries > 0 {
client.MaxBackoffRetries = uint(c.MaxRetries)
client.RetryBackoffFunc = retryBackoffFunc
}

// Validate authentication normally.
err = huaweisdk.Authenticate(client, ao)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/huaweicloud/golangsdk v0.0.0-20210625084017-1140ae646b73
github.com/huaweicloud/golangsdk v0.0.0-20210708072926-e14b0cd78758
github.com/huaweicloud/terraform-provider-huaweicloud v1.25.1-0.20210621022651-b1aa07d8ce12
github.com/jen20/awspolicyequivalence v1.1.0
github.com/jtolds/gls v4.20.0+incompatible // indirect
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKe
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20210618111817-9c67059b7682/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210619075926-bc59084e8485 h1:xeJzaCvFOm/myoJJ1EDESXJmuXHCBrK999U5kbyP3AE=
github.com/huaweicloud/golangsdk v0.0.0-20210619075926-bc59084e8485/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210621115823-3cecb9fc9172 h1:6OLBFIxdht5gTP3AVkKM4CngryJSwRrFGX43xqAPAu4=
github.com/huaweicloud/golangsdk v0.0.0-20210621115823-3cecb9fc9172/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210624022515-eefdf1d43b21 h1:S6MdycKYfaCv/e4+xsIiDQE1HsZ3RqsrqKgCq8/3keg=
github.com/huaweicloud/golangsdk v0.0.0-20210624022515-eefdf1d43b21/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210625084017-1140ae646b73 h1:kniJvk+WbEk38yRkDFMRmm1bCaZXPzBa1jkGkePfMvU=
github.com/huaweicloud/golangsdk v0.0.0-20210625084017-1140ae646b73/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210708072926-e14b0cd78758 h1:uvUKatTwob3RtG5b3jD3pJoqBbC+uiAwn8zDOXvSxcU=
github.com/huaweicloud/golangsdk v0.0.0-20210708072926-e14b0cd78758/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/terraform-provider-huaweicloud v1.25.1-0.20210621022651-b1aa07d8ce12 h1:Gnt3/6dft3Q5tKyKMThZdmzdDtkP85diIBcrvgcs2vg=
github.com/huaweicloud/terraform-provider-huaweicloud v1.25.1-0.20210621022651-b1aa07d8ce12/go.mod h1:SPfV6A6ZdqeIaqnPOVMomuMdVjjnzA0pzdALxcR5wXY=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
30 changes: 30 additions & 0 deletions vendor/github.com/huaweicloud/golangsdk/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions vendor/github.com/huaweicloud/golangsdk/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions vendor/github.com/huaweicloud/golangsdk/openstack/endpoint_util.go

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions vendor/github.com/huaweicloud/golangsdk/pagination/http.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 29 additions & 8 deletions vendor/github.com/huaweicloud/golangsdk/params.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f742753

Please sign in to comment.