Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obey X-Rate-Limit-Reset header #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"

"strconv"

"github.com/pkg/errors"
"github.com/sethgrid/pester"
"github.com/hashicorp/go-retryablehttp"
)

type DefaultClient struct {
Expand All @@ -23,23 +22,31 @@ var pClient Client

const defaultEndpoint = "https://papertrailapp.com/api/v1"

const PesterRetries = 50
func MakeRestClient() *http.Client {
client := retryablehttp.NewClient()

func MakeRestClient() *pester.Client {
client := pester.New()
client.RetryMax = 50
client.RetryWaitMax = 60 * time.Second
client.ResponseLogHook = func(logger retryablehttp.Logger, res *http.Response) {
logger.Printf("%+v\n", res)
}
client.Backoff = papertrailBackoff

client.KeepLog = false
client.Concurrency = 1
client.MaxRetries = PesterRetries
client.Timeout = 5 * time.Second
return client.StandardClient()
}

// Use a LinearJitter Backoff algorithm.
client.Backoff = pester.LinearJitterBackoff
client.LogHook = func(e pester.ErrEntry) {
log.Printf(client.FormatError(e))
func papertrailBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
if resp != nil {
if resp.StatusCode == http.StatusTooManyRequests {
if s, ok := resp.Header["X-Rate-Limit-Reset"]; ok {
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
return time.Second * time.Duration(sleep)
}
}
}
}

return client
return retryablehttp.LinearJitterBackoff(min, max, attemptNum, resp)
}

func NewClient(token string) Client {
Expand Down Expand Up @@ -162,7 +169,7 @@ func (c *DefaultClient) ListGroups() ([]Group, error) {
func (c *DefaultClient) UpdateGroup(g Group) error {
params := parseGroupParams(g)
var out interface{}
path := fmt.Sprintf("/groups/%s", g.ID)
path := fmt.Sprintf("/groups/%d", g.ID)

return c.execute("PUT", path, params, &out)
}
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module github.com/reproio/goptrail

go 1.15

require (
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/pkg/errors v0.9.1
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=