@duvdu-v1/duvdu 1.1.274 → 1.1.276
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.
|
@@ -17,18 +17,18 @@ const connect_redis_1 = __importDefault(require("connect-redis"));
|
|
|
17
17
|
const ioredis_1 = __importDefault(require("ioredis"));
|
|
18
18
|
const data_base_connections_1 = require("../errors/data-base-connections");
|
|
19
19
|
// Create a Redis cluster or connection pool
|
|
20
|
-
const MAX_CLIENTS =
|
|
20
|
+
const MAX_CLIENTS = 2; // Limited to 2 connections to stay well below the connection limit
|
|
21
21
|
let connectionPool = [];
|
|
22
22
|
let currentConnectionIndex = 0;
|
|
23
|
-
let totalConnectionsRequested = 0;
|
|
24
23
|
let activeConnections = 0;
|
|
24
|
+
// Cache the RedisStore instance
|
|
25
|
+
let redisStoreInstance = null;
|
|
25
26
|
// Parse Redis connection details
|
|
26
27
|
const getRedisConfig = () => {
|
|
27
28
|
// Get Redis configuration from environment variables
|
|
28
29
|
const host = process.env.REDIS_HOST || 'redis-11177.c9.us-east-1-2.ec2.redns.redis-cloud.com';
|
|
29
30
|
const port = parseInt(process.env.REDIS_PORT || '11177', 10);
|
|
30
31
|
const password = process.env.REDIS_PASS || 'xgThFOa24hvwyVtsiNhIJiAxfhvJCLBU';
|
|
31
|
-
console.log(`[REDIS] Connecting to Redis at ${host}:${port}`);
|
|
32
32
|
return {
|
|
33
33
|
host,
|
|
34
34
|
port,
|
|
@@ -50,7 +50,6 @@ const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
50
50
|
return connectedClients ? parseInt(connectedClients[1], 10) : 0;
|
|
51
51
|
}
|
|
52
52
|
catch (error) {
|
|
53
|
-
console.error('[REDIS] Error getting client info:', error);
|
|
54
53
|
return 0;
|
|
55
54
|
}
|
|
56
55
|
});
|
|
@@ -60,12 +59,11 @@ const startMonitoring = () => {
|
|
|
60
59
|
setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
61
60
|
if (connectionPool.length > 0) {
|
|
62
61
|
try {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
console.log(`[REDIS] Server stats - Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active tracked connections: ${activeConnections}`);
|
|
62
|
+
const clientCount = yield getClientInfo(connectionPool[0]);
|
|
63
|
+
console.log(`[REDIS] Stats: Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active: ${activeConnections}`);
|
|
66
64
|
}
|
|
67
65
|
catch (error) {
|
|
68
|
-
|
|
66
|
+
// Silent error
|
|
69
67
|
}
|
|
70
68
|
}
|
|
71
69
|
}), monitorInterval);
|
|
@@ -74,7 +72,6 @@ const startMonitoring = () => {
|
|
|
74
72
|
const initializePool = () => {
|
|
75
73
|
if (connectionPool.length === 0) {
|
|
76
74
|
const config = getRedisConfig();
|
|
77
|
-
console.log(`[REDIS] Initializing connection pool with ${MAX_CLIENTS} clients to ${config.host}:${config.port}`);
|
|
78
75
|
for (let i = 0; i < MAX_CLIENTS; i++) {
|
|
79
76
|
const client = new ioredis_1.default(config);
|
|
80
77
|
client.setMaxListeners(1000);
|
|
@@ -83,12 +80,11 @@ const initializePool = () => {
|
|
|
83
80
|
activeConnections++;
|
|
84
81
|
console.log(`[REDIS] Client #${i + 1} connected successfully (Active: ${activeConnections})`);
|
|
85
82
|
});
|
|
86
|
-
client.on('error', (
|
|
87
|
-
|
|
83
|
+
client.on('error', () => {
|
|
84
|
+
// Silent error
|
|
88
85
|
});
|
|
89
86
|
client.on('close', () => {
|
|
90
87
|
activeConnections = Math.max(0, activeConnections - 1);
|
|
91
|
-
console.log(`[REDIS] Client #${i + 1} connection closed (Active: ${activeConnections})`);
|
|
92
88
|
});
|
|
93
89
|
connectionPool.push(client);
|
|
94
90
|
}
|
|
@@ -101,47 +97,44 @@ const getRedisClient = () => {
|
|
|
101
97
|
if (connectionPool.length === 0) {
|
|
102
98
|
initializePool();
|
|
103
99
|
}
|
|
104
|
-
totalConnectionsRequested++;
|
|
105
100
|
// Round-robin selection
|
|
106
101
|
const client = connectionPool[currentConnectionIndex];
|
|
107
102
|
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
|
-
}
|
|
111
103
|
return client;
|
|
112
104
|
};
|
|
113
105
|
exports.getRedisClient = getRedisClient;
|
|
114
106
|
const redisConnection = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
115
107
|
try {
|
|
116
108
|
const client = (0, exports.getRedisClient)();
|
|
117
|
-
console.log('[REDIS] Connection provided from pool');
|
|
118
109
|
return client;
|
|
119
110
|
}
|
|
120
111
|
catch (error) {
|
|
121
112
|
const config = getRedisConfig();
|
|
122
|
-
console.error(`[REDIS] Cannot connect to Redis: ${config.host}:${config.port}`, error);
|
|
123
113
|
throw new data_base_connections_1.DatabaseConnectionError(`Cannot connect to Redis: ${config.host}:${config.port}`);
|
|
124
114
|
}
|
|
125
115
|
});
|
|
126
116
|
exports.redisConnection = redisConnection;
|
|
127
117
|
const sessionStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
118
|
+
// Return cached instance if available
|
|
119
|
+
if (redisStoreInstance) {
|
|
120
|
+
return redisStoreInstance;
|
|
121
|
+
}
|
|
128
122
|
const client = (0, exports.getRedisClient)();
|
|
129
|
-
|
|
130
|
-
return
|
|
123
|
+
redisStoreInstance = new connect_redis_1.default({ client });
|
|
124
|
+
return redisStoreInstance;
|
|
131
125
|
});
|
|
132
126
|
exports.sessionStore = sessionStore;
|
|
133
127
|
const cleanupRedis = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
128
|
if (connectionPool.length > 0) {
|
|
135
|
-
console.log('[REDIS] Closing all Redis connections in the pool');
|
|
136
129
|
for (let i = 0; i < connectionPool.length; i++) {
|
|
137
130
|
const client = connectionPool[i];
|
|
138
131
|
yield client.quit();
|
|
139
|
-
console.log(`[REDIS] Client #${i + 1} quit successfully`);
|
|
140
132
|
}
|
|
141
133
|
connectionPool = [];
|
|
142
134
|
currentConnectionIndex = 0;
|
|
143
135
|
activeConnections = 0;
|
|
144
|
-
|
|
136
|
+
// Reset the store instance
|
|
137
|
+
redisStoreInstance = null;
|
|
145
138
|
}
|
|
146
139
|
});
|
|
147
140
|
exports.cleanupRedis = cleanupRedis;
|