Skip to content

Instantly share code, notes, and snippets.

@dolfly
Forked from florentchauveau/bucket_get.lua
Created October 19, 2018 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolfly/6770e8ebbbd922daaa789f26d6101830 to your computer and use it in GitHub Desktop.
Save dolfly/6770e8ebbbd922daaa789f26d6101830 to your computer and use it in GitHub Desktop.
-- Redis script to get the size of a token bucket
-- see https://medium.com/callr-techblog/rate-limiting-for-distributed-systems-with-redis-and-lua-eeea745cb260
-- (c) Florent CHAUVEAU <florent.chauveau@gmail.com>
local ts = tonumber(ARGV[1])
local key = KEYS[1]
-- remove tokens < min (older than now() -1s)
local min = ts -1
redis.call('ZREMRANGEBYSCORE', key, '-inf', min)
return redis.call('ZCARD', key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment