Skip to content

Adds a Max Queue Size Param #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
3 changes: 2 additions & 1 deletion errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};


4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down