@duvdu-v1/duvdu 1.1.275 → 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,10 +17,9 @@ 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;
|
|
25
24
|
// Cache the RedisStore instance
|
|
26
25
|
let redisStoreInstance = null;
|
|
@@ -30,7 +29,6 @@ const getRedisConfig = () => {
|
|
|
30
29
|
const host = process.env.REDIS_HOST || 'redis-11177.c9.us-east-1-2.ec2.redns.redis-cloud.com';
|
|
31
30
|
const port = parseInt(process.env.REDIS_PORT || '11177', 10);
|
|
32
31
|
const password = process.env.REDIS_PASS || 'xgThFOa24hvwyVtsiNhIJiAxfhvJCLBU';
|
|
33
|
-
console.log(`[REDIS] Connecting to Redis at ${host}:${port}`);
|
|
34
32
|
return {
|
|
35
33
|
host,
|
|
36
34
|
port,
|
|
@@ -52,7 +50,6 @@ const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
52
50
|
return connectedClients ? parseInt(connectedClients[1], 10) : 0;
|
|
53
51
|
}
|
|
54
52
|
catch (error) {
|
|
55
|
-
console.error('[REDIS] Error getting client info:', error);
|
|
56
53
|
return 0;
|
|
57
54
|
}
|
|
58
55
|
});
|
|
@@ -62,12 +59,11 @@ const startMonitoring = () => {
|
|
|
62
59
|
setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
60
|
if (connectionPool.length > 0) {
|
|
64
61
|
try {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
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}`);
|
|
68
64
|
}
|
|
69
65
|
catch (error) {
|
|
70
|
-
|
|
66
|
+
// Silent error
|
|
71
67
|
}
|
|
72
68
|
}
|
|
73
69
|
}), monitorInterval);
|
|
@@ -76,7 +72,6 @@ const startMonitoring = () => {
|
|
|
76
72
|
const initializePool = () => {
|
|
77
73
|
if (connectionPool.length === 0) {
|
|
78
74
|
const config = getRedisConfig();
|
|
79
|
-
console.log(`[REDIS] Initializing connection pool with ${MAX_CLIENTS} clients to ${config.host}:${config.port}`);
|
|
80
75
|
for (let i = 0; i < MAX_CLIENTS; i++) {
|
|
81
76
|
const client = new ioredis_1.default(config);
|
|
82
77
|
client.setMaxListeners(1000);
|
|
@@ -85,12 +80,11 @@ const initializePool = () => {
|
|
|
85
80
|
activeConnections++;
|
|
86
81
|
console.log(`[REDIS] Client #${i + 1} connected successfully (Active: ${activeConnections})`);
|
|
87
82
|
});
|
|
88
|
-
client.on('error', (
|
|
89
|
-
|
|
83
|
+
client.on('error', () => {
|
|
84
|
+
// Silent error
|
|
90
85
|
});
|
|
91
86
|
client.on('close', () => {
|
|
92
87
|
activeConnections = Math.max(0, activeConnections - 1);
|
|
93
|
-
console.log(`[REDIS] Client #${i + 1} connection closed (Active: ${activeConnections})`);
|
|
94
88
|
});
|
|
95
89
|
connectionPool.push(client);
|
|
96
90
|
}
|
|
@@ -103,25 +97,19 @@ const getRedisClient = () => {
|
|
|
103
97
|
if (connectionPool.length === 0) {
|
|
104
98
|
initializePool();
|
|
105
99
|
}
|
|
106
|
-
totalConnectionsRequested++;
|
|
107
100
|
// Round-robin selection
|
|
108
101
|
const client = connectionPool[currentConnectionIndex];
|
|
109
102
|
currentConnectionIndex = (currentConnectionIndex + 1) % connectionPool.length;
|
|
110
|
-
if (totalConnectionsRequested % 100 === 0) {
|
|
111
|
-
console.log(`[REDIS] Total connection requests: ${totalConnectionsRequested}, Current pool size: ${connectionPool.length}, Active connections: ${activeConnections}`);
|
|
112
|
-
}
|
|
113
103
|
return client;
|
|
114
104
|
};
|
|
115
105
|
exports.getRedisClient = getRedisClient;
|
|
116
106
|
const redisConnection = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
107
|
try {
|
|
118
108
|
const client = (0, exports.getRedisClient)();
|
|
119
|
-
console.log('[REDIS] Connection provided from pool');
|
|
120
109
|
return client;
|
|
121
110
|
}
|
|
122
111
|
catch (error) {
|
|
123
112
|
const config = getRedisConfig();
|
|
124
|
-
console.error(`[REDIS] Cannot connect to Redis: ${config.host}:${config.port}`, error);
|
|
125
113
|
throw new data_base_connections_1.DatabaseConnectionError(`Cannot connect to Redis: ${config.host}:${config.port}`);
|
|
126
114
|
}
|
|
127
115
|
});
|
|
@@ -132,25 +120,21 @@ const sessionStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
132
120
|
return redisStoreInstance;
|
|
133
121
|
}
|
|
134
122
|
const client = (0, exports.getRedisClient)();
|
|
135
|
-
console.log('[REDIS] Created session store with pooled connection');
|
|
136
123
|
redisStoreInstance = new connect_redis_1.default({ client });
|
|
137
124
|
return redisStoreInstance;
|
|
138
125
|
});
|
|
139
126
|
exports.sessionStore = sessionStore;
|
|
140
127
|
const cleanupRedis = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
141
128
|
if (connectionPool.length > 0) {
|
|
142
|
-
console.log('[REDIS] Closing all Redis connections in the pool');
|
|
143
129
|
for (let i = 0; i < connectionPool.length; i++) {
|
|
144
130
|
const client = connectionPool[i];
|
|
145
131
|
yield client.quit();
|
|
146
|
-
console.log(`[REDIS] Client #${i + 1} quit successfully`);
|
|
147
132
|
}
|
|
148
133
|
connectionPool = [];
|
|
149
134
|
currentConnectionIndex = 0;
|
|
150
135
|
activeConnections = 0;
|
|
151
136
|
// Reset the store instance
|
|
152
137
|
redisStoreInstance = null;
|
|
153
|
-
console.log('[REDIS] Connection pool cleared');
|
|
154
138
|
}
|
|
155
139
|
});
|
|
156
140
|
exports.cleanupRedis = cleanupRedis;
|