1. class InputThrottler {
  2. constructor(limit = 5, interval = 1000) {
  3. this.limit = limit; // Maximum number of requests allowed within the interval
  4. this.interval = interval; // Time interval in milliseconds
  5. this.queue = []; // Queue to store pending requests
  6. this.running = false; // Flag to indicate if the throttler is running
  7. this.lastExecutionTime = 0; // Timestamp of the last execution
  8. }
  9. /**
  10. * Checks if a request is allowed based on the throttling rules.
  11. * @param {function} requestFunction - The function to be throttled.
  12. * @param {...any} args - Arguments to pass to the request function.
  13. * @returns {boolean} - True if the request is allowed, false otherwise.
  14. */
  15. isThrottled(requestFunction, ...args) {
  16. if (!this.running) {
  17. this.run();
  18. }
  19. // Check if the queue is full
  20. if (this.queue.length >= this.limit) {
  21. return false;
  22. }
  23. // Add the request to the queue
  24. this.queue.push({ function: requestFunction, args });
  25. return true;
  26. }
  27. /**
  28. * Executes the pending requests in the queue.
  29. */
  30. async run() {
  31. if (this.running) {
  32. return; // Prevent concurrent runs
  33. }
  34. this.running = true;
  35. const now = Date.now();
  36. // Clear the queue
  37. this.queue = [];
  38. for (let i = 0; i < this.queue.length; i++) {
  39. const { function: requestFunction, args } = this.queue[i];
  40. try {
  41. await requestFunction(...args);
  42. } catch (error) {
  43. console.error("Throttled request failed:", error);
  44. }
  45. }
  46. this.lastExecutionTime = now;
  47. this.running = false;
  48. // Schedule the next execution
  49. setTimeout(() => {
  50. this.run();
  51. }, this.interval);
  52. }
  53. /**
  54. * Sets the throttler to dry-run mode. Requests are logged but not executed.
  55. * @returns {boolean} - true if dry-run mode is enabled.
  56. */
  57. setDryRunMode(dryRun = true) {
  58. this.dryRun = dryRun;
  59. }
  60. /**
  61. * Returns the dry run status.
  62. * @returns {boolean} - dryRun status.
  63. */
  64. getDryRunMode() {
  65. return this.dryRun;
  66. }
  67. }
  68. // Example usage:
  69. // const throttler = new InputThrottler(3, 1000); // Limit 3 requests per 1 second
  70. // async function myRequest(data) {
  71. // console.log("Request executed with data:", data);
  72. // // Simulate an asynchronous operation
  73. // await new Promise(resolve => setTimeout(resolve, 200));
  74. // }
  75. // for (let i = 0; i < 10; i++) {
  76. // if (throttler.isThrottled(myRequest, `Data ${i}`)) {
  77. // console.log(`Request ${i} allowed`);
  78. // } else {
  79. // console.log(`Request ${i} throttled`);
  80. // }
  81. // }
  82. // throttler.setDryRunMode(true); //Enable dry run mode
  83. // for (let i = 0; i < 10; i++) {
  84. // if (throttler.isThrottled(myRequest, `Data ${i}`)) {
  85. // console.log(`Request ${i} allowed`);
  86. // } else {
  87. // console.log(`Request ${i} throttled`);
  88. // }
  89. // }

Add your comment