Skip to content

Commit eca7831

Browse files
kayrusjtopjian
andauthored
Client: introduce the retry func (honor 429 http code) (#144)
* Client: introduce the retry func (honor 429 http code) * Fix unit tests * Add context support (prerequisite for Terraform SDK v2) * Change the if condition order * Return real error on context cancel * Depend on gophercloud.RetryFunc type * Updating go.mod and go.sum * go mod tidy Co-authored-by: Joe Topjian <[email protected]>
1 parent 7e0b3b3 commit eca7831

File tree

5 files changed

+83
-26
lines changed

5 files changed

+83
-26
lines changed

client/client.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ package client
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
89
"io/ioutil"
910
"log"
1011
"net/http"
1112
"sort"
13+
"strconv"
1214
"strings"
15+
"time"
16+
17+
"github.com/gophercloud/gophercloud"
1318
)
1419

1520
// Logger is an interface representing the Logger struct
@@ -358,3 +363,42 @@ func FormatJSON(raw []byte) (string, error) {
358363

359364
return string(pretty), nil
360365
}
366+
367+
func RetryBackoffFunc(logger Logger) gophercloud.RetryFunc {
368+
return func(ctx context.Context, respErr *gophercloud.ErrUnexpectedResponseCode, e error, retries uint) error {
369+
retryAfter := respErr.ResponseHeader.Get("Retry-After")
370+
if retryAfter == "" {
371+
return e
372+
}
373+
374+
var sleep time.Duration
375+
376+
// Parse delay seconds or HTTP date
377+
if v, err := strconv.ParseUint(retryAfter, 10, 32); err == nil {
378+
sleep = time.Duration(v) * time.Second
379+
} else if v, err := time.Parse(http.TimeFormat, retryAfter); err == nil {
380+
sleep = time.Until(v)
381+
} else {
382+
return e
383+
}
384+
385+
l := logger
386+
if l != nil {
387+
l.Printf("Received StatusTooManyRequests response code sleeping for %s", sleep)
388+
}
389+
if c := ctx; c != nil {
390+
select {
391+
case <-time.After(sleep):
392+
case <-c.Done():
393+
if l != nil {
394+
l.Printf("Sleeping aborted: %w", c.Err())
395+
}
396+
return e
397+
}
398+
} else {
399+
time.Sleep(sleep)
400+
}
401+
402+
return nil
403+
}
404+
}

go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module github.com/gophercloud/utils
22

33
require (
4-
github.com/gophercloud/gophercloud v0.6.1-0.20191122030953-d8ac278c1c9d
5-
github.com/hashicorp/go-uuid v1.0.1
4+
github.com/gophercloud/gophercloud v0.15.1-0.20210202035223-633d73521055
5+
github.com/hashicorp/go-uuid v1.0.2
66
github.com/mitchellh/go-homedir v1.1.0
7-
gopkg.in/yaml.v2 v2.2.4
7+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68
8+
golang.org/x/text v0.3.0
9+
gopkg.in/yaml.v2 v2.4.0
810
)
911

10-
go 1.13
12+
go 1.15

go.sum

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
github.com/gophercloud/gophercloud v0.0.0-20190212181753-892256c46858 h1:QBK4YvYPJGsghVtVJcEZcQ70UNGFnWUCjtv2NTcEFHA=
2-
github.com/gophercloud/gophercloud v0.0.0-20190212181753-892256c46858/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
3-
github.com/gophercloud/gophercloud v0.6.0 h1:Xb2lcqZtml1XjgYZxbeayEemq7ASbeTp09m36gQFpEU=
4-
github.com/gophercloud/gophercloud v0.6.0/go.mod h1:GICNByuaEBibcjmjvI7QvYJSZEbGkcYwAR7EZK2WMqM=
5-
github.com/gophercloud/gophercloud v0.6.1-0.20191122030953-d8ac278c1c9d h1:r5dcOhiqucDq0XNmvsN+HMB7+o3TrjZlLdNYGFRtM3o=
6-
github.com/gophercloud/gophercloud v0.6.1-0.20191122030953-d8ac278c1c9d/go.mod h1:ozGNgr9KYOVATV5jsgHl/ceCDXGuguqOZAzoQ/2vcNM=
7-
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
8-
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
1+
github.com/gophercloud/gophercloud v0.15.1-0.20210202035223-633d73521055 h1:/wFA5WAzWcHNjpUs/Vuf4g2Sbv0wj3MUkZdeYgU4RkI=
2+
github.com/gophercloud/gophercloud v0.15.1-0.20210202035223-633d73521055/go.mod h1:wRtmUelyIIv3CSSDI47aUwbs075O6i+LY+pXsKCBsb4=
3+
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
4+
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
95
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
106
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
11-
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67 h1:ng3VDlRp5/DHpSWl02R4rM9I+8M2rhmsuLwAMmkLQWE=
12-
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
13-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
147
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
15-
golang.org/x/sys v0.0.0-20190209173611-3b5209105503 h1:5SvYFrOM3W8Mexn9/oA44Ji7vhXAZQ9hiP+1Q/DMrWg=
16-
golang.org/x/sys v0.0.0-20190209173611-3b5209105503/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
8+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
9+
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
10+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
1711
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
12+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
13+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
14+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15+
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
16+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
17+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
18+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
19+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1820
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1921
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
20-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
21-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
22-
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
23-
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
22+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
23+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
24+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

openstack/helpers/projectpurge.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,10 @@ func clearNetworkingRouters(projectID string, networkClient *gophercloud.Service
336336
return err
337337
}
338338

339+
routes := []routers.Route{}
339340
// Clear all routes
340341
updateOpts := routers.UpdateOpts{
341-
Routes: []routers.Route{},
342+
Routes: &routes,
342343
}
343344

344345
_, err := routers.Update(networkClient, router.ID, updateOpts).Extract()

terraform/auth/config.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"net/http"
@@ -45,6 +46,7 @@ type Config struct {
4546
UseOctavia bool
4647
MaxRetries int
4748
DisableNoCacheHeader bool
49+
Context context.Context
4850

4951
DelayedAuth bool
5052
AllowReauth bool
@@ -90,6 +92,10 @@ func (c *Config) LoadAndValidate() error {
9092
return fmt.Errorf("Invalid endpoint type provided")
9193
}
9294

95+
if c.MaxRetries < 0 {
96+
return fmt.Errorf("max_retries should be a positive value")
97+
}
98+
9399
clientOpts := new(clientconfig.ClientOpts)
94100

95101
// If a cloud entry was given, base AuthOptions on a clouds.yaml file.
@@ -157,6 +163,8 @@ func (c *Config) LoadAndValidate() error {
157163
return err
158164
}
159165

166+
client.Context = c.Context
167+
160168
// Set UserAgent
161169
client.UserAgent.Prepend(terraformUserAgent(c.TerraformVersion, c.SDKVersion))
162170

@@ -187,17 +195,18 @@ func (c *Config) LoadAndValidate() error {
187195
client.HTTPClient.Transport.(*osClient.RoundTripper).SetHeaders(extraHeaders)
188196
}
189197

198+
if c.MaxRetries > 0 {
199+
client.MaxBackoffRetries = uint(c.MaxRetries)
200+
client.RetryBackoffFunc = osClient.RetryBackoffFunc(logger)
201+
}
202+
190203
if !c.DelayedAuth && !c.Swauth {
191204
err = openstack.Authenticate(client, *ao)
192205
if err != nil {
193206
return err
194207
}
195208
}
196209

197-
if c.MaxRetries < 0 {
198-
return fmt.Errorf("max_retries should be a positive value")
199-
}
200-
201210
c.authOpts = ao
202211
c.OsClient = client
203212

0 commit comments

Comments
 (0)