Skip to content

Commit

Permalink
fix for limitsDontMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyagara committed Dec 12, 2023
1 parent 9b06f3d commit b8ae9de
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func main() {
- More tests for the internal client and rate limit
- Maybe the context usage throughout the project could be improved
- Maybe more options to customize the rate limiter
- Maybe allow for custom logger
- Exponential backoff for retries
- Improve DDragon/CDragon support

Expand Down
4 changes: 2 additions & 2 deletions ratelimit/limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (l *Limit) checkBuckets(ctx context.Context, logger zerolog.Logger, route a
}

// Checks if the limits given in the header match the current buckets.
func (l *Limit) limitsDontMatch(limitHeader string) bool {
func (l *Limit) limitsDontMatch(limitHeader string, limitOffset int) bool {
if limitHeader == "" {
return false
}
Expand All @@ -79,7 +79,7 @@ func (l *Limit) limitsDontMatch(limitHeader string) bool {
return true
}
limit, interval := getNumbersFromPair(pair)
if l.buckets[i].limit != limit || l.buckets[i].interval != time.Duration(interval)*time.Second {
if l.buckets[i].limit != limit-limitOffset || l.buckets[i].interval != time.Duration(interval)*time.Second {
return true
}
}
Expand Down
4 changes: 2 additions & 2 deletions ratelimit/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func (r *RateLimit) Update(logger zerolog.Logger, route any, methodID string, he
r.mutex.Lock()
defer r.mutex.Unlock()
limits := r.Region[route]
if limits.App.limitsDontMatch(headers.Get(APP_RATE_LIMIT_HEADER)) {
if limits.App.limitsDontMatch(headers.Get(APP_RATE_LIMIT_HEADER), r.LimitOffset) {
limits.App = r.parseHeaders(headers.Get(APP_RATE_LIMIT_HEADER), headers.Get(APP_RATE_LIMIT_COUNT_HEADER), APP_RATE_LIMIT_TYPE)
logger.Debug().Msg("New Application buckets")
}
if limits.Methods[methodID].limitsDontMatch(headers.Get(METHOD_RATE_LIMIT_HEADER)) {
if limits.Methods[methodID].limitsDontMatch(headers.Get(METHOD_RATE_LIMIT_HEADER), r.LimitOffset) {
limits.Methods[methodID] = r.parseHeaders(headers.Get(METHOD_RATE_LIMIT_HEADER), headers.Get(METHOD_RATE_LIMIT_COUNT_HEADER), METHOD_RATE_LIMIT_TYPE)
logger.Debug().Msg("New Method buckets")
}
Expand Down
20 changes: 11 additions & 9 deletions test/benchmark/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ import (

// This revealed problems with multiple limits in a bucket, only the first bucket was being respected.
//
// The only thing that really matters here is B/op and allocs/op.
// The only thing that really matters here for benchmark purposes is B/op and allocs/op.
/*
goos: linux - WSL2
goarch: amd64
pkg: github.com/Kyagara/equinox/test/benchmark
cpu: AMD Ryzen 7 2700 Eight-Core Processor
BenchmarkParallelRateLimit-16 100 60013967 ns/op 3216 B/op 35 allocs/op
BenchmarkParallelRateLimit-16 100 50023944 ns/op 3048 B/op 34 allocs/op
BenchmarkParallelRateLimit-16 100 50022594 ns/op 3112 B/op 34 allocs/op
BenchmarkParallelRateLimit-16 100 60005112 ns/op 3092 B/op 34 allocs/op
BenchmarkParallelRateLimit-16 100 60008024 ns/op 2788 B/op 33 allocs/op
BenchmarkParallelRateLimit-16 100 90047134 ns/op 3209 B/op 38 allocs/op
BenchmarkParallelRateLimit-16 100 90036735 ns/op 2983 B/op 37 allocs/op
BenchmarkParallelRateLimit-16 100 90033642 ns/op 3161 B/op 37 allocs/op
BenchmarkParallelRateLimit-16 100 90035373 ns/op 3055 B/op 37 allocs/op
BenchmarkParallelRateLimit-16 100 90035765 ns/op 3156 B/op 37 allocs/op
*/
func BenchmarkParallelRateLimit(b *testing.B) {
b.ReportAllocs()
Expand All @@ -37,14 +37,16 @@ func BenchmarkParallelRateLimit(b *testing.B) {

httpmock.RegisterResponder("GET", "https://br1.api.riotgames.com/lol/summoner/v4/summoners/by-puuid/puuid",
httpmock.NewBytesResponder(200, util.ReadFile(b, "../data/summoner.json")).HeaderSet(http.Header{
ratelimit.APP_RATE_LIMIT_HEADER: {"20:1,50:3,80:5"},
ratelimit.APP_RATE_LIMIT_COUNT_HEADER: {"1:1,1:3,1:5"},
ratelimit.APP_RATE_LIMIT_HEADER: {"20:1,40:4"},
ratelimit.APP_RATE_LIMIT_COUNT_HEADER: {"1:1,1:4"},
ratelimit.METHOD_RATE_LIMIT_HEADER: {"1300:60"},
ratelimit.METHOD_RATE_LIMIT_COUNT_HEADER: {"1:60"},
}))

config := util.NewTestEquinoxConfig()
config.LogLevel = zerolog.WarnLevel
config.Retries = 3
config.RateLimit = ratelimit.NewInternalRateLimit(0, 0.5)
config.RateLimit = ratelimit.NewInternalRateLimit(1, 0.5)

client := equinox.NewClientWithConfig(config)

Expand Down

0 comments on commit b8ae9de

Please sign in to comment.