Skip to content

Commit

Permalink
feat(queue): add getRateLimitTtl method (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf authored Aug 3, 2023
1 parent 235ff47 commit 7426c64
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/classes/queue-getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ export class QueueGetters<
return count;
}

/**
* Returns the time to live for a rate limited key in milliseconds.
* @returns -2 if the key does not exist.
* @returns -1 if the key exists but has no associated expire.
* @see {@link https://redis.io/commands/pttl/}
*/
async getRateLimitTtl(): Promise<number> {
const client = await this.client;
return client.pttl(this.keys.limiter);
}

/**
* Job counts by type
*
Expand Down
6 changes: 6 additions & 0 deletions tests/test_rate_limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,17 @@ describe('Rate Limiter', function () {
const duration = 100;
const margin = 0.95; // 5% margin for CI

const ttl = await queue.getRateLimitTtl();
expect(ttl).to.be.equal(-2);

const worker = new Worker(
queueName,
async job => {
if (job.attemptsMade === 1) {
await worker.rateLimit(dynamicLimit);
const currentTtl = await queue.getRateLimitTtl();
expect(currentTtl).to.be.lessThanOrEqual(250);
expect(currentTtl).to.be.greaterThan(100);
throw Worker.RateLimitError();
}
},
Expand Down

0 comments on commit 7426c64

Please sign in to comment.