@duvdu-v1/duvdu 1.1.272 → 1.1.273
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -20,6 +20,8 @@ const data_base_connections_1 = require("../errors/data-base-connections");
|
|
|
20
20
|
const MAX_CLIENTS = 5; // Limited to 5 connections to stay well below the 30 connection limit
|
|
21
21
|
let connectionPool = [];
|
|
22
22
|
let currentConnectionIndex = 0;
|
|
23
|
+
let totalConnectionsRequested = 0;
|
|
24
|
+
let activeConnections = 0;
|
|
23
25
|
// Parse Redis connection details
|
|
24
26
|
const getRedisConfig = () => {
|
|
25
27
|
var _a;
|
|
@@ -50,16 +52,58 @@ const getRedisConfig = () => {
|
|
|
50
52
|
}
|
|
51
53
|
};
|
|
52
54
|
};
|
|
55
|
+
// Get Redis client info to monitor connections
|
|
56
|
+
const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
+
try {
|
|
58
|
+
const info = yield client.info('clients');
|
|
59
|
+
const connectedClients = info.match(/connected_clients:(\d+)/);
|
|
60
|
+
return connectedClients ? parseInt(connectedClients[1], 10) : 0;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
console.error('[REDIS] Error getting client info:', error);
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
// Log Redis server stats periodically
|
|
68
|
+
const startMonitoring = () => {
|
|
69
|
+
const monitorInterval = 60000; // 1 minute
|
|
70
|
+
setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
71
|
+
if (connectionPool.length > 0) {
|
|
72
|
+
try {
|
|
73
|
+
const client = connectionPool[0]; // Use first client for monitoring
|
|
74
|
+
const clientCount = yield getClientInfo(client);
|
|
75
|
+
console.log(`[REDIS] Server stats - Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active tracked connections: ${activeConnections}`);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
console.error('[REDIS] Error monitoring Redis:', error);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}), monitorInterval);
|
|
82
|
+
};
|
|
53
83
|
// Initialize the connection pool
|
|
54
84
|
const initializePool = () => {
|
|
55
85
|
if (connectionPool.length === 0) {
|
|
56
|
-
console.log(`Initializing Redis connection pool with ${MAX_CLIENTS} clients`);
|
|
57
86
|
const config = getRedisConfig();
|
|
87
|
+
console.log(`[REDIS] Initializing connection pool with ${MAX_CLIENTS} clients to ${config.host}:${config.port}`);
|
|
58
88
|
for (let i = 0; i < MAX_CLIENTS; i++) {
|
|
59
89
|
const client = new ioredis_1.default(config);
|
|
60
90
|
client.setMaxListeners(1000);
|
|
91
|
+
// Add connection event listeners
|
|
92
|
+
client.on('connect', () => {
|
|
93
|
+
activeConnections++;
|
|
94
|
+
console.log(`[REDIS] Client #${i + 1} connected successfully (Active: ${activeConnections})`);
|
|
95
|
+
});
|
|
96
|
+
client.on('error', (err) => {
|
|
97
|
+
console.error(`[REDIS] Client #${i + 1} connection error:`, err);
|
|
98
|
+
});
|
|
99
|
+
client.on('close', () => {
|
|
100
|
+
activeConnections = Math.max(0, activeConnections - 1);
|
|
101
|
+
console.log(`[REDIS] Client #${i + 1} connection closed (Active: ${activeConnections})`);
|
|
102
|
+
});
|
|
61
103
|
connectionPool.push(client);
|
|
62
104
|
}
|
|
105
|
+
// Start monitoring
|
|
106
|
+
startMonitoring();
|
|
63
107
|
}
|
|
64
108
|
};
|
|
65
109
|
// Get a client from the pool using round-robin
|
|
@@ -67,37 +111,47 @@ const getRedisClient = () => {
|
|
|
67
111
|
if (connectionPool.length === 0) {
|
|
68
112
|
initializePool();
|
|
69
113
|
}
|
|
114
|
+
totalConnectionsRequested++;
|
|
70
115
|
// Round-robin selection
|
|
71
116
|
const client = connectionPool[currentConnectionIndex];
|
|
72
117
|
currentConnectionIndex = (currentConnectionIndex + 1) % connectionPool.length;
|
|
118
|
+
if (totalConnectionsRequested % 100 === 0) {
|
|
119
|
+
console.log(`[REDIS] Total connection requests: ${totalConnectionsRequested}, Current pool size: ${connectionPool.length}, Active connections: ${activeConnections}`);
|
|
120
|
+
}
|
|
73
121
|
return client;
|
|
74
122
|
};
|
|
75
123
|
exports.getRedisClient = getRedisClient;
|
|
76
124
|
const redisConnection = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
125
|
try {
|
|
78
126
|
const client = (0, exports.getRedisClient)();
|
|
79
|
-
|
|
127
|
+
const config = getRedisConfig();
|
|
128
|
+
console.log(`[REDIS] Connection provided from pool to ${config.host}:${config.port}`);
|
|
80
129
|
return client;
|
|
81
130
|
}
|
|
82
131
|
catch (error) {
|
|
83
|
-
console.error(`Cannot connect to Redis: ${process.env.REDIS_HOST}`, error);
|
|
132
|
+
console.error(`[REDIS] Cannot connect to Redis: ${process.env.REDIS_HOST}`, error);
|
|
84
133
|
throw new data_base_connections_1.DatabaseConnectionError(`Cannot connect to Redis: ${process.env.REDIS_HOST}`);
|
|
85
134
|
}
|
|
86
135
|
});
|
|
87
136
|
exports.redisConnection = redisConnection;
|
|
88
137
|
const sessionStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
138
|
const client = (0, exports.getRedisClient)();
|
|
139
|
+
console.log('[REDIS] Created session store with pooled connection');
|
|
90
140
|
return new connect_redis_1.default({ client });
|
|
91
141
|
});
|
|
92
142
|
exports.sessionStore = sessionStore;
|
|
93
143
|
const cleanupRedis = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
94
144
|
if (connectionPool.length > 0) {
|
|
95
|
-
console.log('Closing all Redis connections in the pool');
|
|
96
|
-
for (
|
|
145
|
+
console.log('[REDIS] Closing all Redis connections in the pool');
|
|
146
|
+
for (let i = 0; i < connectionPool.length; i++) {
|
|
147
|
+
const client = connectionPool[i];
|
|
97
148
|
yield client.quit();
|
|
149
|
+
console.log(`[REDIS] Client #${i + 1} quit successfully`);
|
|
98
150
|
}
|
|
99
151
|
connectionPool = [];
|
|
100
152
|
currentConnectionIndex = 0;
|
|
153
|
+
activeConnections = 0;
|
|
154
|
+
console.log('[REDIS] Connection pool cleared');
|
|
101
155
|
}
|
|
102
156
|
});
|
|
103
157
|
exports.cleanupRedis = cleanupRedis;
|