lua.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package redis_rate
  2. import "github.com/go-redis/redis/v8"
  3. // Copyright (c) 2017 Pavel Pravosud
  4. // https://github.com/rwz/redis-gcra/blob/master/vendor/perform_gcra_ratelimit.lua
  5. var allowN = redis.NewScript(`
  6. -- this script has side-effects, so it requires replicate commands mode
  7. redis.replicate_commands()
  8. local rate_limit_key = KEYS[1]
  9. local burst = ARGV[1]
  10. local rate = ARGV[2]
  11. local period = ARGV[3]
  12. local cost = tonumber(ARGV[4])
  13. local emission_interval = period / rate
  14. local increment = emission_interval * cost
  15. local burst_offset = emission_interval * burst
  16. -- redis returns time as an array containing two integers: seconds of the epoch
  17. -- time (10 digits) and microseconds (6 digits). for convenience we need to
  18. -- convert them to a floating point number. the resulting number is 16 digits,
  19. -- bordering on the limits of a 64-bit double-precision floating point number.
  20. -- adjust the epoch to be relative to Jan 1, 2017 00:00:00 GMT to avoid floating
  21. -- point problems. this approach is good until "now" is 2,483,228,799 (Wed, 09
  22. -- Sep 2048 01:46:39 GMT), when the adjusted value is 16 digits.
  23. local jan_1_2017 = 1483228800
  24. local now = redis.call("TIME")
  25. now = (now[1] - jan_1_2017) + (now[2] / 1000000)
  26. local tat = redis.call("GET", rate_limit_key)
  27. if not tat then
  28. tat = now
  29. else
  30. tat = tonumber(tat)
  31. end
  32. tat = math.max(tat, now)
  33. local new_tat = tat + increment
  34. local allow_at = new_tat - burst_offset
  35. local diff = now - allow_at
  36. local remaining = math.floor(diff / emission_interval + 0.5)
  37. if remaining < 0 then
  38. local reset_after = tat - now
  39. local retry_after = diff * -1
  40. return {
  41. 0, -- allowed
  42. 0, -- remaining
  43. tostring(retry_after),
  44. tostring(reset_after),
  45. }
  46. end
  47. local reset_after = new_tat - now
  48. redis.call("SET", rate_limit_key, new_tat, "EX", math.ceil(reset_after))
  49. local retry_after = -1
  50. return {cost, remaining, tostring(retry_after), tostring(reset_after)}
  51. `)
  52. var allowAtMost = redis.NewScript(`
  53. -- this script has side-effects, so it requires replicate commands mode
  54. redis.replicate_commands()
  55. local rate_limit_key = KEYS[1]
  56. local burst = ARGV[1]
  57. local rate = ARGV[2]
  58. local period = ARGV[3]
  59. local cost = tonumber(ARGV[4])
  60. local emission_interval = period / rate
  61. local burst_offset = emission_interval * burst
  62. -- redis returns time as an array containing two integers: seconds of the epoch
  63. -- time (10 digits) and microseconds (6 digits). for convenience we need to
  64. -- convert them to a floating point number. the resulting number is 16 digits,
  65. -- bordering on the limits of a 64-bit double-precision floating point number.
  66. -- adjust the epoch to be relative to Jan 1, 2017 00:00:00 GMT to avoid floating
  67. -- point problems. this approach is good until "now" is 2,483,228,799 (Wed, 09
  68. -- Sep 2048 01:46:39 GMT), when the adjusted value is 16 digits.
  69. local jan_1_2017 = 1483228800
  70. local now = redis.call("TIME")
  71. now = (now[1] - jan_1_2017) + (now[2] / 1000000)
  72. local tat = redis.call("GET", rate_limit_key)
  73. if not tat then
  74. tat = now
  75. else
  76. tat = tonumber(tat)
  77. end
  78. tat = math.max(tat, now)
  79. local diff = now - (tat - burst_offset)
  80. local remaining = math.floor(diff / emission_interval + 0.5)
  81. if remaining == 0 then
  82. local reset_after = tat - now
  83. local retry_after = emission_interval - diff
  84. return {
  85. 0, -- allowed
  86. 0, -- remaining
  87. tostring(retry_after),
  88. tostring(reset_after),
  89. }
  90. end
  91. if remaining < cost then
  92. cost = remaining
  93. remaining = 0
  94. else
  95. remaining = remaining - cost
  96. end
  97. local increment = emission_interval * cost
  98. local new_tat = tat + increment
  99. local reset_after = new_tat - now
  100. redis.call("SET", rate_limit_key, new_tat, "EX", math.ceil(reset_after))
  101. return {
  102. cost,
  103. remaining,
  104. tostring(-1),
  105. tostring(reset_after),
  106. }
  107. `)