1. const RateLimiter = require('rate-limiter-flexible');
  2. /**
  3. * Cleans data in a collection with rate limiting.
  4. * @param {object} collection - The collection to clean. Must have a `collectionName` property.
  5. * @param {function} cleanupFunction - A function that performs the cleanup operations.
  6. * @param {number} maxRequests - The maximum number of requests allowed.
  7. * @param {number} interval - The time interval (in seconds) for rate limiting.
  8. */
  9. async function cleanCollection(collection, cleanupFunction, maxRequests, interval) {
  10. const limiter = RateLimiter({ maxRequests, duration: interval });
  11. try {
  12. await limiter.consume(); // Acquire a token
  13. console.log(`Cleaning collection: ${collection.collectionName}`);
  14. await cleanupFunction(collection);
  15. console.log(`Finished cleaning collection: ${collection.collectionName}`);
  16. } catch (error) {
  17. console.error(`Error cleaning collection ${collection.collectionName}:`, error);
  18. }
  19. }
  20. module.exports = cleanCollection;

Add your comment