Skip to main content
Deno 2 is finally here 🎉️
Learn more
Latest
variable slidingWindowRemainingTokensScript
import { slidingWindowRemainingTokensScript } from "https://deno.land/x/upstash_ratelimit@v2.0.5-canary/src/lua-scripts/multi.ts";

type

` local currentKey = KEYS[1] -- identifier including prefixes local previousKey = KEYS[2] -- key of the previous bucket local now = ARGV[1] -- current timestamp in milliseconds local window = ARGV[2] -- interval in milliseconds local currentFields = redis.call("HGETALL", currentKey) local requestsInCurrentWindow = 0 for i = 2, #currentFields, 2 do requestsInCurrentWindow = requestsInCurrentWindow + tonumber(currentFields[i]) end local previousFields = redis.call("HGETALL", previousKey) local requestsInPreviousWindow = 0 for i = 2, #previousFields, 2 do requestsInPreviousWindow = requestsInPreviousWindow + tonumber(previousFields[i]) end local percentageInCurrent = ( now % window) / window requestsInPreviousWindow = math.floor(( 1 - percentageInCurrent ) * requestsInPreviousWindow) return requestsInCurrentWindow + requestsInPreviousWindow `