@pioneer-platform/default-redis 8.11.0 → 8.11.2
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.
- package/CHANGELOG.md +12 -0
- package/index.js +70 -17
- package/package.json +2 -3
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -7,29 +7,82 @@
|
|
|
7
7
|
const TAG = " | REDIS-CONNECTION-MODULE | "
|
|
8
8
|
//const log = require('@pioneer-platform/loggerdog')()
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
let Redis
|
|
12
|
-
if(process.env.NODE_ENV === 'test'){
|
|
13
|
-
Redis = require('ioredis-mock');
|
|
14
|
-
}else{
|
|
15
|
-
Redis = require("ioredis");
|
|
16
|
-
}
|
|
17
|
-
|
|
10
|
+
const Redis = require("ioredis");
|
|
18
11
|
|
|
19
12
|
let redis
|
|
20
|
-
let redisQueue
|
|
13
|
+
let redisQueue // Dedicated client for blocking operations (brpop, blpop, etc.)
|
|
21
14
|
let publisher
|
|
22
15
|
let subscriber
|
|
23
16
|
|
|
17
|
+
// Redis connection config with IPv4 enforcement and extended timeouts
|
|
18
|
+
// IPv4 enforcement prevents 10-second IPv6 timeout fallback
|
|
19
|
+
// Extended timeouts accommodate DNS resolution and network latency
|
|
20
|
+
const redisConfig = {
|
|
21
|
+
host: '127.0.0.1', // Force IPv4, not "localhost" which tries ::1 first
|
|
22
|
+
port: 6379,
|
|
23
|
+
family: 4, // Force IPv4 socket family
|
|
24
|
+
connectTimeout: 10000, // Increased from 1500ms to 10000ms for reliable connection
|
|
25
|
+
commandTimeout: 10000, // Timeout for individual Redis commands (GET, SET, etc.)
|
|
26
|
+
keepAlive: 5000, // TCP keep-alive interval
|
|
27
|
+
maxRetriesPerRequest: 3, // Increased from 1 to 3 - allow retries for transient network issues
|
|
28
|
+
enableReadyCheck: true,
|
|
29
|
+
enableOfflineQueue: true, // Queue commands when disconnected to prevent crashes during startup
|
|
30
|
+
retryStrategy(times) {
|
|
31
|
+
const delay = Math.min(times * 50, 2000);
|
|
32
|
+
console.log(TAG, `Redis reconnect attempt ${times} in ${delay}ms`);
|
|
33
|
+
return delay;
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Override with connection string if provided
|
|
38
|
+
if (process.env.REDIS_CONNECTION) {
|
|
39
|
+
console.log(TAG, "Using REDIS_CONNECTION from env:", process.env.REDIS_CONNECTION);
|
|
40
|
+
const connUrl = new URL(process.env.REDIS_CONNECTION);
|
|
41
|
+
|
|
42
|
+
// Merge with base config to preserve timeouts and IPv4 enforcement
|
|
43
|
+
Object.assign(redisConfig, {
|
|
44
|
+
host: connUrl.hostname,
|
|
45
|
+
port: parseInt(connUrl.port) || 6379,
|
|
46
|
+
password: connUrl.password || undefined,
|
|
47
|
+
username: (connUrl.username && connUrl.username !== 'default') ? connUrl.username : undefined,
|
|
48
|
+
});
|
|
24
49
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
50
|
+
console.log(TAG, "Merged config with IPv4 enforcement:", {
|
|
51
|
+
host: redisConfig.host,
|
|
52
|
+
port: redisConfig.port,
|
|
53
|
+
family: redisConfig.family,
|
|
54
|
+
connectTimeout: redisConfig.connectTimeout,
|
|
55
|
+
commandTimeout: redisConfig.commandTimeout
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
console.log(TAG, "Connecting to Redis on 127.0.0.1:6379 (IPv4 only)");
|
|
28
59
|
}
|
|
29
60
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
61
|
+
// Create Redis clients
|
|
62
|
+
redis = new Redis(redisConfig);
|
|
63
|
+
publisher = new Redis(redisConfig);
|
|
64
|
+
subscriber = new Redis(redisConfig);
|
|
65
|
+
|
|
66
|
+
// CRITICAL: Dedicated client for blocking operations (brpop, blpop, bzpopmin, etc.)
|
|
67
|
+
// Blocking operations on the main redis client will block ALL cache read/write operations
|
|
68
|
+
// This was the root cause of 2-10 second cache GET timeouts
|
|
69
|
+
redisQueue = new Redis(redisConfig);
|
|
70
|
+
|
|
71
|
+
// Event handlers
|
|
72
|
+
redis.on('connect', () => console.log(TAG, 'Main Redis client connected'));
|
|
73
|
+
redis.on('ready', () => console.log(TAG, 'Main Redis client ready'));
|
|
74
|
+
redis.on('error', (err) => console.error(TAG, 'Main Redis error:', err.message));
|
|
75
|
+
|
|
76
|
+
redisQueue.on('connect', () => console.log(TAG, 'Queue Redis client connected'));
|
|
77
|
+
redisQueue.on('ready', () => console.log(TAG, 'Queue Redis client ready'));
|
|
78
|
+
redisQueue.on('error', (err) => console.error(TAG, 'Queue Redis error:', err.message));
|
|
79
|
+
|
|
80
|
+
publisher.on('connect', () => console.log(TAG, 'Publisher client connected'));
|
|
81
|
+
subscriber.on('connect', () => console.log(TAG, 'Subscriber client connected'));
|
|
34
82
|
|
|
35
|
-
module.exports = {
|
|
83
|
+
module.exports = {
|
|
84
|
+
redis,
|
|
85
|
+
publisher,
|
|
86
|
+
subscriber,
|
|
87
|
+
redisQueue // Export dedicated queue client for blocking operations
|
|
88
|
+
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pioneer-platform/default-redis",
|
|
3
|
-
"version": "8.11.
|
|
3
|
+
"version": "8.11.2",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"@pioneer-platform/loggerdog": "^8.11.0",
|
|
6
6
|
"dotenv": "^8.2.0",
|
|
7
|
-
"
|
|
8
|
-
"ioredis-mock": "^4.21.1"
|
|
7
|
+
"redis": "^5.9.0"
|
|
9
8
|
},
|
|
10
9
|
"gitHead": "aeae28273014ab69b42f22abec159c6693a56c40"
|
|
11
10
|
}
|