Fix proxy

This commit is contained in:
2025-10-06 02:46:08 +07:00
parent fa292d1688
commit 1dbe2a48fc

View File

@@ -162,18 +162,16 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
// Helper function to check Redis connectivity for idempotency // Helper function to check Redis connectivity for idempotency
async function checkRedis() { async function checkRedis() {
let redisClient = null; let redisClient = null;
let isConnected = false;
try { try {
const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379'; const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
const redisPassword = process.env.REDIS_PASSWORD; const redisPassword = process.env.REDIS_PASSWORD;
console.log('Redis URL:', redisUrl)
console.log('Redis Password:', redisPassword ? '***' : 'none')
// Create Redis client configuration with timeout // Create Redis client configuration with timeout
const redisConfig: any = { const redisConfig: any = {
url: redisUrl, url: redisUrl,
socket: { socket: {
connectTimeout: 3000, // 3 second connection timeout connectTimeout: 2000, // 2 second connection timeout
reconnectStrategy: false // Don't retry on health check reconnectStrategy: false // Don't retry on health check
} }
}; };
@@ -184,17 +182,17 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
redisClient = createClient(redisConfig); redisClient = createClient(redisConfig);
// Set up error handling // Suppress error logs for health checks to avoid spam
redisClient.on('error', (err) => { redisClient.on('error', () => {
console.error('Redis health check error:', err); // Silently ignore errors in health check
}); });
// Connect to Redis with timeout // Connect to Redis with timeout
const startTime = Date.now(); const startTime = Date.now();
await Promise.race([ await Promise.race([
redisClient.connect(), redisClient.connect().then(() => { isConnected = true; }),
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout(() => reject(new Error('Connection timeout after 3s')), 3000) setTimeout(() => reject(new Error('Connection timeout after 2s')), 2000)
) )
]); ]);
const connectTime = Date.now() - startTime; const connectTime = Date.now() - startTime;
@@ -241,21 +239,29 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
}; };
} catch (error) { } catch (error) {
return { return {
status: 'unhealthy', status: 'degraded',
message: `Redis connection failed: ${error instanceof Error ? error.message : 'Unknown error'}`, message: 'Redis unavailable, using in-memory fallback',
data: { data: {
errorType: error instanceof Error ? error.constructor.name : 'Unknown', errorType: error instanceof Error ? error.constructor.name : 'Unknown',
redisUrl: process.env.REDIS_URL || 'redis://localhost:6379', redisUrl: process.env.REDIS_URL || 'redis://localhost:6379',
hasPassword: !!process.env.REDIS_PASSWORD hasPassword: !!process.env.REDIS_PASSWORD,
note: 'Service will function normally with in-memory idempotency storage'
} }
}; };
} finally { } finally {
// Always close the Redis connection // Always close the Redis connection gracefully
if (redisClient && redisClient.isOpen) { if (redisClient) {
try { try {
await redisClient.quit(); // Only try to quit if we successfully connected
if (isConnected && redisClient.isOpen) {
// Use disconnect instead of quit for faster cleanup
await Promise.race([
redisClient.disconnect(),
new Promise((resolve) => setTimeout(resolve, 500)) // 500ms timeout for disconnect
]);
}
} catch (closeError) { } catch (closeError) {
console.error('Error closing Redis connection in health check:', closeError); // Silently ignore close errors in health check
} }
} }
} }