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_update.lua 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. -- This script updates a token bucket rate limiter with dynamic rate and burst multipliers 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] - dynamic_rate_multiplier: A multiplier to adjust the rate limit dynamically.
  6. -- KEYS[4] - dynamic_burst_multiplier: A multiplier to adjust the burst limit dynamically.
  7. -- KEYS[5] - max_dyn_rate: The maximum allowed value for the dynamic rate multiplier.
  8. -- KEYS[6] - max_burst_rate: The maximum allowed value for the dynamic burst multiplier.
  9. -- KEYS[7] - expire: The expiration time for the Redis key storing the bucket information, in seconds.
  10. -- KEYS[8] - number_of_recipients: The number of requests to be allowed (or the increase rate).
  11. -- 1. Retrieve the last hit time and initialize variables
  12. local prefix = KEYS[1]
  13. local last = redis.call('HGET', prefix, 'l')
  14. local now = tonumber(KEYS[2])
  15. local nrcpt = tonumber(KEYS[8])
  16. if not last then
  17. -- 2. Initialize a new bucket if the last hit time is not found (must not happen)
  18. redis.call('HMSET', prefix, 'l', tostring(now), 'b', tostring(nrcpt), 'dr', '10000', 'db', '10000', 'p', '0')
  19. redis.call('EXPIRE', prefix, KEYS[7])
  20. return { 1, 1, 1 }
  21. end
  22. -- 3. Update the dynamic rate multiplier based on input parameters
  23. local dr, db = 1.0, 1.0
  24. local max_dr = tonumber(KEYS[5])
  25. if max_dr > 1 then
  26. local rate_mult = tonumber(KEYS[3])
  27. dr = tonumber(redis.call('HGET', prefix, 'dr')) / 10000
  28. if rate_mult > 1.0 and dr < max_dr then
  29. dr = dr * rate_mult
  30. if dr > 0.0001 then
  31. redis.call('HSET', prefix, 'dr', tostring(math.floor(dr * 10000)))
  32. else
  33. redis.call('HSET', prefix, 'dr', '1')
  34. end
  35. elseif rate_mult < 1.0 and dr > (1.0 / max_dr) then
  36. dr = dr * rate_mult
  37. if dr > 0.0001 then
  38. redis.call('HSET', prefix, 'dr', tostring(math.floor(dr * 10000)))
  39. else
  40. redis.call('HSET', prefix, 'dr', '1')
  41. end
  42. end
  43. end
  44. -- 4. Update the dynamic burst multiplier based on input parameters
  45. local max_db = tonumber(KEYS[6])
  46. if max_db > 1 then
  47. local rate_mult = tonumber(KEYS[4])
  48. db = tonumber(redis.call('HGET', prefix, 'db')) / 10000
  49. if rate_mult > 1.0 and db < max_db then
  50. db = db * rate_mult
  51. if db > 0.0001 then
  52. redis.call('HSET', prefix, 'db', tostring(math.floor(db * 10000)))
  53. else
  54. redis.call('HSET', prefix, 'db', '1')
  55. end
  56. elseif rate_mult < 1.0 and db > (1.0 / max_db) then
  57. db = db * rate_mult
  58. if db > 0.0001 then
  59. redis.call('HSET', prefix, 'db', tostring(math.floor(db * 10000)))
  60. else
  61. redis.call('HSET', prefix, 'db', '1')
  62. end
  63. end
  64. end
  65. -- 5. Update the burst and pending values based on the number of recipients (requests)
  66. local burst, pending = unpack(redis.call('HMGET', prefix, 'b', 'p'))
  67. burst, pending = tonumber(burst or '0'), tonumber(pending or '0')
  68. if burst < 0 then
  69. burst = nrcpt
  70. else
  71. burst = burst + nrcpt
  72. end
  73. if pending < nrcpt then
  74. pending = 0
  75. else
  76. pending = pending - nrcpt
  77. end
  78. -- 6. Set the updated values back to Redis and update the expiration time for the bucket
  79. redis.call('HMSET', prefix, 'b', tostring(burst), 'p', tostring(pending), 'l', KEYS[2])
  80. redis.call('EXPIRE', prefix, KEYS[7])
  81. -- 7. Return the updated burst value, dynamic rate multiplier, and dynamic burst multiplier
  82. return { tostring(burst), tostring(dr), tostring(db) }