export const fixedWindowLimitScript = `#!lua flags=allow-key-locking local key = KEYS[1] local id = ARGV[1] local window = ARGV[2] local incrementBy = tonumber(ARGV[3]) redis.call("HSET", key, id, incrementBy) local fields = redis.call("HGETALL", key) if #fields == 2 and tonumber(fields[2])==incrementBy then -- The first time this key is set, and the value will be equal to incrementBy. -- So we only need the expire command once redis.call("PEXPIRE", key, window) end return fields `; export const fixedWindowRemainingTokensScript = `#!lua flags=allow-key-locking local key = KEYS[1] local tokens = 0 local fields = redis.call("HGETALL", key) return fields `; 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 tokens = tonumber(ARGV[1]) -- tokens per window local now = ARGV[2] -- current timestamp in milliseconds local window = ARGV[3] -- interval in milliseconds local requestId = ARGV[4] -- uuid for this request local incrementBy = tonumber(ARGV[5]) -- custom rate, default is 1 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 -- Only check limit if not refunding (negative rate) if incrementBy > 0 and requestsInPreviousWindow * (1 - percentageInCurrent ) + requestsInCurrentWindow + incrementBy > tokens then return {currentFields, previousFields, false} end redis.call("HSET", currentKey, requestId, incrementBy) if requestsInCurrentWindow == 0 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 {currentFields, previousFields, true} `; 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 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 `;