@duvdu-v1/duvdu 1.1.276 → 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,10 +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
23
|
let activeConnections = 0;
|
|
24
|
+
let isInitializing = false;
|
|
24
25
|
// Cache the RedisStore instance
|
|
25
26
|
let redisStoreInstance = null;
|
|
26
27
|
// Parse Redis connection details
|
|
@@ -35,6 +36,7 @@ const getRedisConfig = () => {
|
|
|
35
36
|
password,
|
|
36
37
|
maxRetriesPerRequest: null,
|
|
37
38
|
enableReadyCheck: false,
|
|
39
|
+
lazyConnect: true, // Don't connect immediately
|
|
38
40
|
retryStrategy: (times) => {
|
|
39
41
|
if (times > 3)
|
|
40
42
|
return null;
|
|
@@ -57,7 +59,7 @@ const getClientInfo = (client) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
57
59
|
const startMonitoring = () => {
|
|
58
60
|
const monitorInterval = 60000; // 1 minute
|
|
59
61
|
setInterval(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
|
-
if (connectionPool.length > 0) {
|
|
62
|
+
if (connectionPool.length > 0 && activeConnections > 0) {
|
|
61
63
|
try {
|
|
62
64
|
const clientCount = yield getClientInfo(connectionPool[0]);
|
|
63
65
|
console.log(`[REDIS] Stats: Connected clients: ${clientCount}, Pool size: ${connectionPool.length}, Active: ${activeConnections}`);
|
|
@@ -69,8 +71,9 @@ const startMonitoring = () => {
|
|
|
69
71
|
}), monitorInterval);
|
|
70
72
|
};
|
|
71
73
|
// Initialize the connection pool
|
|
72
|
-
const initializePool = () => {
|
|
73
|
-
if (connectionPool.length === 0) {
|
|
74
|
+
const initializePool = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
if (connectionPool.length === 0 && !isInitializing) {
|
|
76
|
+
isInitializing = true;
|
|
74
77
|
const config = getRedisConfig();
|
|
75
78
|
for (let i = 0; i < MAX_CLIENTS; i++) {
|
|
76
79
|
const client = new ioredis_1.default(config);
|
|
@@ -90,22 +93,27 @@ const initializePool = () => {
|
|
|
90
93
|
}
|
|
91
94
|
// Start monitoring
|
|
92
95
|
startMonitoring();
|
|
96
|
+
isInitializing = false;
|
|
93
97
|
}
|
|
94
|
-
};
|
|
98
|
+
});
|
|
95
99
|
// Get a client from the pool using round-robin
|
|
96
|
-
const getRedisClient = () => {
|
|
100
|
+
const getRedisClient = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
97
101
|
if (connectionPool.length === 0) {
|
|
98
|
-
initializePool();
|
|
102
|
+
yield initializePool();
|
|
99
103
|
}
|
|
100
|
-
// Round-robin selection
|
|
104
|
+
// Round-robin selection (though with MAX_CLIENTS=1, this just returns the single client)
|
|
101
105
|
const client = connectionPool[currentConnectionIndex];
|
|
102
106
|
currentConnectionIndex = (currentConnectionIndex + 1) % connectionPool.length;
|
|
107
|
+
// Ensure the client is connected
|
|
108
|
+
if (client.status !== 'ready' && client.status !== 'connecting') {
|
|
109
|
+
yield client.connect();
|
|
110
|
+
}
|
|
103
111
|
return client;
|
|
104
|
-
};
|
|
112
|
+
});
|
|
105
113
|
exports.getRedisClient = getRedisClient;
|
|
106
114
|
const redisConnection = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
107
115
|
try {
|
|
108
|
-
const client = (0, exports.getRedisClient)();
|
|
116
|
+
const client = yield (0, exports.getRedisClient)();
|
|
109
117
|
return client;
|
|
110
118
|
}
|
|
111
119
|
catch (error) {
|
|
@@ -119,7 +127,7 @@ const sessionStore = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
119
127
|
if (redisStoreInstance) {
|
|
120
128
|
return redisStoreInstance;
|
|
121
129
|
}
|
|
122
|
-
const client = (0, exports.getRedisClient)();
|
|
130
|
+
const client = yield (0, exports.getRedisClient)();
|
|
123
131
|
redisStoreInstance = new connect_redis_1.default({ client });
|
|
124
132
|
return redisStoreInstance;
|
|
125
133
|
});
|