syslog-portal
Version:
A portal for ingesting syslog data
119 lines • 4.67 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { MongoClient } from 'mongodb';
import { inject, injectable, preDestroy } from 'inversify';
import genericPool from 'generic-pool';
import { TYPES } from '../types.js';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';
let MongoConnPool = class MongoConnPool {
pool;
config;
log;
constructor(config, log) {
log.info('Creating mongo connection pool');
this.pool = genericPool.createPool(this, {
min: 0,
max: 10,
autostart: true,
acquireTimeoutMillis: 10000,
idleTimeoutMillis: 30000,
evictionRunIntervalMillis: 10000,
});
this.config = config;
this.log = log;
}
getUrl() {
return `mongodb://${this.config.archiver.hostname}:${this.config.archiver.port}/`;
}
async getUsername() {
if (this.config.archiver?.usernameFile == null)
return Promise.resolve(this.config.archiver.username);
this.log.trace('Reading username from file %s', this.config.archiver.usernameFile);
return readFile(resolve(this.config.archiver.usernameFile), {
encoding: 'utf8',
});
}
async getPassword() {
if (this.config.archiver?.passwordFile == null)
return Promise.resolve(this.config.archiver.password);
this.log.trace('Reading password from file %s', this.config.archiver.passwordFile);
return readFile(resolve(this.config.archiver.passwordFile), {
encoding: 'utf8',
});
}
async getOptions() {
return {
auth: {
username: await this.getUsername(),
password: await this.getPassword(),
},
...this.config.archiver.options,
};
}
async create() {
const url = this.getUrl();
this.log.trace('Fetching mongodb options');
const options = await this.getOptions();
this.log.debug('Allocating new mongodb connection to %s', url);
return new MongoClient(url, options).connect().catch((err) => {
this.log.error(err, 'Error occurred while connecting to mongodb %s', err.message);
throw err;
});
}
async destroy(mongoClient) {
this.log.info('Destroying mongodb connection');
await mongoClient.close(true).catch((err) => {
this.log.error(err, 'Error occurred while closing connection to mongodb %s', err.message);
throw err;
});
this.log.info('Finished destroying mongodb connection');
}
connect() {
this.log.debug('Leasing connection');
return this.pool.acquire();
}
release(conn) {
this.log.debug('Releasing connection');
return this.pool.release(conn);
}
count() {
return this.pool.size;
}
async clearAll() {
try {
this.log.info('Draining connection pool');
await this.pool.drain();
this.log.info('Clearing connection pool');
await this.pool.clear();
this.log.info('Finished destroying connection pool');
}
catch (err) {
this.log.error(err, `Error while clearing connections: ${err}`);
}
}
};
__decorate([
preDestroy(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], MongoConnPool.prototype, "clearAll", null);
MongoConnPool = __decorate([
injectable(),
__param(0, inject(TYPES.Configurations.Main)),
__param(1, inject(TYPES.Logger)),
__metadata("design:paramtypes", [Object, Object])
], MongoConnPool);
export { MongoConnPool };
//# sourceMappingURL=mongoConnectionPool.js.map