Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
feat: ExponentialBackOff support for API.
Browse files Browse the repository at this point in the history
  • Loading branch information
ipfans committed Dec 29, 2020
1 parent bf8d522 commit 276fe5e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sync"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/go-log/log"
"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
Expand All @@ -26,6 +27,7 @@ type Client struct {
logger log.Logger
storage TokenStorage
validator *validator.Validate
maxRetires uint64
}

// NewClient returns API client.
Expand All @@ -35,6 +37,7 @@ func NewClient(endpoint Endpoint, accessID, accessKey string, opts ...Option) (c
httpClient: &http.Client{Timeout: 10 * time.Second},
logger: log.DefaultLogger,
storage: &MemoryStore{},
maxRetires: 5,
}
for _, opt := range opts {
opt(conf)
Expand All @@ -43,6 +46,7 @@ func NewClient(endpoint Endpoint, accessID, accessKey string, opts ...Option) (c
c.logger = conf.logger
c.storage = conf.storage
c.validator = validator.New()
c.maxRetires = conf.maxRetires
return
}

Expand Down Expand Up @@ -127,7 +131,12 @@ func (c *Client) Parse(res *http.Response, resp interface{}) error {

// Do send HTTP request.
func (c *Client) Do(r *http.Request) (res *http.Response, err error) {
res, err = c.httpClient.Do(r)
// ExponentialBackOff support.
err = backoff.Retry(func() error {
var e error
res, e = c.httpClient.Do(r)
return e
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(), c.maxRetires))
return
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/yunjuiot/tuyacloud
go 1.14

require (
github.com/cenkalti/backoff/v4 v4.1.0
github.com/go-log/log v0.2.0
github.com/go-playground/validator/v10 v10.4.1
github.com/golang/mock v1.4.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-log/log v0.2.0 h1:z8i91GBudxD5L3RmF0KVpetCbcGWAV7q1Tw1eRwQM9Q=
Expand Down
9 changes: 9 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type options struct {
httpClient HTTPClient
logger log.Logger
storage TokenStorage
maxRetires uint64
}

// Option settings.
Expand All @@ -33,3 +34,11 @@ func WithTokenStore(s TokenStorage) Option {
o.storage = s
}
}

// WithMaxRetries setup max retries.
// Disable retries when i == 0.
func WithMaxRetries(i uint64) Option {
return func(o *options) {
o.maxRetires = i
}
}

0 comments on commit 276fe5e

Please sign in to comment.