Skip to content

Commit cdd09e2

Browse files
committed
fix rate limiter and increase requests for Discord
1 parent 43286f9 commit cdd09e2

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

services/libs/integrations/src/integrations/discord/api/handleRateLimit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IProcessStreamContext } from '../../../types'
22

3-
const DISCORD_RATE_LIMIT = 50
4-
const DISCORD_RATE_LIMIT_TIME = 1
3+
const DISCORD_RATE_LIMIT = 100
4+
const DISCORD_RATE_LIMIT_TIME = 1 // 1 second
55
const REDIS_KEY = 'discord-request-count'
66

77
export const getRateLimiter = (ctx: IProcessStreamContext) => {

services/libs/redis/src/rateLimiter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ export class RateLimiter implements IRateLimiter {
1919
const requestCount = value === null ? 0 : parseInt(value)
2020
const canMakeRequest = requestCount < this.maxRequests
2121

22+
if (requestCount === 0) {
23+
await this.cache.set(this.counterKey, '0', this.timeWindowSeconds)
24+
}
25+
2226
if (!canMakeRequest) {
2327
const sleepTime = this.timeWindowSeconds + Math.floor(Math.random() * this.maxRequests)
2428
throw new RateLimitError(sleepTime, endpoint)
2529
}
2630
}
2731

2832
public async incrementRateLimit() {
29-
await this.cache.increment(this.counterKey, 1, this.timeWindowSeconds)
33+
await this.cache.increment(this.counterKey, 1)
3034
}
3135
}
3236

0 commit comments

Comments
 (0)