@duvdu-v1/duvdu 1.1.275 → 1.1.277
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.
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const bullRedis: import("ioredis/built/Redis").default
|
|
1
|
+
declare const bullRedis: Promise<import("ioredis/built/Redis").default>;
|
|
2
2
|
export { bullRedis };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import RedisStore from 'connect-redis';
|
|
2
2
|
import Redis from 'ioredis';
|
|
3
|
-
export declare const getRedisClient: () => Redis
|
|
3
|
+
export declare const getRedisClient: () => Promise<Redis>;
|
|
4
4
|
export declare const redisConnection: () => Promise<Redis>;
|
|
5
5
|
export declare const sessionStore: () => Promise<RedisStore>;
|
|
6
6
|
export declare const cleanupRedis: () => Promise<void>;
|
|
@@ -17,11 +17,11 @@ 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 = 1; // Single connection per service to minimize total connections
|
|
21
21
|
let connectionPool = [];
|
|
22
22
|
let currentConnectionIndex = 0;
|
|
23
|
-
let totalConnectionsRequested = 0;
|
|
24
23
|
let activeConnections = 0;
|
|
24
|
+
let isInitializing = false;
|
|
25
25
|
// Cache the RedisStore instance
|
|
26
26
|
let redisStoreInstance = null;
|
|
27
27
|
// Parse Redis connection details
|
|
@@ -30,13 +30,13 @@ const getRedisConfig = () => {
|
|
|
30
30
|
const host = process.env.REDIS_HOST || 'redis-11177.c9.us-east-1-2.ec2.redns.redis-cloud.com';
|
|
31
31
|
const port = parseInt(process.env.REDIS_PORT || '11177', 10);
|
|
32
32
|
const password = process.env.REDIS_PASS || 'xgThFOa24hvwyVtsiNhIJiAxfhvJCLBU';
|
|
33
|
-
console.log(`[REDIS] Connecting to Redis at ${host}:${port}`);
|
|
34
33
|
return {
|
|
35
34
|
host,
|
|
36
35
|
port,
|
|
37
36
|
password,
|
|
38
37
|
maxRetriesPerRequest: null,
|
|
39
38
|
enableReadyCheck: false,
|
|
39
|
+
lazyConnect: true, // Don't connect immediately
|
|
40
40
|
retryStrategy: (times) => {
|
|
41
41
|
if (times > 3)
|
|
42
42
|
return null;
|
|
@@ -52,7 +52,6 @@ const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
52
52
|
return connectedClients ? parseInt(connectedClients[1], 10) : 0;
|
|
53
53
|
}
|
|
54
54
|
catch (error) {
|
|
55
|
-
console.error('[REDIS] Error getting client info:', error);
|
|
56
55
|
return 0;
|
|
57
56
|
}
|
|
58
57
|
});
|
|
@@ -60,23 +59,22 @@ const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
60
59
|
const startMonitoring = () => {
|
|
61
60
|
const monitorInterval = 60000; // 1 minute
|
|
62
61
|
setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
-
if (connectionPool.length > 0) {
|
|
62
|
+
if (connectionPool.length > 0 && activeConnections > 0) {
|
|
64
63
|
try {
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
console.log(`[REDIS] Server stats - Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active tracked connections: ${activeConnections}`);
|
|
64
|
+
const clientCount = yield getClientInfo(connectionPool[0]);
|
|
65
|
+
console.log(`[REDIS] Stats: Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active: ${activeConnections}`);
|
|
68
66
|
}
|
|
69
67
|
catch (error) {
|
|
70
|
-
|
|
68
|
+
// Silent error
|
|
71
69
|
}
|
|
72
70
|
}
|
|
73
71
|
}), monitorInterval);
|
|
74
72
|
};
|
|
75
73
|
// Initialize the connection pool
|
|
76
|
-
const initializePool = () => {
|
|
77
|
-
if (connectionPool.length === 0) {
|
|
74
|
+
const initializePool = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
if (connectionPool.length === 0 && !isInitializing) {
|
|
76
|
+
isInitializing = true;
|
|
78
77
|
const config = getRedisConfig();
|
|
79
|
-
console.log(`[REDIS] Initializing connection pool with ${MAX_CLIENTS} clients to ${config.host}:${config.port}`);
|
|
80
78
|
for (let i = 0; i < MAX_CLIENTS; i++) {
|
|
81
79
|
const client = new ioredis_1.default(config);
|
|
82
80
|
client.setMaxListeners(1000);
|
|
@@ -85,43 +83,41 @@ const initializePool = () => {
|
|
|
85
83
|
activeConnections++;
|
|
86
84
|
console.log(`[REDIS] Client #${i + 1} connected successfully (Active: ${activeConnections})`);
|
|
87
85
|
});
|
|
88
|
-
client.on('error', (
|
|
89
|
-
|
|
86
|
+
client.on('error', () => {
|
|
87
|
+
// Silent error
|
|
90
88
|
});
|
|
91
89
|
client.on('close', () => {
|
|
92
90
|
activeConnections = Math.max(0, activeConnections - 1);
|
|
93
|
-
console.log(`[REDIS] Client #${i + 1} connection closed (Active: ${activeConnections})`);
|
|
94
91
|
});
|
|
95
92
|
connectionPool.push(client);
|
|
96
93
|
}
|
|
97
94
|
// Start monitoring
|
|
98
95
|
startMonitoring();
|
|
96
|
+
isInitializing = false;
|
|
99
97
|
}
|
|
100
|
-
};
|
|
98
|
+
});
|
|
101
99
|
// Get a client from the pool using round-robin
|
|
102
|
-
const getRedisClient = () => {
|
|
100
|
+
const getRedisClient = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
103
101
|
if (connectionPool.length === 0) {
|
|
104
|
-
initializePool();
|
|
102
|
+
yield initializePool();
|
|
105
103
|
}
|
|
106
|
-
|
|
107
|
-
// Round-robin selection
|
|
104
|
+
// Round-robin selection (though with MAX_CLIENTS=1, this just returns the single client)
|
|
108
105
|
const client = connectionPool[currentConnectionIndex];
|
|
109
106
|
currentConnectionIndex = (currentConnectionIndex + 1) % connectionPool.length;
|
|
110
|
-
|
|
111
|
-
|
|
107
|
+
// Ensure the client is connected
|
|
108
|
+
if (client.status !== 'ready' && client.status !== 'connecting') {
|
|
109
|
+
yield client.connect();
|
|
112
110
|
}
|
|
113
111
|
return client;
|
|
114
|
-
};
|
|
112
|
+
});
|
|
115
113
|
exports.getRedisClient = getRedisClient;
|
|
116
114
|
const redisConnection = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
117
115
|
try {
|
|
118
|
-
const client = (0, exports.getRedisClient)();
|
|
119
|
-
console.log('[REDIS] Connection provided from pool');
|
|
116
|
+
const client = yield (0, exports.getRedisClient)();
|
|
120
117
|
return client;
|
|
121
118
|
}
|
|
122
119
|
catch (error) {
|
|
123
120
|
const config = getRedisConfig();
|
|
124
|
-
console.error(`[REDIS] Cannot connect to Redis: ${config.host}:${config.port}`, error);
|
|
125
121
|
throw new data_base_connections_1.DatabaseConnectionError(`Cannot connect to Redis: ${config.host}:${config.port}`);
|
|
126
122
|
}
|
|
127
123
|
});
|
|
@@ -131,26 +127,22 @@ const sessionStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
131
127
|
if (redisStoreInstance) {
|
|
132
128
|
return redisStoreInstance;
|
|
133
129
|
}
|
|
134
|
-
const client = (0, exports.getRedisClient)();
|
|
135
|
-
console.log('[REDIS] Created session store with pooled connection');
|
|
130
|
+
const client = yield (0, exports.getRedisClient)();
|
|
136
131
|
redisStoreInstance = new connect_redis_1.default({ client });
|
|
137
132
|
return redisStoreInstance;
|
|
138
133
|
});
|
|
139
134
|
exports.sessionStore = sessionStore;
|
|
140
135
|
const cleanupRedis = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
141
136
|
if (connectionPool.length > 0) {
|
|
142
|
-
console.log('[REDIS] Closing all Redis connections in the pool');
|
|
143
137
|
for (let i = 0; i < connectionPool.length; i++) {
|
|
144
138
|
const client = connectionPool[i];
|
|
145
139
|
yield client.quit();
|
|
146
|
-
console.log(`[REDIS] Client #${i + 1} quit successfully`);
|
|
147
140
|
}
|
|
148
141
|
connectionPool = [];
|
|
149
142
|
currentConnectionIndex = 0;
|
|
150
143
|
activeConnections = 0;
|
|
151
144
|
// Reset the store instance
|
|
152
145
|
redisStoreInstance = null;
|
|
153
|
-
console.log('[REDIS] Connection pool cleared');
|
|
154
146
|
}
|
|
155
147
|
});
|
|
156
148
|
exports.cleanupRedis = cleanupRedis;
|