Repository navigation
Expand file tree
/
Copy pathredis.ts
More file actions
244 lines (217 loc) · 7.91 KB
/
Copy pathredis.ts
File metadata and controls
244 lines (217 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import Redis from 'ioredis'
import { createLogger } from '@/lib/logs/console-logger'
import { env } from './env'
const logger = createLogger('Redis')
// Default to localhost if REDIS_URL is not provided
const redisUrl = env.REDIS_URL || 'redis://localhost:6379'
// Global Redis client for connection pooling
// This is important for serverless environments like Vercel
let globalRedisClient: Redis | null = null
// Fallback in-memory cache for when Redis is not available
const inMemoryCache = new Map<string, { value: string; expiry: number | null }>()
const MAX_CACHE_SIZE = 1000
/**
* Get a Redis client instance
* Uses connection pooling to avoid creating a new connection for each request
* This is critical for performance in serverless environments like Vercel
*/
export function getRedisClient(): Redis | null {
// For server-side only
if (typeof window !== 'undefined') return null
if (globalRedisClient) return globalRedisClient
try {
// Create a new Redis client with optimized settings for serverless
globalRedisClient = new Redis(redisUrl, {
// Keep alive is critical for serverless to reuse connections
keepAlive: 1000,
// Faster connection timeout for serverless
connectTimeout: 5000,
// Disable reconnection attempts in serverless
maxRetriesPerRequest: 3,
// Retry strategy with exponential backoff
retryStrategy: (times) => {
if (times > 5) {
logger.warn('Redis connection failed after 5 attempts, using fallback')
return null // Stop retrying
}
return Math.min(times * 200, 2000) // Exponential backoff
},
})
// Handle connection events
globalRedisClient.on('error', (err: any) => {
logger.error('Redis connection error:', { err })
if (err.code === 'ECONNREFUSED' || err.code === 'ETIMEDOUT') {
globalRedisClient = null
}
})
globalRedisClient.on('connect', () => {})
return globalRedisClient
} catch (error) {
logger.error('Failed to initialize Redis client:', { error })
return null
}
}
// Message ID cache functions
const MESSAGE_ID_PREFIX = 'processed:' // Generic prefix
const MESSAGE_ID_EXPIRY = 60 * 60 * 24 * 7 // 7 days in seconds
/**
* Check if a key exists in Redis or fallback cache.
* @param key The key to check (e.g., messageId, lockKey).
* @returns True if the key exists and hasn't expired, false otherwise.
*/
export async function hasProcessedMessage(key: string): Promise<boolean> {
try {
const redis = getRedisClient()
const fullKey = `${MESSAGE_ID_PREFIX}${key}` // Use generic prefix
if (redis) {
// Use Redis if available
const result = await redis.exists(fullKey)
return result === 1
}
// Fallback to in-memory cache
const cacheEntry = inMemoryCache.get(fullKey)
if (!cacheEntry) return false
// Check if the entry has expired
if (cacheEntry.expiry && cacheEntry.expiry < Date.now()) {
inMemoryCache.delete(fullKey)
return false
}
return true
} catch (error) {
logger.error(`Error checking key ${key}:`, { error })
// Fallback to in-memory cache on error
const fullKey = `${MESSAGE_ID_PREFIX}${key}`
const cacheEntry = inMemoryCache.get(fullKey)
return !!cacheEntry && (!cacheEntry.expiry || cacheEntry.expiry > Date.now())
}
}
/**
* Mark a key as processed/present in Redis or fallback cache.
* @param key The key to mark (e.g., messageId, lockKey).
* @param expirySeconds Optional expiry time in seconds (defaults to 7 days).
*/
export async function markMessageAsProcessed(
key: string,
expirySeconds: number = MESSAGE_ID_EXPIRY
): Promise<void> {
try {
const redis = getRedisClient()
const fullKey = `${MESSAGE_ID_PREFIX}${key}` // Use generic prefix
if (redis) {
// Use Redis if available - use pipelining for efficiency
await redis.set(fullKey, '1', 'EX', expirySeconds)
} else {
// Fallback to in-memory cache
const expiry = expirySeconds ? Date.now() + expirySeconds * 1000 : null
inMemoryCache.set(fullKey, { value: '1', expiry })
// Clean up old message IDs if cache gets too large
if (inMemoryCache.size > MAX_CACHE_SIZE) {
const now = Date.now()
// First try to remove expired entries
for (const [cacheKey, entry] of inMemoryCache.entries()) {
if (entry.expiry && entry.expiry < now) {
inMemoryCache.delete(cacheKey)
}
}
// If still too large, remove oldest entries (FIFO based on insertion order)
if (inMemoryCache.size > MAX_CACHE_SIZE) {
const keysToDelete = Array.from(inMemoryCache.keys()).slice(
0,
inMemoryCache.size - MAX_CACHE_SIZE
)
for (const keyToDelete of keysToDelete) {
inMemoryCache.delete(keyToDelete)
}
}
}
}
} catch (error) {
logger.error(`Error marking key ${key} as processed:`, { error })
// Fallback to in-memory cache on error
const fullKey = `${MESSAGE_ID_PREFIX}${key}`
const expiry = expirySeconds ? Date.now() + expirySeconds * 1000 : null
inMemoryCache.set(fullKey, { value: '1', expiry })
}
}
/**
* Attempts to acquire a lock using Redis SET NX command.
* @param lockKey The key to use for the lock.
* @param value The value to set (e.g., a unique identifier for the process holding the lock).
* @param expirySeconds The lock's time-to-live in seconds.
* @returns True if the lock was acquired successfully, false otherwise.
*/
export async function acquireLock(
lockKey: string,
value: string,
expirySeconds: number
): Promise<boolean> {
try {
const redis = getRedisClient()
if (!redis) {
logger.warn('Redis client not available, cannot acquire lock.')
// Fallback behavior: maybe allow processing but log a warning?
// Or treat as lock acquired if no Redis? Depends on desired behavior.
return true // Or false, depending on safety requirements
}
// Use SET key value EX expirySeconds NX
// Returns "OK" if successful, null if key already exists (lock held)
const result = await redis.set(lockKey, value, 'EX', expirySeconds, 'NX')
return result === 'OK'
} catch (error) {
logger.error(`Error acquiring lock for key ${lockKey}:`, { error })
// Treat errors as failure to acquire lock for safety
return false
}
}
/**
* Retrieves the value of a key from Redis.
* @param key The key to retrieve.
* @returns The value of the key, or null if the key doesn't exist or an error occurs.
*/
export async function getLockValue(key: string): Promise<string | null> {
try {
const redis = getRedisClient()
if (!redis) {
logger.warn('Redis client not available, cannot get lock value.')
return null // Cannot determine lock value
}
return await redis.get(key)
} catch (error) {
logger.error(`Error getting value for key ${key}:`, { error })
return null
}
}
/**
* Releases a lock by deleting the key.
* Ideally, use Lua script for safe release (check value before deleting),
* but simple DEL is often sufficient if lock expiry is handled well.
* @param lockKey The key of the lock to release.
*/
export async function releaseLock(lockKey: string): Promise<void> {
try {
const redis = getRedisClient()
if (redis) {
await redis.del(lockKey)
} else {
logger.warn('Redis client not available, cannot release lock.')
// No fallback needed for releasing if using in-memory cache for locking wasn't implemented
}
} catch (error) {
logger.error(`Error releasing lock for key ${lockKey}:`, { error })
}
}
/**
* Close the Redis connection
* Important for proper cleanup in serverless environments
*/
export async function closeRedisConnection(): Promise<void> {
if (globalRedisClient) {
try {
await globalRedisClient.quit()
} catch (error) {
logger.error('Error closing Redis connection:', { error })
} finally {
globalRedisClient = null
}
}
}