Fixed Bug Server
## Summary This branch contains several bug fixes and performance improvements, primarily focusing on: - Database connection management - MQTT client stability - Logging optimization - API enhancements ## Detailed Changes ### Fixed Issues 1. **Database Connection Management** - Removed from user-validate API route to prevent connection pool exhaustion - Added proper connection handling in global Prisma setup - Reduced logging verbosity in production environments 2. **MQTT Client Improvements** - Enhanced MQTT client initialization with proper error handling - Added reconnection logic with configurable intervals - Implemented cleanup functions to prevent memory leaks - Added separate initialization logic for server and client-side code 3. **Logging Optimization** - Removed excessive logging in middleware that was causing high CPU usage - Configured appropriate log levels for development and production 4. **Component Stability** - Added safety checks in text editor component to prevent MQTT operations on the server side - Improved MQTT publishing logic with client availability checks ### New Files - - Utility functions for safe database operations ### Modified Files 1. - Removed problematic call 2. - Configured different logging levels for dev/prod - Removed process listeners that were causing disconnections - Exported prisma instance separately 3. - Removed excessive logging statements 4. - Enhanced initialization with error handling - Added reconnection and timeout configurations 5. - Added proper cleanup functions - Improved connection handling 6. - Added MQTT client availability checks - Prevented server-side MQTT operations ### Performance Improvements - Reduced database connection overhead - Optimized MQTT connection handling - Eliminated unnecessary logging in production - Better memory management with proper cleanup functions ### No Issue
This commit is contained in:
@@ -4,7 +4,66 @@ declare global {
|
||||
var mqtt_client: mqtt.MqttClient;
|
||||
}
|
||||
|
||||
const mqtt_client =
|
||||
globalThis.mqtt_client || mqtt.connect("wss://io.wibudev.com");
|
||||
// Initialize MQTT client with proper error handling and reconnection settings
|
||||
let mqtt_client: mqtt.MqttClient;
|
||||
|
||||
if (typeof window === 'undefined') {
|
||||
// Server-side code
|
||||
mqtt_client = globalThis.mqtt_client || (() => {
|
||||
const client = mqtt.connect("wss://io.wibudev.com", {
|
||||
reconnectPeriod: 5000, // Reconnect every 5 seconds
|
||||
connectTimeout: 30 * 1000, // 30 second timeout
|
||||
// Clean session to avoid message queue buildup
|
||||
clean: true,
|
||||
// Reduce unnecessary pings
|
||||
keepalive: 60
|
||||
});
|
||||
|
||||
// Prevent multiple initializations
|
||||
globalThis.mqtt_client = client;
|
||||
|
||||
// Add error handling
|
||||
client.on('error', (error) => {
|
||||
console.error('MQTT Connection Error:', error);
|
||||
});
|
||||
|
||||
client.on('reconnect', () => {
|
||||
console.log('MQTT Reconnecting...');
|
||||
});
|
||||
|
||||
client.on('close', () => {
|
||||
console.log('MQTT Connection Closed');
|
||||
});
|
||||
|
||||
return client;
|
||||
})();
|
||||
} else {
|
||||
// Client-side code - initialize only once
|
||||
if (!(globalThis as any).mqtt_client) {
|
||||
(globalThis as any).mqtt_client = mqtt.connect("wss://io.wibudev.com", {
|
||||
reconnectPeriod: 5000, // Reconnect every 5 seconds
|
||||
connectTimeout: 30 * 1000, // 30 second timeout
|
||||
// Clean session to avoid message queue buildup
|
||||
clean: true,
|
||||
// Reduce unnecessary pings
|
||||
keepalive: 60
|
||||
});
|
||||
|
||||
// Add error handling
|
||||
(globalThis as any).mqtt_client.on('error', (error: any) => {
|
||||
console.error('MQTT Connection Error:', error);
|
||||
});
|
||||
|
||||
(globalThis as any).mqtt_client.on('reconnect', () => {
|
||||
console.log('MQTT Reconnecting...');
|
||||
});
|
||||
|
||||
(globalThis as any).mqtt_client.on('close', () => {
|
||||
console.log('MQTT Connection Closed');
|
||||
});
|
||||
}
|
||||
|
||||
mqtt_client = (globalThis as any).mqtt_client;
|
||||
}
|
||||
|
||||
export default mqtt_client;
|
||||
|
||||
@@ -3,20 +3,27 @@
|
||||
import { useEffect } from "react";
|
||||
import mqtt_client from "./mqtt_client";
|
||||
|
||||
export default function MqttLoader() {
|
||||
export default function MqttLoader() {
|
||||
useEffect(() => {
|
||||
mqtt_client.on("connect", () => {
|
||||
console.log("connected");
|
||||
});
|
||||
// Only set up connection handlers once
|
||||
const handleConnect = () => {
|
||||
console.log("MQTT connected");
|
||||
};
|
||||
|
||||
const handleError = (error: any) => {
|
||||
console.error("MQTT Error:", error);
|
||||
};
|
||||
|
||||
// Subscribe to events
|
||||
mqtt_client.on("connect", handleConnect);
|
||||
mqtt_client.on("error", handleError);
|
||||
|
||||
// Cleanup function to unsubscribe when component unmounts
|
||||
return () => {
|
||||
mqtt_client.off("connect", handleConnect);
|
||||
mqtt_client.off("error", handleError);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
|
||||
// <>
|
||||
// <Stack>
|
||||
// <Button onClick={onClick}>Tekan</Button>
|
||||
// <Button onClick={onClick2}>Tekan 2</Button>
|
||||
// </Stack>
|
||||
// </>
|
||||
// );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user