diff --git a/config.example.js b/config.example.js index 33f9139..b6447f2 100644 --- a/config.example.js +++ b/config.example.js @@ -64,4 +64,6 @@ module.exports = { 'price_key': '', // OPTIONAL: Key by the caller to allow placing bulk searches 'bulk_key': '', + // OPTIONAL: Maximum queue size allowed before dropping requests + 'max_queue_size': -1, }; diff --git a/errors.js b/errors.js index b096736..5fe9f0e 100644 --- a/errors.js +++ b/errors.js @@ -29,7 +29,8 @@ module.exports = { BadBody: new Error('Improper body format', 7, 400), BadSecret: new Error('Bad Secret', 8, 400), NoBotsAvailable: new Error('No bots available to fulfill this request', 9, 500), - RateLimit: new Error('Rate limit exceeded, too many requests', 10, 429) + RateLimit: new Error('Rate limit exceeded, too many requests', 10, 429), + MaxQueueSize: new Error('Queue size is full, please try again later', 11, 500), }; diff --git a/index.js b/index.js index 29dea6f..fb38264 100644 --- a/index.js +++ b/index.js @@ -94,6 +94,10 @@ async function handleJob(job) { return job.setResponseRemaining(errors.MaxRequests); } + if (CONFIG.max_queue_size > 0 && (queue.size() + job.remainingSize()) > CONFIG.max_queue_size) { + return job.setResponseRemaining(errors.MaxQueueSize); + } + if (job.remainingSize() > 0) { queue.addJob(job, CONFIG.bot_settings.max_attempts); } diff --git a/lib/queue.js b/lib/queue.js index ae356a7..11b0d49 100644 --- a/lib/queue.js +++ b/lib/queue.js @@ -10,6 +10,10 @@ class Queue extends EventEmitter { this.running = false; } + size() { + return this.queue.length; + } + process(concurrency, controller, handler) { this.handler = handler; this.concurrency = concurrency;