You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ratelimit_cleanup_pending.lua 1.1KB

123456789101112131415161718192021222324252627282930313233
  1. -- This script cleans up the pending requests in Redis.
  2. -- KEYS: Input parameters
  3. -- KEYS[1] - prefix: The Redis key prefix used to store the bucket information.
  4. -- KEYS[2] - now: The current time in milliseconds.
  5. -- KEYS[3] - expire: The expiration time for the Redis key storing the bucket information, in seconds.
  6. -- KEYS[4] - number_of_recipients: The number of requests to be allowed (or the increase rate).
  7. -- 1. Retrieve the last hit time and initialize variables
  8. local prefix = KEYS[1]
  9. local last = redis.call('HGET', prefix, 'l')
  10. local nrcpt = tonumber(KEYS[4])
  11. if not last then
  12. -- No bucket, no cleanup
  13. return 0
  14. end
  15. -- 2. Update the pending values based on the number of recipients (requests)
  16. local pending = redis.call('HGET', prefix, 'p')
  17. pending = tonumber(pending or '0')
  18. if pending < nrcpt then
  19. pending = 0
  20. else
  21. pending = pending - nrcpt
  22. end
  23. -- 3. Set the updated values back to Redis and update the expiration time for the bucket
  24. redis.call('HMSET', prefix, 'p', tostring(pending), 'l', KEYS[2])
  25. redis.call('EXPIRE', prefix, KEYS[3])
  26. -- 4. Return the updated pending value
  27. return pending