From e07ffd6453ac06bdfeb3536f4dbce0742cd77189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Sun, 1 Sep 2024 16:56:44 +0200 Subject: [PATCH 1/2] Fix tests on 32bits arch --- limiter_atomic.go | 2 +- limiter_atomic_int64.go | 2 +- limiter_mutexbased.go | 2 +- ratelimit.go | 2 +- ratelimit_bench_test.go | 6 +++--- ratelimit_test.go | 14 +++++++------- 6 files changed, 14 insertions(+), 14 deletions(-) diff --git a/limiter_atomic.go b/limiter_atomic.go index ac6465c..ddb945e 100644 --- a/limiter_atomic.go +++ b/limiter_atomic.go @@ -44,7 +44,7 @@ type atomicLimiter struct { } // newAtomicBased returns a new atomic based limiter. -func newAtomicBased(rate int, opts ...Option) *atomicLimiter { +func newAtomicBased(rate int64, opts ...Option) *atomicLimiter { // TODO consider moving config building to the implementation // independent code. config := buildConfig(opts) diff --git a/limiter_atomic_int64.go b/limiter_atomic_int64.go index 8d370b7..75e4630 100644 --- a/limiter_atomic_int64.go +++ b/limiter_atomic_int64.go @@ -39,7 +39,7 @@ type atomicInt64Limiter struct { } // newAtomicBased returns a new atomic based limiter. -func newAtomicInt64Based(rate int, opts ...Option) *atomicInt64Limiter { +func newAtomicInt64Based(rate int64, opts ...Option) *atomicInt64Limiter { // TODO consider moving config building to the implementation // independent code. config := buildConfig(opts) diff --git a/limiter_mutexbased.go b/limiter_mutexbased.go index 7bd18f7..a1be896 100644 --- a/limiter_mutexbased.go +++ b/limiter_mutexbased.go @@ -35,7 +35,7 @@ type mutexLimiter struct { } // newMutexBased returns a new mutex based limiter. -func newMutexBased(rate int, opts ...Option) *mutexLimiter { +func newMutexBased(rate int64, opts ...Option) *mutexLimiter { // TODO consider moving config building to the implementation // independent code. config := buildConfig(opts) diff --git a/ratelimit.go b/ratelimit.go index 22b88ec..561deff 100644 --- a/ratelimit.go +++ b/ratelimit.go @@ -53,7 +53,7 @@ type config struct { } // New returns a Limiter that will limit to the given RPS. -func New(rate int, opts ...Option) Limiter { +func New(rate int64, opts ...Option) Limiter { return newAtomicInt64Based(rate, opts...) } diff --git a/ratelimit_bench_test.go b/ratelimit_bench_test.go index a1125c0..3ac4cf8 100644 --- a/ratelimit_bench_test.go +++ b/ratelimit_bench_test.go @@ -14,9 +14,9 @@ func BenchmarkRateLimiter(b *testing.B) { for _, procs := range []int{1, 4, 8, 16} { runtime.GOMAXPROCS(procs) for name, limiter := range map[string]Limiter{ - "atomic": newAtomicBased(b.N * 1000000000000), - "atomic_int64": newAtomicInt64Based(b.N * 1000000000000), - "mutex": newMutexBased(b.N * 1000000000000), + "atomic": newAtomicBased(int64(b.N * 1000000000000)), + "atomic_int64": newAtomicInt64Based(int64(b.N * 1000000000000)), + "mutex": newMutexBased(int64(b.N * 1000000000000)), } { for ng := 1; ng < 16; ng++ { runner(b, name, procs, ng, limiter, count) diff --git a/ratelimit_test.go b/ratelimit_test.go index ce81038..e5ef134 100644 --- a/ratelimit_test.go +++ b/ratelimit_test.go @@ -13,7 +13,7 @@ import ( type testRunner interface { // createLimiter builds a limiter with given options. - createLimiter(int, ...Option) Limiter + createLimiter(int64, ...Option) Limiter // takeOnceAfter attempts to Take at a specific time. takeOnceAfter(time.Duration, Limiter) // startTaking tries to Take() on passed in limiters in a loop/goroutine. @@ -32,7 +32,7 @@ type runnerImpl struct { t *testing.T clock *clock.Mock - constructor func(int, ...Option) Limiter + constructor func(int64, ...Option) Limiter count atomic.Int32 // maxDuration is the time we need to move into the future for a test. // It's populated automatically based on assertCountAt/afterFunc. @@ -44,23 +44,23 @@ type runnerImpl struct { func runTest(t *testing.T, fn func(testRunner)) { impls := []struct { name string - constructor func(int, ...Option) Limiter + constructor func(int64, ...Option) Limiter }{ { name: "mutex", - constructor: func(rate int, opts ...Option) Limiter { + constructor: func(rate int64, opts ...Option) Limiter { return newMutexBased(rate, opts...) }, }, { name: "atomic", - constructor: func(rate int, opts ...Option) Limiter { + constructor: func(rate int64, opts ...Option) Limiter { return newAtomicBased(rate, opts...) }, }, { name: "atomic_int64", - constructor: func(rate int, opts ...Option) Limiter { + constructor: func(rate int64, opts ...Option) Limiter { return newAtomicInt64Based(rate, opts...) }, }, @@ -88,7 +88,7 @@ func runTest(t *testing.T, fn func(testRunner)) { } // createLimiter builds a limiter with given options. -func (r *runnerImpl) createLimiter(rate int, opts ...Option) Limiter { +func (r *runnerImpl) createLimiter(rate int64, opts ...Option) Limiter { opts = append(opts, WithClock(r.clock)) return r.constructor(rate, opts...) } From 75fa5c1e1368311347cfbc787bcd9266b70122fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alo=C3=AFs=20Micard?= Date: Mon, 2 Sep 2024 07:44:37 +0200 Subject: [PATCH 2/2] Fix tests --- ratelimit_bench_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ratelimit_bench_test.go b/ratelimit_bench_test.go index 3ac4cf8..33a437e 100644 --- a/ratelimit_bench_test.go +++ b/ratelimit_bench_test.go @@ -14,9 +14,9 @@ func BenchmarkRateLimiter(b *testing.B) { for _, procs := range []int{1, 4, 8, 16} { runtime.GOMAXPROCS(procs) for name, limiter := range map[string]Limiter{ - "atomic": newAtomicBased(int64(b.N * 1000000000000)), - "atomic_int64": newAtomicInt64Based(int64(b.N * 1000000000000)), - "mutex": newMutexBased(int64(b.N * 1000000000000)), + "atomic": newAtomicBased(int64(b.N) * int64(1000000000000)), + "atomic_int64": newAtomicInt64Based(int64(b.N) * int64(1000000000000)), + "mutex": newMutexBased(int64(b.N) * int64(1000000000000)), } { for ng := 1; ng < 16; ng++ { runner(b, name, procs, ng, limiter, count)