getRateLimitTtl-2.js 1.1 KB

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