getRateLimitTtl-2.js 974 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const content = `--[[
  2. Get rate limit ttl
  3. Input:
  4. KEYS[1] 'limiter'
  5. KEYS[2] 'meta'
  6. ARGV[1] maxJobs
  7. ]]
  8. local rcall = redis.call
  9. -- Includes
  10. --[[
  11. Function to get current rate limit ttl.
  12. ]]
  13. local function getRateLimitTTL(maxJobs, rateLimiterKey)
  14. if maxJobs and maxJobs <= tonumber(rcall("GET", rateLimiterKey) or 0) then
  15. local pttl = rcall("PTTL", rateLimiterKey)
  16. if pttl == 0 then
  17. rcall("DEL", rateLimiterKey)
  18. end
  19. if pttl > 0 then
  20. return pttl
  21. end
  22. end
  23. return 0
  24. end
  25. local rateLimiterKey = KEYS[1]
  26. if ARGV[1] ~= "0" then
  27. return getRateLimitTTL(tonumber(ARGV[1]), rateLimiterKey)
  28. else
  29. local rateLimitMax = rcall("HGET", KEYS[2], "max")
  30. if rateLimitMax then
  31. return getRateLimitTTL(tonumber(rateLimitMax), rateLimiterKey)
  32. end
  33. return rcall("PTTL", rateLimiterKey)
  34. end
  35. `;
  36. export const getRateLimitTtl = {
  37. name: 'getRateLimitTtl',
  38. content,
  39. keys: 2,
  40. };
  41. //# sourceMappingURL=getRateLimitTtl-2.js.map