Comments (5)
Hi,
This looks like a bug in Lua script - https://github.com/go-redis/redis_rate/blob/v9/lua.go . It is not immediately clear what is wrong there...
from redis_rate.
Hi,
I've did this change:
diff --git a/lua.go b/lua.go
index 00a1177..dc6c1f7 100644
--- a/lua.go
+++ b/lua.go
@@ -43,7 +43,7 @@ local new_tat = tat + increment
local allow_at = new_tat - burst_offset
local diff = now - allow_at
-local remaining = math.floor(diff / emission_interval + 0.5)
+local remaining = math.floor(diff / emission_interval)
if remaining < 0 then
local reset_after = tat - now
And now it seems to report fine. However, I'm not sure why this +0.5
was there, so it may brake some other stuff.
2020/11/12 12:02:24 -1ns
2020/11/12 12:02:24 9.799548998s
2020/11/12 12:02:25 9.598224997s
2020/11/12 12:02:25 9.397761002s
2020/11/12 12:02:25 9.194830998s
2020/11/12 12:02:25 8.991724997s
2020/11/12 12:02:25 8.791387006s
2020/11/12 12:02:26 8.590687006s
2020/11/12 12:02:26 8.388626009s
2020/11/12 12:02:26 8.187648996s
2020/11/12 12:02:26 7.984241008s
2020/11/12 12:02:26 7.779868006s
2020/11/12 12:02:27 7.579449996s
2020/11/12 12:02:27 7.379165008s
2020/11/12 12:02:27 7.178017005s
2020/11/12 12:02:27 6.976421996s
2020/11/12 12:02:27 6.775869995s
2020/11/12 12:02:28 6.573429003s
2020/11/12 12:02:28 6.372973009s
2020/11/12 12:02:28 6.171716004s
2020/11/12 12:02:28 5.971336007s
2020/11/12 12:02:28 5.769253998s
2020/11/12 12:02:29 5.568884s
2020/11/12 12:02:29 5.367034003s
2020/11/12 12:02:29 5.166488006s
2020/11/12 12:02:29 4.966213002s
2020/11/12 12:02:29 4.765294s
2020/11/12 12:02:30 4.565043002s
2020/11/12 12:02:30 4.364315003s
2020/11/12 12:02:30 4.163448005s
2020/11/12 12:02:30 3.962646007s
2020/11/12 12:02:30 3.761032998s
2020/11/12 12:02:31 3.560769006s
2020/11/12 12:02:31 3.357135996s
2020/11/12 12:02:31 3.156644999s
2020/11/12 12:02:31 2.954966008s
2020/11/12 12:02:31 2.753356009s
2020/11/12 12:02:32 2.549777001s
2020/11/12 12:02:32 2.349068999s
2020/11/12 12:02:32 2.148274004s
2020/11/12 12:02:32 1.946217s
2020/11/12 12:02:32 1.744691997s
2020/11/12 12:02:33 1.544281005s
2020/11/12 12:02:33 1.340514004s
2020/11/12 12:02:33 1.140128999s
2020/11/12 12:02:33 939.062997ms
2020/11/12 12:02:33 738.867998ms
2020/11/12 12:02:34 538.006007ms
2020/11/12 12:02:34 337.228ms
2020/11/12 12:02:34 136.642009ms
Last allowed since: 10.065357881
2020/11/12 12:02:34 -1ns
2020/11/12 12:02:34 9.795255005s
2020/11/12 12:02:35 9.593696996s
2020/11/12 12:02:35 9.389268994s
2020/11/12 12:02:35 9.187741994s
2020/11/12 12:02:35 8.983551993s
2020/11/12 12:02:35 8.782241001s
2020/11/12 12:02:36 8.581724002s
2020/11/12 12:02:36 8.377003997s
2020/11/12 12:02:36 8.172646s
2020/11/12 12:02:36 7.971110999s
2020/11/12 12:02:36 7.768710002s
2020/11/12 12:02:37 7.565398991s
2020/11/12 12:02:37 7.365051999s
2020/11/12 12:02:37 7.162248998s
2020/11/12 12:02:37 6.958739995s
2020/11/12 12:02:37 6.757247999s
2020/11/12 12:02:38 6.555720001s
2020/11/12 12:02:38 6.352163001s
2020/11/12 12:02:38 6.151698991s
2020/11/12 12:02:38 5.946638002s
2020/11/12 12:02:38 5.745154991s
2020/11/12 12:02:39 5.543387994s
2020/11/12 12:02:39 5.341874003s
2020/11/12 12:02:39 5.140425994s
2020/11/12 12:02:39 4.940015003s
2020/11/12 12:02:39 4.739174991s
2020/11/12 12:02:40 4.538375005s
2020/11/12 12:02:40 4.333557993s
2020/11/12 12:02:40 4.132116004s
2020/11/12 12:02:40 3.929347991s
2020/11/12 12:02:40 3.727843001s
2020/11/12 12:02:41 3.525530993s
2020/11/12 12:02:41 3.321722999s
2020/11/12 12:02:41 3.120489001s
2020/11/12 12:02:41 2.918787002s
2020/11/12 12:02:41 2.718363001s
2020/11/12 12:02:42 2.517773002s
2020/11/12 12:02:42 2.315688997s
2020/11/12 12:02:42 2.115105003s
2020/11/12 12:02:42 1.914551004s
2020/11/12 12:02:42 1.714140996s
2020/11/12 12:02:43 1.513627991s
2020/11/12 12:02:43 1.310731992s
2020/11/12 12:02:43 1.108968004s
2020/11/12 12:02:43 907.801002ms
2020/11/12 12:02:43 704.115003ms
2020/11/12 12:02:44 501.938998ms
2020/11/12 12:02:44 297.913998ms
2020/11/12 12:02:44 95.117002ms
Last allowed since: 10.109563657
2020/11/12 12:02:44 -1ns
2020/11/12 12:02:44 9.797523006s
from redis_rate.
Good find!
I'm not sure why this +0.5 was there
Me neither...
so it may brake some other stuff.
Can you come up with a test case? It should be fine to change this code as long as we can make sure it is tested.
from redis_rate.
@vmihailenco, what is your opinion on the test - is it fine to use time.Sleep
or I should implement some polling mechanism?
This one fails with the current lua script implementation but succeeds when the rounding is removed.
I don't like the time.Sleep
s, though.
func TestAllow_Remaining(t *testing.T) {
ctx := context.Background()
l := rateLimiter()
limit := redis_rate.Limit{
Rate: 1,
Period: 100 * time.Millisecond,
Burst: 1,
}
res, err := l.Allow(ctx, "test_id", limit)
assert.Nil(t, err)
assert.Equal(t, res.Allowed, 1)
assert.Equal(t, res.Remaining, 0)
assert.Equal(t, res.RetryAfter, time.Duration(-1))
assert.InDelta(t, res.ResetAfter, 100*time.Millisecond, float64(10*time.Millisecond))
res, err = l.Allow(ctx, "test_id", limit)
assert.Nil(t, err)
assert.Equal(t, res.Allowed, 0)
assert.Equal(t, res.Remaining, 0)
assert.InDelta(t, res.RetryAfter, 100*time.Millisecond, float64(10*time.Millisecond))
assert.InDelta(t, res.ResetAfter, 100*time.Millisecond, float64(10*time.Millisecond))
// Wait for 2/3 of the RetryAfter time to pass.
time.Sleep((res.RetryAfter * 2) / 3)
res, err = l.Allow(ctx, "test_id", limit)
assert.Equal(t, res.Allowed, 0)
time.Sleep(res.RetryAfter * 3)
res, err = l.Allow(ctx, "test_id", limit)
assert.Equal(t, res.Allowed, 1)
assert.Equal(t, res.RetryAfter, time.Duration(-1))
}
from redis_rate.
Thanks! This should be fixed by 19dc8f9 and 78e4497
from redis_rate.
Related Issues (20)
- Initialize the limit HOT 1
- WaitN
- How to retrieve the current state (peek)?
- how to clear old keys? HOT 1
- Dependency Dashboard
- questions about allown and allownMost
- Latency benchmarks
- Rate limiter not working as expected HOT 4
- Support go-redis v9? HOT 4
- Export rediser interface
- Problem with package "go-redis/redis/v10" vs "redis/go-redis/v10" HOT 1
- redis_rate.PerDay() ?? HOT 3
- dose lua script length limit?
- v is nil,occured pannic error HOT 1
- Delay returned by Allow* methods not explained
- why burst_offset is neccessary HOT 1
- Rate and Burst configurations are not clear HOT 1
- ERR bad lua script for redis cluster, all the keys that the script uses should be passed using the KEYS array, and KEYS should not be in expression
- change rate on the fly
- is it concurrency safe?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from redis_rate.