You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Based on my understanding, here should be how it would go if we use now?
Example (interval at 60 seconds):
t=0: refilledAt=0
t=90: should refill 1 interval and set refilledAt=60 (but code sets it to 90)
t=150: should refill 1 interval and set refilledAt=120 (but code sets it to 150)
Also, shouldn't we set the EXPIRES for the keys , so the values won't stay in Redis if it not used?
I think the final script should look something like this:
and lastly , While this is not redis doc , I think having more instruction (init cilents , how to load the script) for it would help for someone new like me since as right now , we don't have rate limit example with redis yet to follow
for example here how i do mine :
// redis.tsimport{env}from"@/env";import{createClient}from"@redis/client";constclient=createClient({url: env.REDIS_URL,});client.on("error",(err)=>{console.error("❌ Redis Client Error:",err);});client.on("connect",()=>{console.log("🔄 Redis client attempting to connect...");});process.on("SIGINT",async()=>{console.log("🛑 Shutting down Redis connection...");awaitclient.quit();process.exit();});client.connect().then(()=>{console.log("✅ Redis client connected");});export{client};
and script :
//script.tsexportconsttokenBucketScript=`-- Returns 1 if allowed, 0 if notlocal key = KEYS[1]local max = tonumber(ARGV[1])local refillIntervalSeconds = tonumber(ARGV[2])local cost = tonumber(ARGV[3])local now = tonumber(ARGV[4]) local ttlSeconds = tonumber(ARGV[5])local fields = redis.call("HGETALL", key)if #fields == 0 then redis.call("HSET", key, "count", max - cost, "refilled_at", now) redis.call("EXPIRE", key, ttlSeconds) return {1}endlocal count = 0local refilledAt = 0for i = 1, #fields, 2 do if fields[i] == "count" then count = tonumber(fields[i+1]) elseif fields[i] == "refilled_at" then refilledAt = tonumber(fields[i+1]) endendlocal refill = math.floor((now - refilledAt) / refillIntervalSeconds)count = math.min(count + refill, max)refilledAt = nowif count < cost then return {0}endcount = count - costredis.call("HSET", key, "count", count, "refilled_at", now)redis.call("EXPIRE", key, ttlSeconds) return {1}`;exportconstTOKEN_BUCKET_SHA: Readonly<string>=awaitclient.scriptLoad(tokenBucketScript);
https://lucia-auth.com/rate-limit/token-bucket
refilledAt
should be:and not
Based on my understanding, here should be how it would go if we use
now
?Example (interval at 60 seconds):
Also, shouldn't we set the EXPIRES for the keys , so the values won't stay in Redis if it not used?
I think the final script should look something like this:
and lastly , While this is not redis doc , I think having more instruction (init cilents , how to load the script) for it would help for someone new like me since as right now , we don't have rate limit example with redis yet to follow
for example here how i do mine :
and script :
which then I load using
I am willing to open a PR for this
The text was updated successfully, but these errors were encountered: