correctly close idle connections #920

This commit is contained in:
SPRINX0\prochazka
2024-10-14 14:03:07 +02:00
parent 06753ff312
commit f80ae284fa
5 changed files with 38 additions and 25 deletions

View File

@@ -20,9 +20,10 @@ function start() {
if (handleProcessCommunication(connection)) return; if (handleProcessCommunication(connection)) return;
try { try {
const driver = requireEngineDriver(connection); const driver = requireEngineDriver(connection);
const conn = await connectUtility(driver, connection, 'app'); const dbhan = await connectUtility(driver, connection, 'app');
const res = await driver.getVersion(conn); const res = await driver.getVersion(dbhan);
process.send({ msgtype: 'connected', ...res }); process.send({ msgtype: 'connected', ...res });
await driver.close(dbhan);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
process.send({ process.send({

View File

@@ -329,8 +329,9 @@ async function handleSqlPreview({ msgid, objects, options }) {
await generator.dump(); await generator.dump();
process.send({ msgtype: 'response', msgid, sql: dmp.s, isTruncated: generator.isTruncated }); process.send({ msgtype: 'response', msgid, sql: dmp.s, isTruncated: generator.isTruncated });
if (generator.isUnhandledException) { if (generator.isUnhandledException) {
setTimeout(() => { setTimeout(async () => {
logger.error('Exiting because of unhandled exception'); logger.error('Exiting because of unhandled exception');
await driver.close(dbhan);
process.exit(0); process.exit(0);
}, 500); }, 500);
} }
@@ -406,10 +407,12 @@ async function handleMessage({ msgtype, ...other }) {
function start() { function start() {
childProcessChecker(); childProcessChecker();
setInterval(() => { setInterval(async () => {
const time = new Date().getTime(); const time = new Date().getTime();
if (time - lastPing > 40 * 1000) { if (time - lastPing > 40 * 1000) {
logger.info('Database connection not alive, exiting'); logger.info('Database connection not alive, exiting');
const driver = requireEngineDriver(storedConnection);
await driver.close(dbhan);
process.exit(0); process.exit(0);
} }
}, 10 * 1000); }, 10 * 1000);

View File

@@ -6,7 +6,7 @@ const connectUtility = require('../utility/connectUtility');
const { handleProcessCommunication } = require('../utility/processComm'); const { handleProcessCommunication } = require('../utility/processComm');
const logger = getLogger('srvconnProcess'); const logger = getLogger('srvconnProcess');
let systemConnection; let dbhan;
let storedConnection; let storedConnection;
let lastDatabases = null; let lastDatabases = null;
let lastStatus = null; let lastStatus = null;
@@ -16,7 +16,7 @@ let afterConnectCallbacks = [];
async function handleRefresh() { async function handleRefresh() {
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
try { try {
let databases = await driver.listDatabases(systemConnection); let databases = await driver.listDatabases(dbhan);
if (storedConnection?.allowedDatabases?.trim()) { if (storedConnection?.allowedDatabases?.trim()) {
const allowedDatabaseList = storedConnection.allowedDatabases const allowedDatabaseList = storedConnection.allowedDatabases
.split('\n') .split('\n')
@@ -46,7 +46,7 @@ async function handleRefresh() {
async function readVersion() { async function readVersion() {
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
const version = await driver.getVersion(systemConnection); const version = await driver.getVersion(dbhan);
process.send({ msgtype: 'version', version }); process.send({ msgtype: 'version', version });
} }
@@ -70,7 +70,7 @@ async function handleConnect(connection) {
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
try { try {
systemConnection = await connectUtility(driver, storedConnection, 'app'); dbhan = await connectUtility(driver, storedConnection, 'app');
readVersion(); readVersion();
handleRefresh(); handleRefresh();
if (extractBoolSettingsValue(globalSettings, 'connection.autoRefresh', false)) { if (extractBoolSettingsValue(globalSettings, 'connection.autoRefresh', false)) {
@@ -95,7 +95,7 @@ async function handleConnect(connection) {
} }
function waitConnected() { function waitConnected() {
if (systemConnection) return Promise.resolve(); if (dbhan) return Promise.resolve();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
afterConnectCallbacks.push([resolve, reject]); afterConnectCallbacks.push([resolve, reject]);
}); });
@@ -108,14 +108,14 @@ function handlePing() {
async function handleDatabaseOp(op, { msgid, name }) { async function handleDatabaseOp(op, { msgid, name }) {
try { try {
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
systemConnection = await connectUtility(driver, storedConnection, 'app'); dbhan = await connectUtility(driver, storedConnection, 'app');
if (driver[op]) { if (driver[op]) {
await driver[op](systemConnection, name); await driver[op](dbhan, name);
} else { } else {
const dmp = driver.createDumper(); const dmp = driver.createDumper();
dmp[op](name); dmp[op](name);
logger.info({ sql: dmp.s }, 'Running script'); logger.info({ sql: dmp.s }, 'Running script');
await driver.query(systemConnection, dmp.s, { discardResult: true }); await driver.query(dbhan, dmp.s, { discardResult: true });
} }
await handleRefresh(); await handleRefresh();
@@ -137,11 +137,11 @@ async function handleDriverDataCore(msgid, callMethod) {
} }
async function handleServerSummary({ msgid }) { async function handleServerSummary({ msgid }) {
return handleDriverDataCore(msgid, driver => driver.serverSummary(systemConnection)); return handleDriverDataCore(msgid, driver => driver.serverSummary(dbhan));
} }
async function handleSummaryCommand({ msgid, command, row }) { async function handleSummaryCommand({ msgid, command, row }) {
return handleDriverDataCore(msgid, driver => driver.summaryCommand(systemConnection, command, row)); return handleDriverDataCore(msgid, driver => driver.summaryCommand(dbhan, command, row));
} }
const messageHandlers = { const messageHandlers = {
@@ -161,10 +161,13 @@ async function handleMessage({ msgtype, ...other }) {
function start() { function start() {
childProcessChecker(); childProcessChecker();
setInterval(() => { setInterval(async () => {
const time = new Date().getTime(); const time = new Date().getTime();
if (time - lastPing > 40 * 1000) { if (time - lastPing > 40 * 1000) {
logger.info('Server connection not alive, exiting'); logger.info('Server connection not alive, exiting');
const driver = requireEngineDriver(storedConnection);
await driver.close(dbhan);
process.exit(0); process.exit(0);
} }
}, 10 * 1000); }, 10 * 1000);

View File

@@ -14,7 +14,7 @@ const { getLogger, extractIntSettingsValue, extractBoolSettingsValue } = require
const logger = getLogger('sessionProcess'); const logger = getLogger('sessionProcess');
let systemConnection; let dbhan;
let storedConnection; let storedConnection;
let afterConnectCallbacks = []; let afterConnectCallbacks = [];
// let currentHandlers = []; // let currentHandlers = [];
@@ -177,7 +177,7 @@ function handleStream(driver, resultIndexHolder, sqlItem) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const start = sqlItem.trimStart || sqlItem.start; const start = sqlItem.trimStart || sqlItem.start;
const handler = new StreamHandler(resultIndexHolder, resolve, start && start.line); const handler = new StreamHandler(resultIndexHolder, resolve, start && start.line);
driver.stream(systemConnection, sqlItem.text, handler); driver.stream(dbhan, sqlItem.text, handler);
}); });
} }
@@ -196,7 +196,7 @@ async function handleConnect(connection) {
storedConnection = connection; storedConnection = connection;
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
systemConnection = await connectUtility(driver, storedConnection, 'app'); dbhan = await connectUtility(driver, storedConnection, 'app');
for (const [resolve] of afterConnectCallbacks) { for (const [resolve] of afterConnectCallbacks) {
resolve(); resolve();
} }
@@ -210,7 +210,7 @@ async function handleConnect(connection) {
// } // }
function waitConnected() { function waitConnected() {
if (systemConnection) return Promise.resolve(); if (dbhan) return Promise.resolve();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
afterConnectCallbacks.push([resolve, reject]); afterConnectCallbacks.push([resolve, reject]);
}); });
@@ -230,7 +230,7 @@ async function handleStartProfiler({ jslid }) {
const writer = new TableWriter(); const writer = new TableWriter();
writer.initializeFromReader(jslid); writer.initializeFromReader(jslid);
currentProfiler = await driver.startProfiler(systemConnection, { currentProfiler = await driver.startProfiler(dbhan, {
row: data => writer.rowFromReader(data), row: data => writer.rowFromReader(data),
}); });
currentProfiler.writer = writer; currentProfiler.writer = writer;
@@ -241,7 +241,7 @@ async function handleStopProfiler({ jslid }) {
const driver = requireEngineDriver(storedConnection); const driver = requireEngineDriver(storedConnection);
currentProfiler.writer.close(); currentProfiler.writer.close();
driver.stopProfiler(systemConnection, currentProfiler); driver.stopProfiler(dbhan, currentProfiler);
currentProfiler = null; currentProfiler = null;
} }
@@ -304,7 +304,7 @@ async function handleExecuteReader({ jslid, sql, fileName }) {
const writer = new TableWriter(); const writer = new TableWriter();
writer.initializeFromReader(jslid); writer.initializeFromReader(jslid);
const reader = await driver.readQuery(systemConnection, sql); const reader = await driver.readQuery(dbhan, sql);
reader.on('data', data => { reader.on('data', data => {
writer.rowFromReader(data); writer.rowFromReader(data);
@@ -340,10 +340,12 @@ function start() {
lastPing = new Date().getTime(); lastPing = new Date().getTime();
setInterval(() => { setInterval(async () => {
const time = new Date().getTime(); const time = new Date().getTime();
if (time - lastPing > 25 * 1000) { if (time - lastPing > 25 * 1000) {
logger.info('Session not alive, exiting'); logger.info('Session not alive, exiting');
const driver = requireEngineDriver(storedConnection);
await driver.close(dbhan);
process.exit(0); process.exit(0);
} }
@@ -362,6 +364,8 @@ function start() {
executingScripts == 0 executingScripts == 0
) { ) {
logger.info('Session not active, exiting'); logger.info('Session not active, exiting');
const driver = requireEngineDriver(storedConnection);
await driver.close(dbhan);
process.exit(0); process.exit(0);
} }
}, 10 * 1000); }, 10 * 1000);

View File

@@ -67,8 +67,10 @@ const drivers = driverBases.map(driverBase => ({
} }
return dbhan; return dbhan;
}, },
async close(dbhan) { close(dbhan) {
return dbhan.client.close(); return new Promise(resolve => {
dbhan.client.end(resolve);
});
}, },
query(dbhan, sql, options) { query(dbhan, sql, options) {
if (sql == null) { if (sql == null) {