Skip to content

Commit

Permalink
Adjust API limits and log rate limits
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Oct 29, 2023
1 parent 2d9dae0 commit f7f2b40
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions service/adapters/twitter/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import (
"github.com/planetary-social/nos-crossposting-service/service/domain/accounts"
)

const (
apiLimitWindow = 15 * time.Minute
apiLimitCreateTweet = 25 // docs claim 200 but it doesn't seem true at all
apiLimitGetUserDetails = 75
)

type Twitter struct {
conf config.Config
logger logging.Logger
Expand Down Expand Up @@ -55,7 +61,11 @@ func (t *Twitter) PostTweet(
Host: "https://api.twitter.com",
}

if err := t.limiter.Limit(fmt.Sprintf("create-tweet-%s", userAccessToken), 200, 15*time.Minute); err != nil {
if err := t.limiter.Limit(
fmt.Sprintf("create-tweet-%s", userAccessToken),
apiLimitCreateTweet,
apiLimitWindow,
); err != nil {
return errors.Wrap(err, "limiter error")
}

Expand Down Expand Up @@ -95,7 +105,11 @@ func (t *Twitter) GetAccountDetails(
Host: "https://api.twitter.com",
}

if err := t.limiter.Limit(fmt.Sprintf("user-lookup-%s", userAccessToken), 75, 15*time.Minute); err != nil {
if err := t.limiter.Limit(
fmt.Sprintf("user-lookup-%s", userAccessToken),
apiLimitGetUserDetails,
apiLimitWindow,
); err != nil {
return app.TwitterAccountDetails{}, errors.Wrap(err, "limiter error")
}

Expand Down Expand Up @@ -128,6 +142,9 @@ func (t *Twitter) logError(err error) {
WithField("title", errorResponse.Title).
WithField("detail", errorResponse.Detail).
WithField("type", errorResponse.Type).
WithField("rateLimit.limit", errorResponse.RateLimit.Limit).
WithField("rateLimit.reset", errorResponse.RateLimit.Reset).
WithField("rateLimit.remaining", errorResponse.RateLimit.Remaining).
Message("received an error response from twitter")
}
}
Expand Down

0 comments on commit f7f2b40

Please sign in to comment.