-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cde896b
commit 76ba3e2
Showing
12 changed files
with
153 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Redis } from 'ioredis'; | ||
import { get } from '../utils/redis'; | ||
import { getQueueRateLimitKey } from '../utils/keys'; | ||
|
||
export interface QueueRateLimitConfig { | ||
points: number; | ||
duration: number; | ||
} | ||
|
||
export const getQueueRateLimitConfig = async ({ | ||
queue, | ||
client, | ||
}: { | ||
queue: string; | ||
client: Redis; | ||
}) => { | ||
const rateLimitString = await get({ | ||
key: getQueueRateLimitKey({ queue }), | ||
client, | ||
}); | ||
if (!rateLimitString) { | ||
return null; | ||
} | ||
return JSON.parse(rateLimitString) as QueueRateLimitConfig; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
import { Redis } from 'ioredis'; | ||
import { set } from '../utils/redis'; | ||
import { getQueueRateLimitKey } from '../utils/keys'; | ||
import { exec } from '../utils/redis'; | ||
import { | ||
getQueueRateLimitKey, | ||
getQueueRateLimitUpdatedChannel, | ||
} from '../utils/keys'; | ||
import { EventType } from '../domain/events/event-type'; | ||
import { serializeEvent } from '../domain/events/serialize-event'; | ||
|
||
export const setQueueRateLimit = async ({ | ||
amount, | ||
interval, | ||
points, | ||
duration, | ||
queue, | ||
client, | ||
}: { | ||
amount: number; | ||
interval: number; | ||
points: number; | ||
duration: number; | ||
queue: string; | ||
client: Redis; | ||
}) => { | ||
await set({ | ||
key: getQueueRateLimitKey({ queue }), | ||
value: JSON.stringify({ amount, interval }), | ||
client, | ||
}); | ||
const multi = client.multi(); | ||
multi.set( | ||
getQueueRateLimitKey({ queue }), | ||
JSON.stringify({ points, duration }), | ||
); | ||
multi.publish( | ||
getQueueRateLimitUpdatedChannel({ queue }), | ||
serializeEvent({ | ||
type: EventType.QueueRateLimitUpdated, | ||
createdAt: new Date(), | ||
data: { rateLimitConfig: { points, duration } }, | ||
}), | ||
); | ||
await exec(multi); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { Task } from '../tasks/task'; | ||
import { EventType } from './event-type'; | ||
import { WorkerInstance } from '../worker/worker-instance'; | ||
import { QueueRateLimitConfig } from '../../actions/get-queue-rate-limit-config'; | ||
|
||
export interface Event { | ||
createdAt: Date; | ||
type: EventType; | ||
task?: Task; | ||
worker?: WorkerInstance; | ||
data?: { | ||
rateLimitConfig?: QueueRateLimitConfig; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters