First memory leak I needed to debug on Nodejs/JS, so some newbie questions here. I wanted to get some advice on code design..specifically:
I have a connection a mysql database, and I leave it open as a global variable, so that I don't have to establish connection and tear down. I have frequent access requests. I have connection to google services, and similarly, establish a connection object at global level so that I don't have to re-connect each time and delay users. For 1&2, above, are there any negative concerns I should have on memory leaks? (vs. having them invoked and torn down within a function scope)
snippets:
import mysql from 'mysql';
const connection = mysql.createConnection({
host: 'localhost',
user: 'xxxx',
password: 'xxx',
database: 'xxx' ,
charset: 'utf8mb4'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
...
export {connection as default};
and here is the other:
import config from './config.json';
import Discord from 'discord.js';
//discord bot
const bot = new Discord.Client();
// We also need to make sure we're attaching the config to the CLIENT so it's accessible everywhere!
bot.config = config;
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
bot.help = new Discord.Collection();
bot.login(config.token);
export default bot;
and the google connection
// Imports the Google Cloud client library
import { TranslationServiceClient } from '@google-cloud/translate';
// instantiate an instance of a connection to google translate API
const translationClient = new TranslationServiceClient();
Wondering if there is a different/better way to approach this?