Skip to content

Commit

Permalink
Merge pull request #129 from csgofloat/feature/max-queue-size-d
Browse files Browse the repository at this point in the history
Adds a Max Queue Size Param
  • Loading branch information
Step7750 authored Apr 12, 2023
2 parents 5a602cb + f316b29 commit d4ff8e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
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

0 comments on commit d4ff8e6

Please sign in to comment.