export const fixedWindowLimitScript = `#!lua flags=allow-key-locking local key = KEYS[1] local dynamicLimitKey = KEYS[2] -- optional: key for dynamic limit in redis local tokens = tonumber(ARGV[1]) -- default limit local window = ARGV[2] local incrementBy = ARGV[3] -- increment rate per request at a given value, default is 1 -- Check for dynamic limit local effectiveLimit = tokens if dynamicLimitKey and dynamicLimitKey ~= "" then local dynamicLimit = redis.call("GET", dynamicLimitKey) if dynamicLimit then effectiveLimit = tonumber(dynamicLimit) end end local r = redis.call("INCRBY", key, incrementBy) if r == tonumber(incrementBy) then -- The first time this key is set, the value will be equal to incrementBy. -- So we only need the expire command once redis.call("PEXPIRE", key, window) end return {r, effectiveLimit} `; export const fixedWindowRemainingTokensScript = `#!lua flags=allow-key-locking local key = KEYS[1] local dynamicLimitKey = KEYS[2] -- optional: key for dynamic limit in redis local tokens = tonumber(ARGV[1]) -- default limit -- Check for dynamic limit local effectiveLimit = tokens if dynamicLimitKey and dynamicLimitKey ~= "" then local dynamicLimit = redis.call("GET", dynamicLimitKey) if dynamicLimit then effectiveLimit = tonumber(dynamicLimit) end end local value = redis.call('GET', key) local usedTokens = 0 if value then usedTokens = tonumber(value) end return {effectiveLimit - usedTokens, effectiveLimit} `; export const slidingWindowLimitScript = `#!lua flags=allow-key-locking local currentKey = KEYS[1] -- identifier including prefixes local previousKey = KEYS[2] -- key of the previous bucket local dynamicLimitKey = KEYS[3] -- optional: key for dynamic limit in redis local tokens = tonumber(ARGV[1]) -- default tokens per window local now = ARGV[2] -- current timestamp in milliseconds local window = ARGV[3] -- interval in milliseconds local incrementBy = tonumber(ARGV[4]) -- increment rate per request at a given value, default is 1 -- Check for dynamic limit local effectiveLimit = tokens if dynamicLimitKey and dynamicLimitKey ~= "" then local dynamicLimit = redis.call("GET", dynamicLimitKey) if dynamicLimit then effectiveLimit = tonumber(dynamicLimit) end end 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) -- Only check limit if not refunding (negative rate) if incrementBy > 0 and requestsInPreviousWindow + requestsInCurrentWindow >= effectiveLimit then return {-1, effectiveLimit} end local newValue = redis.call("INCRBY", currentKey, incrementBy) if newValue == incrementBy then -- The first time this key is set, the value will be equal to incrementBy. -- So we only need the expire command once redis.call("PEXPIRE", currentKey, window * 2 + 1000) -- Enough time to overlap with a new window + 1 second end return {effectiveLimit - ( newValue + requestsInPreviousWindow ), effectiveLimit} `; export const slidingWindowRemainingTokensScript = `#!lua flags=allow-key-locking local currentKey = KEYS[1] -- identifier including prefixes local previousKey = KEYS[2] -- key of the previous bucket local dynamicLimitKey = KEYS[3] -- optional: key for dynamic limit in redis local tokens = tonumber(ARGV[1]) -- default tokens per window local now = ARGV[2] -- current timestamp in milliseconds local window = ARGV[3] -- interval in milliseconds -- Check for dynamic limit local effectiveLimit = tokens if dynamicLimitKey and dynamicLimitKey ~= "" then local dynamicLimit = redis.call("GET", dynamicLimitKey) if dynamicLimit then effectiveLimit = tonumber(dynamicLimit) end end 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) local usedTokens = requestsInPreviousWindow + requestsInCurrentWindow return {effectiveLimit - usedTokens, effectiveLimit} `; export const tokenBucketLimitScript = `#!lua flags=allow-key-locking local key = KEYS[1] -- identifier including prefixes local dynamicLimitKey = KEYS[2] -- optional: key for dynamic limit in redis local maxTokens = tonumber(ARGV[1]) -- default maximum number of tokens local interval = tonumber(ARGV[2]) -- size of the window in milliseconds local refillRate = tonumber(ARGV[3]) -- how many tokens are refilled after each interval local now = tonumber(ARGV[4]) -- current timestamp in milliseconds local incrementBy = tonumber(ARGV[5]) -- how many tokens to consume, default is 1 -- Check for dynamic limit local effectiveLimit = maxTokens if dynamicLimitKey and dynamicLimitKey ~= "" then local dynamicLimit = redis.call("GET", dynamicLimitKey) if dynamicLimit then effectiveLimit = tonumber(dynamicLimit) end end local bucket = redis.call("HMGET", key, "refilledAt", "tokens") local refilledAt local tokens if bucket[1] == false then refilledAt = now tokens = effectiveLimit else refilledAt = tonumber(bucket[1]) tokens = tonumber(bucket[2]) end if now >= refilledAt + interval then local numRefills = math.floor((now - refilledAt) / interval) tokens = math.min(effectiveLimit, tokens + numRefills * refillRate) refilledAt = refilledAt + numRefills * interval end -- Only reject when consuming (not refunding) and there are not enough tokens -- for this request. Checking tokens == 0 only covered incrementBy == 1: with -- a larger rate (e.g. 2 tokens left, incrementBy 5) the request fell through, -- drove tokens negative and still wrote it back, so a rejected request -- consumed tokens and a single large rate could lock the identifier out. if incrementBy > 0 and tokens < incrementBy then return {-1, refilledAt + interval, effectiveLimit} end local remaining = tokens - incrementBy local expireAt = math.ceil(((effectiveLimit - remaining) / refillRate)) * interval redis.call("HSET", key, "refilledAt", refilledAt, "tokens", remaining) if (expireAt > 0) then redis.call("PEXPIRE", key, expireAt) end return {remaining, refilledAt + interval, effectiveLimit} `; export const tokenBucketIdentifierNotFound = -1 export const tokenBucketRemainingTokensScript = `#!lua flags=allow-key-locking local key = KEYS[1] local dynamicLimitKey = KEYS[2] -- optional: key for dynamic limit in redis local maxTokens = tonumber(ARGV[1]) -- default maximum number of tokens -- Check for dynamic limit local effectiveLimit = maxTokens if dynamicLimitKey and dynamicLimitKey ~= "" then local dynamicLimit = redis.call("GET", dynamicLimitKey) if dynamicLimit then effectiveLimit = tonumber(dynamicLimit) end end local bucket = redis.call("HMGET", key, "refilledAt", "tokens") if bucket[1] == false then return {effectiveLimit, ${tokenBucketIdentifierNotFound}, effectiveLimit} end return {tonumber(bucket[2]), tonumber(bucket[1]), effectiveLimit} `; export const cachedFixedWindowLimitScript = `#!lua flags=allow-key-locking local key = KEYS[1] local window = ARGV[1] local incrementBy = ARGV[2] -- increment rate per request at a given value, default is 1 local r = redis.call("INCRBY", key, incrementBy) if r == incrementBy then -- The first time this key is set, the value will be equal to incrementBy. -- So we only need the expire command once redis.call("PEXPIRE", key, window) end return r `; export const cachedFixedWindowRemainingTokenScript = `#!lua flags=allow-key-locking local key = KEYS[1] local tokens = 0 local value = redis.call('GET', key) if value then tokens = value end return tokens `;