mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
add a bunch options for les.js
This commit is contained in:
parent
88316e78fb
commit
38b65ff9ea
78
lib/les.js
78
lib/les.js
@ -13,18 +13,56 @@
|
||||
// Garbage Collector for Gun
|
||||
// Originally By: Collin Conrad (@masterex1000)
|
||||
|
||||
//NOTE: set to false is running from file in YOUR code
|
||||
/**
|
||||
*
|
||||
* Usage: require the file in your application
|
||||
*
|
||||
* Gun Params: these are passed to the new gun constructor
|
||||
*
|
||||
* - gc_enable : enables the gc, good if you are running multiple instances of gun, etc... def. true
|
||||
* - gc_delay : sets the amount of time between attempted garbage collections in milliseconds
|
||||
* - gc_info_enable : Enables or Disables the info printout
|
||||
* - gc_info : sets the ~ amount of time between info messages
|
||||
* this is checked everytime the gc is ran
|
||||
* - gc_info_mini : this will use a smaller, less user friendly info printout
|
||||
* - gc_importance_func : This will be the function used for finding the importance of a potental collect
|
||||
* takes the form of func(timestamp, ctime, memoryUsageRatio) {return val}
|
||||
* Collects when returned value is 100
|
||||
*/
|
||||
|
||||
//NOTE: set to false is running from file in YOUR code DEFUALT: false
|
||||
var USELOCALGUN = true;
|
||||
|
||||
|
||||
//NOTE: adds some debug messages
|
||||
var DEBUG = false;
|
||||
//NOTE: adds some debug messages DEFUALT: false
|
||||
var DEBUG = true;
|
||||
|
||||
if(!(typeof window !== "undefined") && USELOCALGUN)
|
||||
console.log("NOTE: You currently have LES.js set to use the 'local' file version of gun, This might crash if set wrong!");
|
||||
|
||||
var Gun = (typeof window !== "undefined") ? window.Gun : (USELOCALGUN ? require('../gun') : require("gun"));
|
||||
var ev = {};
|
||||
var empty = {};
|
||||
|
||||
Gun.on('opt', function(root) {
|
||||
|
||||
//Setup various options
|
||||
|
||||
const gc_enable = root.opt.gc_enable ? root.opt.gc_enable : true;
|
||||
const gc_delay = root.opt.gc_delay ? root.opt.gc_delay : 1000;
|
||||
|
||||
const gc_info_enable = root.opt.gc_info_enable ? root.opt.gc_info_enable : true;
|
||||
const gc_info = root.opt.gc_info ? root.opt.gc_info : 5000;
|
||||
const gc_info_mini = root.opt.gc_info_mini ? root.opt.gc_info_mini : false;
|
||||
|
||||
//This is kindof long but it works
|
||||
const calcRemoveImportance = root.opt.gc_importance_func ? root.opt.gc_importance_func : function (timestamp, ctime, memoryUsageRatio) {
|
||||
var time = (ctime - timestamp) * 0.001;
|
||||
return time * 10 * (memoryUsageRatio * memoryUsageRatio);
|
||||
}
|
||||
|
||||
if(DEBUG) console.log(root.opt);
|
||||
|
||||
this.to.next(root);
|
||||
if (root.once)
|
||||
return;
|
||||
@ -32,6 +70,9 @@
|
||||
return
|
||||
var mem = process.memoryUsage;
|
||||
|
||||
if(!gc_enable) // exit because the gc is disabled
|
||||
return;
|
||||
|
||||
if (!mem) //exit because we are in the browser
|
||||
return;
|
||||
|
||||
@ -49,7 +90,7 @@
|
||||
}, 1);
|
||||
}
|
||||
|
||||
setInterval(check, 1000); // set the garbage collector to run every second, TODO: make configurable
|
||||
setInterval(check, gc_delay); // set the garbage collector to run every second, TODO: make configurable
|
||||
|
||||
//Executed every time a node gets modifyed
|
||||
root.on("put", function(e) {
|
||||
@ -60,6 +101,18 @@
|
||||
}
|
||||
});
|
||||
|
||||
//Removes a node from the queue
|
||||
function dequeueNode(soul) {
|
||||
if (nodes[soul] == true) { //The node already exists in the queue
|
||||
var index = nodesArray.findIndex(function(e) {
|
||||
return e[0] === soul;
|
||||
});
|
||||
if (index != -1) {
|
||||
nodesArray.splice(index, 1); // remove the existing ref.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Adds a soul the garbage collectors "freeing" queue
|
||||
function enqueueNode(soul, ctime) {
|
||||
if (nodes[soul] == true) { //The node already exists in the queue
|
||||
@ -70,7 +123,7 @@
|
||||
console.err("Something happened and the node '" + soul + "' won't get garbage collection unless the value is updated agian");
|
||||
return;
|
||||
} else {
|
||||
nodesArray.splice(index, 1); // remove the existing ref.
|
||||
nodesArray.splice(index, 1); // remove the existing ref. faster than dequeue
|
||||
nodesArray.push([soul, ctime]); // push the new instance
|
||||
}
|
||||
} else {
|
||||
@ -83,8 +136,11 @@
|
||||
function GC(memRatio) {
|
||||
var curTime = Date.now(); // get the current time
|
||||
|
||||
if (curTime - memoryUpdate >= 5000) {
|
||||
console.log("|GC| %s | Current Memory Ratio: %d | Current Ram Usage %sMB | Nodes in Memory %s", new Date().toLocaleString(), round(memRatio, 2), round(ev.used, 2), Object.keys(root.graph || empty).length);
|
||||
if (gc_info_enable && curTime - memoryUpdate >= gc_info) {
|
||||
if(!gc_info_mini)
|
||||
console.log("|GC| %s | Current Memory Ratio: %d | Current Ram Usage %sMB | Nodes in Memory %s", new Date().toLocaleString(), round(memRatio, 2), round(ev.used, 2), Object.keys(root.graph || empty).length);
|
||||
else
|
||||
console.log("|GC| %s, Mem Ratio %d, Ram %sMB, Nodes in mem %s", new Date().toLocaleString(), round(memRatio, 2), round(ev.used, 2), Object.keys(root.graph || empty).length);
|
||||
memoryUpdate = curTime;
|
||||
}
|
||||
|
||||
@ -109,10 +165,10 @@
|
||||
}
|
||||
|
||||
//Generates a number that, after it hits a threshold, the node gets removed
|
||||
function calcRemoveImportance(timestamp, ctime, memoryUsageRatio) {
|
||||
var time = (ctime - timestamp) * 0.001;
|
||||
return time * 10 * (memoryUsageRatio * memoryUsageRatio)
|
||||
}
|
||||
//function calcRemoveImportance(timestamp, ctime, memoryUsageRatio) {
|
||||
// var time = (ctime - timestamp) * 0.001;
|
||||
// return time * 10 * (memoryUsageRatio * memoryUsageRatio)
|
||||
//}
|
||||
|
||||
function round(value, decimals) { //a basic rounding function
|
||||
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
|
||||
|
Loading…
x
Reference in New Issue
Block a user