Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Latest
variable slidingWindowRemainingTokensScript
import { slidingWindowRemainingTokensScript } from "https://deno.land/x/upstash_ratelimit@v1.2.1/src/lua-scripts/single.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 requestsInCurrentWindow = redis.call("GET", currentKey) if requestsInCurrentWindow == false then requestsInCurrentWindow = 0 end local requestsInPreviousWindow = redis.call("GET", previousKey) if requestsInPreviousWindow == false then requestsInPreviousWindow = 0 end local percentageInCurrent = ( now % window ) / window -- weighted requests to consider from the previous window requestsInPreviousWindow = math.floor(( 1 - percentageInCurrent ) * requestsInPreviousWindow) return requestsInPreviousWindow + requestsInCurrentWindow `