Skip to content

Commit a56cd77

Browse files
committed
Fix Redis lazy connect race condition causing 500 on first request
1 parent 73a3282 commit a56cd77

3 files changed

Lines changed: 0 additions & 45 deletions

File tree

src/lib/rate-limit.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ function getIdentifier(request: NextRequest, prefix: string = '', customKey?: st
5656

5757
async function getRateLimitEntry(identifier: string): Promise<RateLimitEntry | null> {
5858
const redis = getRedis()
59-
60-
if (redis.status !== 'ready') {
61-
await redis.connect()
62-
}
63-
6459
const data = await redis.get(identifier)
6560
if (!data) return null
6661

@@ -78,22 +73,12 @@ async function setRateLimitEntry(
7873
ttlMs: number
7974
): Promise<void> {
8075
const redis = getRedis()
81-
82-
if (redis.status !== 'ready') {
83-
await redis.connect()
84-
}
85-
8676
const ttlSeconds = Math.ceil(ttlMs / 1000)
8777
await redis.setex(identifier, ttlSeconds, JSON.stringify(entry))
8878
}
8979

9080
async function deleteRateLimitEntry(identifier: string): Promise<void> {
9181
const redis = getRedis()
92-
93-
if (redis.status !== 'ready') {
94-
await redis.connect()
95-
}
96-
9782
await redis.del(identifier)
9883
}
9984

@@ -300,10 +285,6 @@ export async function getRateLimitedEntries(): Promise<Array<{
300285
try {
301286
const redis = getRedis()
302287

303-
if (redis.status !== 'ready') {
304-
await redis.connect()
305-
}
306-
307288
// Use SCAN instead of KEYS to avoid blocking Redis on large datasets
308289
const lockedEntries: Array<{
309290
key: string
@@ -359,11 +340,6 @@ export async function getRateLimitedEntries(): Promise<Array<{
359340
export async function clearRateLimitByKey(key: string): Promise<number> {
360341
try {
361342
const redis = getRedis()
362-
363-
if (redis.status !== 'ready') {
364-
await redis.connect()
365-
}
366-
367343
const deleted = await redis.del(key)
368344
if (deleted === 0) {
369345
logWarn(`Rate limit key not found in Redis: ${key}`)
@@ -387,10 +363,6 @@ export async function clearAllRateLimits(): Promise<number> {
387363
try {
388364
const redis = getRedis()
389365

390-
if (redis.status !== 'ready') {
391-
await redis.connect()
392-
}
393-
394366
let clearedCount = 0
395367
let cursor = '0'
396368

src/lib/redis.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export function getRedis(): IORedis {
3535
password: process.env.REDIS_PASSWORD,
3636
maxRetriesPerRequest: 3,
3737
enableReadyCheck: true,
38-
lazyConnect: true,
3938
retryStrategy: (times) => {
4039
if (times > 3) {
4140
logError('Redis connection failed after 3 retries')

src/lib/token-revocation.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ import { logWarn } from './logging'
3131
export async function revokeToken(token: string, expiresIn: number): Promise<void> {
3232
const redis = getRedis()
3333

34-
if (redis.status !== 'ready') {
35-
await redis.connect()
36-
}
37-
3834
// Use the token signature (last part) as the key to save space
3935
// Format: blacklist:token:{signature}
4036
const tokenParts = token.split('.')
@@ -63,9 +59,6 @@ export async function revokeToken(token: string, expiresIn: number): Promise<voi
6359
export async function isTokenRevoked(token: string): Promise<boolean> {
6460
const redis = getRedis()
6561

66-
if (redis.status !== 'ready') {
67-
await redis.connect()
68-
}
6962

7063
const tokenParts = token.split('.')
7164
const signature = tokenParts[tokenParts.length - 1]
@@ -90,9 +83,6 @@ export async function isTokenRevoked(token: string): Promise<boolean> {
9083
export async function revokeAllUserTokens(userId: string): Promise<void> {
9184
const redis = getRedis()
9285

93-
if (redis.status !== 'ready') {
94-
await redis.connect()
95-
}
9686

9787
// Store a flag that user's session is invalidated
9888
// This can be checked before validating any token
@@ -116,9 +106,6 @@ export async function revokeAllUserTokens(userId: string): Promise<void> {
116106
export async function isUserTokensRevoked(userId: string, tokenIssuedAt?: number): Promise<boolean> {
117107
const redis = getRedis()
118108

119-
if (redis.status !== 'ready') {
120-
await redis.connect()
121-
}
122109

123110
const key = `blacklist:user:${userId}`
124111
const revocationTimestamp = await redis.get(key)
@@ -149,9 +136,6 @@ export async function isUserTokensRevoked(userId: string, tokenIssuedAt?: number
149136
export async function clearUserRevocation(userId: string): Promise<void> {
150137
const redis = getRedis()
151138

152-
if (redis.status !== 'ready') {
153-
await redis.connect()
154-
}
155139

156140
const key = `blacklist:user:${userId}`
157141
await redis.del(key)

0 commit comments

Comments
 (0)