Skip to content

Commit 6a31e0a

Browse files
authored
feat(queue): support getGlobalRateLimit method (#3511)
1 parent a0fe4fc commit 6a31e0a

File tree

4 files changed

+66
-40
lines changed

4 files changed

+66
-40
lines changed

docs/gitbook/guide/queues/global-rate-limit.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ import { Queue } from 'bullmq';
99
await queue.setGlobalRateLimit(1, 1000);
1010
```
1111

12-
And in order to get this value:
12+
In order to get these values:
1313

1414
```typescript
15-
const globalConcurrency = await queue.getRateLimitTtl();
15+
const { max, duration } = await queue.getGlobalRateLimit();
16+
```
17+
18+
And in order to get current ttl:
19+
20+
```typescript
21+
const ttl = await queue.getRateLimitTtl();
1622
```
1723

1824
{% hint style="info" %}
@@ -30,5 +36,6 @@ await queue.removeGlobalRateLimit();
3036
## Read more:
3137

3238
- 💡 [Set Global Rate Limit API Reference](https://api.docs.bullmq.io/classes/v5.Queue.html#setglobalratelimit)
39+
- 💡 [Get Global Rate Limit API Reference](https://api.docs.bullmq.io/classes/v5.Queue.html#getglobalratelimit)
3340
- 💡 [Get Rate Limit Ttl API Reference](https://api.docs.bullmq.io/classes/v5.Queue.html#getratelimitttl)
3441
- 💡 [Remove Global Rate Limit API Reference](https://api.docs.bullmq.io/classes/v5.Queue.html#removeglobalratelimit)

src/classes/queue-getters.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,42 @@ export class QueueGetters<JobBase extends Job = Job> extends QueueBase {
117117
return client.get(`${this.keys.de}:${id}`);
118118
}
119119

120+
/**
121+
* Get global concurrency value.
122+
* Returns null in case no value is set.
123+
*/
124+
async getGlobalConcurrency(): Promise<number | null> {
125+
const client = await this.client;
126+
const concurrency = await client.hget(this.keys.meta, 'concurrency');
127+
if (concurrency) {
128+
return Number(concurrency);
129+
}
130+
return null;
131+
}
132+
133+
/**
134+
* Get global rate limit values.
135+
* Returns null in case no value is set.
136+
*/
137+
async getGlobalRateLimit(): Promise<{
138+
max: number;
139+
duration: number;
140+
} | null> {
141+
const client = await this.client;
142+
const [max, duration] = await client.hmget(
143+
this.keys.meta,
144+
'max',
145+
'duration',
146+
);
147+
if (max && duration) {
148+
return {
149+
max: Number(max),
150+
duration: Number(duration),
151+
};
152+
}
153+
return null;
154+
}
155+
120156
/**
121157
* Job counts by type
122158
*

src/classes/queue.ts

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -96,33 +96,22 @@ export interface QueueListener<JobBase extends Job = Job>
9696
*/
9797
type IsAny<T> = 0 extends 1 & T ? true : false;
9898
// Helper for JobBase type
99-
type JobBase<T, ResultType, NameType extends string> = IsAny<T> extends true
100-
? Job<T, ResultType, NameType>
101-
: T extends Job<any, any, any>
102-
? T
103-
: Job<T, ResultType, NameType>;
99+
type JobBase<T, ResultType, NameType extends string> =
100+
IsAny<T> extends true
101+
? Job<T, ResultType, NameType>
102+
: T extends Job<any, any, any>
103+
? T
104+
: Job<T, ResultType, NameType>;
104105

105106
// Helper types to extract DataType, ResultType, and NameType
106-
type ExtractDataType<DataTypeOrJob, Default> = DataTypeOrJob extends Job<
107-
infer D,
108-
any,
109-
any
110-
>
111-
? D
112-
: Default;
113-
114-
type ExtractResultType<DataTypeOrJob, Default> = DataTypeOrJob extends Job<
115-
any,
116-
infer R,
117-
any
118-
>
119-
? R
120-
: Default;
121-
122-
type ExtractNameType<
123-
DataTypeOrJob,
124-
Default extends string,
125-
> = DataTypeOrJob extends Job<any, any, infer N> ? N : Default;
107+
type ExtractDataType<DataTypeOrJob, Default> =
108+
DataTypeOrJob extends Job<infer D, any, any> ? D : Default;
109+
110+
type ExtractResultType<DataTypeOrJob, Default> =
111+
DataTypeOrJob extends Job<any, infer R, any> ? R : Default;
112+
113+
type ExtractNameType<DataTypeOrJob, Default extends string> =
114+
DataTypeOrJob extends Job<any, any, infer N> ? N : Default;
126115

127116
/**
128117
* Queue
@@ -277,19 +266,6 @@ export class Queue<
277266
});
278267
}
279268

280-
/**
281-
* Get global concurrency value.
282-
* Returns null in case no value is set.
283-
*/
284-
async getGlobalConcurrency(): Promise<number | null> {
285-
const client = await this.client;
286-
const concurrency = await client.hget(this.keys.meta, 'concurrency');
287-
if (concurrency) {
288-
return Number(concurrency);
289-
}
290-
return null;
291-
}
292-
293269
/**
294270
* Enable and set global concurrency value.
295271
* @param concurrency - Maximum number of simultaneous jobs that the workers can handle.

tests/test_rate_limiter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ describe('Rate Limiter', function () {
9898
);
9999

100100
await queue.setGlobalRateLimit(1, 500);
101+
const globalRateLimit = await queue.getGlobalRateLimit();
102+
103+
expect(globalRateLimit).to.deep.equal({ max: 1, duration: 500 });
101104

102105
const result = new Promise<void>((resolve, reject) => {
103106
queueEvents.on(
@@ -152,6 +155,10 @@ describe('Rate Limiter', function () {
152155
await queue.setGlobalRateLimit(1, 500);
153156
await queue.removeGlobalRateLimit();
154157

158+
const globalRateLimit = await queue.getGlobalRateLimit();
159+
160+
expect(globalRateLimit).to.be.null;
161+
155162
const result = new Promise<void>((resolve, reject) => {
156163
queueEvents.on(
157164
'completed',

0 commit comments

Comments
 (0)