Mám lambda funkciu pomocou Uzla 12.
Musím pridať nové pripojenie do Redis databáze hostil v AWS ElastiCache.
Obaja sú v jednej súkromnej VPC a skupiny zabezpečenia/podsietí sú správne nakonfigurované.
Riešenie:
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 (mimo handler):
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);
}
});
};
};
Volanie funkcie:
app.REDIS.get(redisKey, (err, reply) => {
console.log(`REDIS GET: ${err} ${reply}`);
});
Problém: Pri zvyšovaní lambda časový limit na hodnotu väčšiu ako Redis časového limitu, som si tejto chyby:
REDIS KLIENTA CHYBA:Chyba: Redis pripojenie na ... nepodarilo sa - pripojenie ETIMEDOUT ...
Doplnenie:
Snažil som sa quiting/ukončenia spojenia po každej transakcie:
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()
});
}
}
})
Chyba:
REDIS ZÍSKAŤ: AbortError: DOSTAŤ nemôže byť spracované. Pripojenie je už uzavretá.
Ďalšie Poznámky:
- Musím použiť volania, to je dôvod, prečo som sa prejsť tie v uvedených príkladoch
- Ja používam
"redis": "^3.0.2"
- To nie je konfigurácia problém ako pamäť sa dostali sto krát v malej lehote, ale potom začal dávať časový limit chyby.
- Všetko funguje normálne, lokálne