Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IProcessStreamContext } from '../../../types'

const DISCORD_RATE_LIMIT = 50
const DISCORD_RATE_LIMIT_TIME = 1
const DISCORD_RATE_LIMIT = 100
const DISCORD_RATE_LIMIT_TIME = 1 // 1 second
const REDIS_KEY = 'discord-request-count'

export const getRateLimiter = (ctx: IProcessStreamContext) => {
Expand Down
6 changes: 5 additions & 1 deletion services/libs/redis/src/rateLimiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ export class RateLimiter implements IRateLimiter {
const requestCount = value === null ? 0 : parseInt(value)
const canMakeRequest = requestCount < this.maxRequests

if (requestCount === 0) {
await this.cache.set(this.counterKey, '0', this.timeWindowSeconds)
}

if (!canMakeRequest) {
const sleepTime = this.timeWindowSeconds + Math.floor(Math.random() * this.maxRequests)
throw new RateLimitError(sleepTime, endpoint)
}
}

public async incrementRateLimit() {
await this.cache.increment(this.counterKey, 1, this.timeWindowSeconds)
await this.cache.increment(this.counterKey, 1)
}
}

Expand Down