@duvdu-v1/duvdu 1.1.272 → 1.1.274
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,27 +20,19 @@ 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
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const urlParts = host.split('://');
|
|
31
|
-
if ((_a = urlParts[1]) === null || _a === void 0 ? void 0 : _a.includes(':')) {
|
|
32
|
-
const hostParts = urlParts[1].split(':');
|
|
33
|
-
host = hostParts[0];
|
|
34
|
-
port = parseInt(hostParts[1], 10) || 6379;
|
|
35
|
-
}
|
|
36
|
-
else if (urlParts[1]) {
|
|
37
|
-
host = urlParts[1];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
27
|
+
// Get Redis configuration from environment variables
|
|
28
|
+
const host = process.env.REDIS_HOST || 'redis-11177.c9.us-east-1-2.ec2.redns.redis-cloud.com';
|
|
29
|
+
const port = parseInt(process.env.REDIS_PORT || '11177', 10);
|
|
30
|
+
const password = process.env.REDIS_PASS || 'xgThFOa24hvwyVtsiNhIJiAxfhvJCLBU';
|
|
31
|
+
console.log(`[REDIS] Connecting to Redis at ${host}:${port}`);
|
|
40
32
|
return {
|
|
41
33
|
host,
|
|
42
34
|
port,
|
|
43
|
-
password
|
|
35
|
+
password,
|
|
44
36
|
maxRetriesPerRequest: null,
|
|
45
37
|
enableReadyCheck: false,
|
|
46
38
|
retryStrategy: (times) => {
|
|
@@ -50,16 +42,58 @@ const getRedisConfig = () => {
|
|
|
50
42
|
}
|
|
51
43
|
};
|
|
52
44
|
};
|
|
45
|
+
// Get Redis client info to monitor connections
|
|
46
|
+
const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* () {
|
|
47
|
+
try {
|
|
48
|
+
const info = yield client.info('clients');
|
|
49
|
+
const connectedClients = info.match(/connected_clients:(\d+)/);
|
|
50
|
+
return connectedClients ? parseInt(connectedClients[1], 10) : 0;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
console.error('[REDIS] Error getting client info:', error);
|
|
54
|
+
return 0;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
// Log Redis server stats periodically
|
|
58
|
+
const startMonitoring = () => {
|
|
59
|
+
const monitorInterval = 60000; // 1 minute
|
|
60
|
+
setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
|
+
if (connectionPool.length > 0) {
|
|
62
|
+
try {
|
|
63
|
+
const client = connectionPool[0]; // Use first client for monitoring
|
|
64
|
+
const clientCount = yield getClientInfo(client);
|
|
65
|
+
console.log(`[REDIS] Server stats - Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active tracked connections: ${activeConnections}`);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error('[REDIS] Error monitoring Redis:', error);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}), monitorInterval);
|
|
72
|
+
};
|
|
53
73
|
// Initialize the connection pool
|
|
54
74
|
const initializePool = () => {
|
|
55
75
|
if (connectionPool.length === 0) {
|
|
56
|
-
console.log(`Initializing Redis connection pool with ${MAX_CLIENTS} clients`);
|
|
57
76
|
const config = getRedisConfig();
|
|
77
|
+
console.log(`[REDIS] Initializing connection pool with ${MAX_CLIENTS} clients to ${config.host}:${config.port}`);
|
|
58
78
|
for (let i = 0; i < MAX_CLIENTS; i++) {
|
|
59
79
|
const client = new ioredis_1.default(config);
|
|
60
80
|
client.setMaxListeners(1000);
|
|
81
|
+
// Add connection event listeners
|
|
82
|
+
client.on('connect', () => {
|
|
83
|
+
activeConnections++;
|
|
84
|
+
console.log(`[REDIS] Client #${i + 1} connected successfully (Active: ${activeConnections})`);
|
|
85
|
+
});
|
|
86
|
+
client.on('error', (err) => {
|
|
87
|
+
console.error(`[REDIS] Client #${i + 1} connection error:`, err);
|
|
88
|
+
});
|
|
89
|
+
client.on('close', () => {
|
|
90
|
+
activeConnections = Math.max(0, activeConnections - 1);
|
|
91
|
+
console.log(`[REDIS] Client #${i + 1} connection closed (Active: ${activeConnections})`);
|
|
92
|
+
});
|
|
61
93
|
connectionPool.push(client);
|
|
62
94
|
}
|
|
95
|
+
// Start monitoring
|
|
96
|
+
startMonitoring();
|
|
63
97
|
}
|
|
64
98
|
};
|
|
65
99
|
// Get a client from the pool using round-robin
|
|
@@ -67,37 +101,47 @@ const getRedisClient = () => {
|
|
|
67
101
|
if (connectionPool.length === 0) {
|
|
68
102
|
initializePool();
|
|
69
103
|
}
|
|
104
|
+
totalConnectionsRequested++;
|
|
70
105
|
// Round-robin selection
|
|
71
106
|
const client = connectionPool[currentConnectionIndex];
|
|
72
107
|
currentConnectionIndex = (currentConnectionIndex + 1) % connectionPool.length;
|
|
108
|
+
if (totalConnectionsRequested % 100 === 0) {
|
|
109
|
+
console.log(`[REDIS] Total connection requests: ${totalConnectionsRequested}, Current pool size: ${connectionPool.length}, Active connections: ${activeConnections}`);
|
|
110
|
+
}
|
|
73
111
|
return client;
|
|
74
112
|
};
|
|
75
113
|
exports.getRedisClient = getRedisClient;
|
|
76
114
|
const redisConnection = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
115
|
try {
|
|
78
116
|
const client = (0, exports.getRedisClient)();
|
|
79
|
-
console.log(
|
|
117
|
+
console.log('[REDIS] Connection provided from pool');
|
|
80
118
|
return client;
|
|
81
119
|
}
|
|
82
120
|
catch (error) {
|
|
83
|
-
|
|
84
|
-
|
|
121
|
+
const config = getRedisConfig();
|
|
122
|
+
console.error(`[REDIS] Cannot connect to Redis: ${config.host}:${config.port}`, error);
|
|
123
|
+
throw new data_base_connections_1.DatabaseConnectionError(`Cannot connect to Redis: ${config.host}:${config.port}`);
|
|
85
124
|
}
|
|
86
125
|
});
|
|
87
126
|
exports.redisConnection = redisConnection;
|
|
88
127
|
const sessionStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
89
128
|
const client = (0, exports.getRedisClient)();
|
|
129
|
+
console.log('[REDIS] Created session store with pooled connection');
|
|
90
130
|
return new connect_redis_1.default({ client });
|
|
91
131
|
});
|
|
92
132
|
exports.sessionStore = sessionStore;
|
|
93
133
|
const cleanupRedis = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
94
134
|
if (connectionPool.length > 0) {
|
|
95
|
-
console.log('Closing all Redis connections in the pool');
|
|
96
|
-
for (
|
|
135
|
+
console.log('[REDIS] Closing all Redis connections in the pool');
|
|
136
|
+
for (let i = 0; i < connectionPool.length; i++) {
|
|
137
|
+
const client = connectionPool[i];
|
|
97
138
|
yield client.quit();
|
|
139
|
+
console.log(`[REDIS] Client #${i + 1} quit successfully`);
|
|
98
140
|
}
|
|
99
141
|
connectionPool = [];
|
|
100
142
|
currentConnectionIndex = 0;
|
|
143
|
+
activeConnections = 0;
|
|
144
|
+
console.log('[REDIS] Connection pool cleared');
|
|
101
145
|
}
|
|
102
146
|
});
|
|
103
147
|
exports.cleanupRedis = cleanupRedis;
|