reduce memory footprint by reusing object instances

Signed-off-by: Travis Glenn Hansen <travisghansen@yahoo.com>
This commit is contained in:
Travis Glenn Hansen
2022-04-06 11:14:39 -06:00
parent 1783aebb75
commit 1f2c8cf58d
19 changed files with 412 additions and 229 deletions
+5 -5
View File
@@ -610,17 +610,17 @@ class Filesystem {
const filesystem = this;
args = args || [];
let stdout = "";
let stderr = "";
if (filesystem.options.sudo) {
args.unshift(command);
command = filesystem.options.paths.sudo;
}
console.log("executing filesystem command: %s %s", command, args.join(" "));
const child = filesystem.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = filesystem.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});
+5 -4
View File
@@ -523,17 +523,18 @@ class ISCSI {
const iscsi = this;
args = args || [];
let stdout = "";
let stderr = "";
if (iscsi.options.sudo) {
args.unshift(command);
command = iscsi.options.paths.sudo;
}
console.log("executing iscsi command: %s %s", command, args.join(" "));
const child = iscsi.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = iscsi.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});
+11 -8
View File
@@ -1,3 +1,5 @@
const os = require("os");
/**
* Levels
*
@@ -27,7 +29,7 @@ let formatters;
let defaultMeta;
if (env == "production") {
formatters = [winston.format.json()];
defaultMeta = { service: "democratic-csi" };
defaultMeta = { service: "democratic-csi", host: os.hostname() };
} else {
formatters = [winston.format.colorize(), winston.format.simple()];
defaultMeta = {};
@@ -36,6 +38,7 @@ if (env == "production") {
const logger = winston.createLogger({
level: level,
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.splat(),
...formatters
@@ -43,9 +46,9 @@ const logger = winston.createLogger({
defaultMeta: defaultMeta,
transports: [
new winston.transports.Console({
handleExceptions: true
})
]
handleExceptions: true,
}),
],
});
/**
@@ -102,9 +105,9 @@ var shim = bunyan.createLogger({
{
type: "raw",
level: "trace",
stream: new Bunyan2Winston(logger)
}
]
stream: new Bunyan2Winston(logger),
},
],
});
logger.bunyan = shim;
@@ -112,5 +115,5 @@ logger.bunyan = shim;
//global.console = logger;
module.exports = {
logger: logger
logger: logger,
};
+19 -10
View File
@@ -2,7 +2,7 @@ const cp = require("child_process");
const { Filesystem } = require("../utils/filesystem");
// avoid using avail,size,used as it causes hangs when the fs is stale
FINDMNT_COMMON_OPTIONS = [
const FINDMNT_COMMON_OPTIONS = [
"--output",
"source,target,fstype,label,options",
"-b",
@@ -43,6 +43,14 @@ class Mount {
spawn: cp.spawn,
};
}
if (!options.filesystem) {
options.filesystem = new Filesystem();
}
}
async getFilesystemInstance() {
return this.options.filesystem;
}
/**
@@ -51,12 +59,12 @@ class Mount {
* @param {*} device
*/
async deviceIsMounted(device) {
const filesystem = new Filesystem();
const mount = this;
const filesystem = await mount.getFilesystemInstance();
if (device.startsWith("/")) {
device = await filesystem.realpath(device);
}
const mount = this;
let args = [];
args = args.concat(["--source", device]);
args = args.concat(FINDMNT_COMMON_OPTIONS);
@@ -114,12 +122,12 @@ class Mount {
* @param {*} device
*/
async deviceIsMountedAtPath(device, path) {
const filesystem = new Filesystem();
const mount = this;
const filesystem = await mount.getFilesystemInstance();
if (device.startsWith("/") && !device.startsWith("//")) {
device = await filesystem.realpath(device);
}
const mount = this;
let args = [];
args = args.concat(["--source", device]);
args = args.concat(["--mountpoint", path]);
@@ -282,8 +290,8 @@ class Mount {
* @param {*} path
*/
async isBindMountedBlockDevice(path) {
const filesystem = new Filesystem();
const mount = this;
const filesystem = await mount.getFilesystemInstance();
const is_mounted = await mount.pathIsMounted(path);
if (!is_mounted) {
@@ -385,9 +393,6 @@ class Mount {
const mount = this;
args = args || [];
let stdout = "";
let stderr = "";
if (mount.options.sudo) {
args.unshift(command);
command = mount.options.paths.sudo;
@@ -402,9 +407,13 @@ class Mount {
);
console.log("executing mount command: %s", cleansedLog);
const child = mount.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = mount.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});
+5 -4
View File
@@ -77,9 +77,6 @@ class OneClient {
const oneclient = this;
args = args || [];
let stdout = "";
let stderr = "";
if (oneclient.options.sudo) {
args.unshift(command);
command = oneclient.options.paths.sudo;
@@ -93,9 +90,13 @@ class OneClient {
);
console.log("executing oneclient command: %s", cleansedLog);
const child = oneclient.options.executor.spawn(command, args, options);
return new Promise((resolve, reject) => {
const child = oneclient.options.executor.spawn(command, args, options);
let stdout = "";
let stderr = "";
child.stdout.on("data", function (data) {
stdout = stdout + data;
});
+37
View File
@@ -0,0 +1,37 @@
class Registry {
constructor() {
this.data = {};
}
put(key, value) {
if (!value) {
delete this.data[key];
return;
}
this.data[key] = value;
}
get(key, initialValue = null) {
const val = this.data[key];
if (val) {
return val;
}
if (typeof initialValue == "function") {
initialValue = initialValue();
}
if (initialValue) {
this.put(key, initialValue);
return this.data[key];
}
}
delete(key) {
delete this.data[key];
}
}
const registry = new Registry();
module.exports = registry;