我有一个lambda功能使用节点12。
我需要添加一个新的连接到一个穿红衣的数据库托管。基.
两者都是在一个私人VPC和安全组/子网是正确配置。
方案:
globals.js:
const redis = require('redis');
const redisClient = redis.createClient(
`redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/${process.env.REDIS_DB}`,
);
redisClient.on('error', (err) => {
console.log('REDIS CLIENT ERROR:' + err);
});
module.exports.globals = {
REDIS: require('../helpers/redis')(redisClient),
};
index.js (外处理程序):
const { globals } = require('./config/globals');
global.app = globals;
const lambda_handler = (event, context, callback) => { ... }
exports.handler = lambda_handler;
helpers/redis/index.js:
const get = require('./get');
module.exports = (redisClient) => {
return {
get: get(redisClient)
};
};
helpers/redis/get.js:
module.exports = (redisClient) => {
return (key, cb) => {
redisClient.get(key, (err, reply) => {
if (err) {
cb(err);
} else {
cb(null, reply);
}
});
};
};
功能的电话:
app.REDIS.get(redisKey, (err, reply) => {
console.log(`REDIS GET: ${err} ${reply}`);
});
问题: 当增加lambda超时值大于穿红衣的超时,我得到这个错误:
穿红衣客户错误:错误:穿红衣连接到...失败连接ETIMEDOUT...
此外:
我想退出/关闭连接之后,每个交易:
module.exports = (redisClient) => {
return (cb) => {
redisClient.quit((err, reply) => {
if (err) {
cb(err);
} else {
cb(null, reply);
}
});
};
};
app.REDIS.get(redisKey, (err, reply) => {
console.log(`REDIS GET: ${err} ${reply}`);
if (err) {
cb(err);
} else {
if (reply) {
app.REDIS.quit(() => {
cb()
});
}
}
})
错误:
穿红衣获得:AbortError:获得不可能处理。 连接已经关闭。
额外注意:
- 我有用的回调,这就是为什么我通过那些在上述实例
- 我在使用
"redis": "^3.0.2"
- 这不是一个配置问题作为缓访问百次一小段时间,但它然后开始给予的超时的错误。
- 一切正常,通常在本地