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
async function checkRedis() {
let redisClient = null;
let isConnected = false;
try {
const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
const redisPassword = process.env.REDIS_PASSWORD;
console.log('Redis URL:', redisUrl)
console.log('Redis Password:', redisPassword ? '***' : 'none')
// Create Redis client configuration with timeout
const redisConfig: any = {
url: redisUrl,
socket: {
connectTimeout: 3000, // 3 second connection timeout
connectTimeout: 2000, // 2 second connection timeout
reconnectStrategy: false // Don't retry on health check
}
};
@@ -184,17 +182,17 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
redisClient = createClient(redisConfig);
// Set up error handling
redisClient.on('error', (err) => {
console.error('Redis health check error:', err);
// Suppress error logs for health checks to avoid spam
redisClient.on('error', () => {
// Silently ignore errors in health check
});
// Connect to Redis with timeout
const startTime = Date.now();
await Promise.race([
redisClient.connect(),
redisClient.connect().then(() => { isConnected = true; }),
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;
@@ -241,21 +239,29 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
};
} catch (error) {
return {
status: 'unhealthy',
message: `Redis connection failed: ${error instanceof Error ? error.message : 'Unknown error'}`,
status: 'degraded',
message: 'Redis unavailable, using in-memory fallback',
data: {
errorType: error instanceof Error ? error.constructor.name : 'Unknown',
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 {
// Always close the Redis connection
if (redisClient && redisClient.isOpen) {
// Always close the Redis connection gracefully
if (redisClient) {
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) {
console.error('Error closing Redis connection in health check:', closeError);
// Silently ignore close errors in health check
}
}
}