mm_redis 1.9.9 → 2.0.0
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/README.md +1 -1
- package/index.js +58 -28
- package/package.json +1 -1
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -39,17 +39,18 @@ class Redis extends BaseService {
|
|
|
39
39
|
// 重试次数
|
|
40
40
|
"retry_count": 3,
|
|
41
41
|
// 重试间隔(ms)
|
|
42
|
-
"retry_delay": 1000
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
config: {
|
|
50
|
-
max_clients: 10 // 最大连接数
|
|
42
|
+
"retry_delay": 1000,
|
|
43
|
+
// 连接池配置
|
|
44
|
+
"pool": {
|
|
45
|
+
"max_clients": 100, // 最大连接数
|
|
46
|
+
"min_idle": 5, // 最小空闲连接数
|
|
47
|
+
"max_idle": 20, // 最大空闲连接数
|
|
48
|
+
"idle_timeout": 30000 // 空闲超时时间(ms)
|
|
51
49
|
}
|
|
52
50
|
};
|
|
51
|
+
this.setConfig(config);
|
|
52
|
+
// 初始化连接池
|
|
53
|
+
this._initPool();
|
|
53
54
|
// 唯一标识符
|
|
54
55
|
this.identifier = this.config.host + "/" + this.config.database;
|
|
55
56
|
// 数据库连接器
|
|
@@ -167,28 +168,53 @@ Redis.prototype.message = function(channel, message) {
|
|
|
167
168
|
* @description 设置配置
|
|
168
169
|
* @param {Object} config 配置对象或配置路径
|
|
169
170
|
*/
|
|
170
|
-
// 修复setConfig方法,移除其中的_initPool定义
|
|
171
171
|
Redis.prototype.setConfig = function(config) {
|
|
172
172
|
let obj = {};
|
|
173
173
|
if (typeof config === 'string') {
|
|
174
174
|
obj = config.loadJson(__dirname);
|
|
175
175
|
} else if (config) {
|
|
176
176
|
// 合并配置参数
|
|
177
|
-
|
|
177
|
+
if (config.pool) {
|
|
178
|
+
// 确保连接池配置的合并不会覆盖所有默认值
|
|
179
|
+
if (!this.config.pool) {
|
|
180
|
+
this.config.pool = {};
|
|
181
|
+
}
|
|
182
|
+
Object.assign(this.config.pool, config.pool);
|
|
183
|
+
// 删除config中的pool属性,避免后续Object.assign重复处理
|
|
184
|
+
const configCopy = { ...config };
|
|
185
|
+
delete configCopy.pool;
|
|
186
|
+
Object.assign(this.config, configCopy);
|
|
187
|
+
} else {
|
|
188
|
+
Object.assign(this.config, config);
|
|
189
|
+
}
|
|
178
190
|
} else {
|
|
179
191
|
return;
|
|
180
192
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
// 检查是否存在连接池配置,如果没有则使用默认配置
|
|
193
|
+
|
|
194
|
+
// 确保连接池配置存在且包含所有必要字段
|
|
184
195
|
if (!this.config.pool) {
|
|
185
|
-
this.config.pool = {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
196
|
+
this.config.pool = {};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// 设置默认连接池配置,只覆盖未定义的字段
|
|
200
|
+
const defaultPoolConfig = {
|
|
201
|
+
max_clients: 100,
|
|
202
|
+
min_idle: 5,
|
|
203
|
+
max_idle: 20,
|
|
204
|
+
idle_timeout: 30000
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
for (const key in defaultPoolConfig) {
|
|
208
|
+
if (this.config.pool[key] === undefined) {
|
|
209
|
+
this.config.pool[key] = defaultPoolConfig[key];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// 如果连接池已经初始化,更新其配置
|
|
214
|
+
if (this.connection_pool) {
|
|
215
|
+
this.connection_pool.config = this.config.pool;
|
|
191
216
|
}
|
|
217
|
+
|
|
192
218
|
// 初始化连接池
|
|
193
219
|
this._initPool();
|
|
194
220
|
$.log.debug('[Redis] 配置已更新');
|
|
@@ -198,20 +224,25 @@ Redis.prototype.setConfig = function(config) {
|
|
|
198
224
|
|
|
199
225
|
// 添加连接池初始化方法
|
|
200
226
|
Redis.prototype._initPool = function() {
|
|
227
|
+
// 确保连接池配置存在
|
|
228
|
+
if (!this.config.pool) {
|
|
229
|
+
this.config.pool = {
|
|
230
|
+
max_clients: 100,
|
|
231
|
+
min_idle: 5,
|
|
232
|
+
max_idle: 20,
|
|
233
|
+
idle_timeout: 30000
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
201
237
|
// 初始化连接池相关属性
|
|
202
238
|
this.connection_pool = {
|
|
203
239
|
available: [],
|
|
204
240
|
in_use: new Set(),
|
|
205
|
-
config: this.config.pool
|
|
206
|
-
max_clients: 10,
|
|
207
|
-
min_idle: 2,
|
|
208
|
-
max_idle: 5,
|
|
209
|
-
idle_timeout: 30000
|
|
210
|
-
}
|
|
241
|
+
config: this.config.pool // 直接引用配置对象,确保同步更新
|
|
211
242
|
};
|
|
212
243
|
|
|
213
244
|
// 记录连接池初始化
|
|
214
|
-
$.log.debug('[Redis]
|
|
245
|
+
$.log.debug('[Redis] 连接池已初始化,配置:', this.config.pool);
|
|
215
246
|
};
|
|
216
247
|
|
|
217
248
|
/**
|
|
@@ -2620,7 +2651,6 @@ Redis.prototype._releaseConnection = function(connection) {
|
|
|
2620
2651
|
// 检查是否超过最大空闲连接数
|
|
2621
2652
|
if (this.connection_pool.available.length < this.connection_pool.config.max_idle) {
|
|
2622
2653
|
this.connection_pool.available.push(connection);
|
|
2623
|
-
$.log.debug('[Redis] 连接已释放回池中:', connection.id);
|
|
2624
2654
|
} else {
|
|
2625
2655
|
// 否则关闭多余的连接
|
|
2626
2656
|
try {
|