Skip to content

Commit

Permalink
fix: increase max rate limit usage
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Jul 30, 2021
1 parent 5b0aea6 commit 7907ad0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (

// Config configuration.
type Config struct {
RedisURL string `env:"REDIS_URL" envDefault:"redis://localhost:6379"`
GitHubTokens []string `env:"GITHUB_TOKENS"`
GitHubPageSize int `env:"GITHUB_PAGE_SIZE" envDefault:"100"`
Port string `env:"PORT" envDefault:"3000"`
RedisURL string `env:"REDIS_URL" envDefault:"redis://localhost:6379"`
GitHubTokens []string `env:"GITHUB_TOKENS"`
GitHubPageSize int `env:"GITHUB_PAGE_SIZE" envDefault:"100"`
GitHubMaxRateUsagePct int `env:"GITHUB_MAX_RATE_LIMIT_USAGE" envDefault:"80"`
Port string `env:"PORT" envDefault:"3000"`
}

// Get the current Config.
Expand Down
11 changes: 8 additions & 3 deletions internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type GitHub struct {
tokens roundrobin.RoundRobiner
pageSize int
cache *cache.Redis
maxRateUsagePct int
}

var rateLimits = prometheus.NewCounter(prometheus.CounterOpts{
Expand Down Expand Up @@ -131,10 +132,14 @@ func (gh *GitHub) checkToken(token *roundrobin.Token) error {
rate := limit.Rate
log.Debugf("%s rate %d/%d", token, rate.Remaining, rate.Limit)
rateLimiters.WithLabelValues(token.String()).Set(float64(rate.Remaining))
if rate.Remaining > rate.Limit/2 {
return nil // allow at most 50% rate limit usage
if isAboveTargetUsage(rate, gh.maxRateUsagePct) {
return fmt.Errorf("token usage is too high: %d/%d", rate.Remaining, rate.Limit)
}
return fmt.Errorf("token usage is too high: %d/%d", rate.Remaining, rate.Limit)
return nil // allow at most x% rate limit usage
}

func isAboveTargetUsage(rate rate, target int) bool {
return rate.Remaining*100 / rate.Limit < target
}

type rateLimit struct {
Expand Down
31 changes: 31 additions & 0 deletions internal/github/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package github

import (
"testing"

"github.com/matryer/is"
)

func TestIsRateAboveLimit(t *testing.T) {
is := is.New(t)

is.Equal(false, isAboveTargetUsage(rate{
Remaining: 4000,
Limit: 5000,
}, 50))

is.Equal(false, isAboveTargetUsage(rate{
Remaining: 2500,
Limit: 5000,
}, 50))

is.Equal(true, isAboveTargetUsage(rate{
Remaining: 2499,
Limit: 5000,
}, 50))

is.Equal(true, isAboveTargetUsage(rate{
Remaining: 500,
Limit: 5000,
}, 80))
}

1 comment on commit 7907ad0

@caarlos0
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refs #125

Please sign in to comment.