UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

100 lines (97 loc) 4.43 kB
"use strict"; /* Copyright 2017-2021 Norman Breau Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MySQLDatabase = void 0; const tslib_1 = require("tslib"); const Database_1 = require("./Database"); const MySQLConnection_1 = require("./MySQLConnection"); const MySQL = tslib_1.__importStar(require("mysql2")); const instance_1 = require("./instance"); const ConnectionReplicationWaiter_1 = require("./private/ConnectionReplicationWaiter"); const MetricStore_1 = require("./MetricStore"); const TAG = 'MySQLDatabase'; class MySQLDatabase extends Database_1.Database { constructor() { super(); // TODO: Maybe one day this may be exposed via a bt config setting. this.$cluster = MySQL.createPoolCluster({ removeNodeErrorCount: Infinity, restoreNodeTimeout: 1000 }); this.$cluster.on('enqueue', () => { (0, instance_1.getInstance)().getLogger().warn(TAG, 'Waiting for available connection...'); }); this.$activeConnectionsGauge = MetricStore_1.MetricStore.getInstance().createGauge({ name: 'storm_mysql_active_connections', help: 'Number of MySQL connections currently claimed from the pool' }); } escape(value) { return MySQLDatabase.escape(value); } static escape(value) { return MySQL.escape(value); } _addNode(nodeID, config) { (0, instance_1.getInstance)().getLogger().trace(TAG, `Adding node to connection pool: "${nodeID}"`); this.$cluster.add(nodeID, config); } _removeNode(nodeID) { (0, instance_1.getInstance)().getLogger().trace(TAG, `Removing node to connection pool: "${nodeID}"`); this.$cluster.remove(nodeID); } async _destroy() { this.$cluster.end(); } $getConnectionFromPool(query, requireWriteAccess, instantationStack) { return new Promise((resolve, reject) => { this.$cluster.getConnection(query, (error, connection) => { if (error) { reject(error); return; } this.$activeConnectionsGauge.inc(); resolve(new MySQLConnection_1.MySQLConnection(connection, instantationStack, !requireWriteAccess, this.$activeConnectionsGauge)); }); }); } async _getConnection(query, requireWriteAccess, position) { let logger = (0, instance_1.getInstance)().getLogger(); logger.trace(TAG, `Querying connection pool for "${query}".`); let instantationStack = new Error().stack; let conn = await this.$getConnectionFromPool(query, requireWriteAccess, instantationStack); await conn.__internal_init(); logger.trace(TAG, `Replication Enabled: ${conn.hasReplicationEnabled() ? 'true' : 'false'}`); logger.trace(TAG, `Connection Replicating: ${conn.isReplication() ? 'true' : 'false'}`); if (conn.hasReplicationEnabled() && conn.isReplication() && position && position.page && position.position) { logger.trace(TAG, 'Connection is waiting on Replication'); // master connections will not wait on database positions // they are guarenteed to be at the tip. // readonly, or otherwise known as replication connections // may have replication lag. If we have a desired position target, // then await for the connection to catch up to that target. let waiter = new ConnectionReplicationWaiter_1.ConnectionReplicationWaiter(conn); try { await waiter.wait(position); } catch (ex) { void conn.close(true); throw ex; } } return conn; } } exports.MySQLDatabase = MySQLDatabase; //# sourceMappingURL=MySQLDatabase.js.map