Skip to content

Commit d4ff8e6

Browse files
authored
Merge pull request #129 from csgofloat/feature/max-queue-size-d
Adds a Max Queue Size Param
2 parents 5a602cb + f316b29 commit d4ff8e6

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

config.example.js

+2
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,6 @@ module.exports = {
6464
'price_key': '',
6565
// OPTIONAL: Key by the caller to allow placing bulk searches
6666
'bulk_key': '',
67+
// OPTIONAL: Maximum queue size allowed before dropping requests
68+
'max_queue_size': -1,
6769
};

errors.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module.exports = {
2929
BadBody: new Error('Improper body format', 7, 400),
3030
BadSecret: new Error('Bad Secret', 8, 400),
3131
NoBotsAvailable: new Error('No bots available to fulfill this request', 9, 500),
32-
RateLimit: new Error('Rate limit exceeded, too many requests', 10, 429)
32+
RateLimit: new Error('Rate limit exceeded, too many requests', 10, 429),
33+
MaxQueueSize: new Error('Queue size is full, please try again later', 11, 500),
3334
};
3435

3536

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ async function handleJob(job) {
9494
return job.setResponseRemaining(errors.MaxRequests);
9595
}
9696

97+
if (CONFIG.max_queue_size > 0 && (queue.size() + job.remainingSize()) > CONFIG.max_queue_size) {
98+
return job.setResponseRemaining(errors.MaxQueueSize);
99+
}
100+
97101
if (job.remainingSize() > 0) {
98102
queue.addJob(job, CONFIG.bot_settings.max_attempts);
99103
}

lib/queue.js

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class Queue extends EventEmitter {
1010
this.running = false;
1111
}
1212

13+
size() {
14+
return this.queue.length;
15+
}
16+
1317
process(concurrency, controller, handler) {
1418
this.handler = handler;
1519
this.concurrency = concurrency;

0 commit comments

Comments
 (0)