gun/lib/evict.js
2020-02-21 16:38:26 -08:00

52 lines
1.7 KiB
JavaScript

;(function(){
var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
var ev = {}, empty = {}, u;
Gun.on('opt', function(root){
this.to.next(root);
if(root.once){ return }
if(typeof process == 'undefined'){ return }
var util = process.memoryUsage, heap;
if(!util){ return }
try{ heap = require('v8').getHeapStatistics }catch(e){}
if(!heap){ return }
ev.max = parseFloat(root.opt.memory || (heap().heap_size_limit / 1024 / 1024) || process.env.WEB_MEMORY || 1399) * 0.8; // max_old_space_size defaults to 1400 MB. Note: old space !== memory space though. // KEEPING USED_HEA_SIZE < HEAP_SIZE_LIMIT ONLY THING TO BE BELOW TO PREVENT CRASH!
setInterval(check, 1000);
function check(){
var used = util().rss / 1024 / 1024;
var hused = heap().used_heap_size / 1024 / 1024;
if(hused < ev.max && used < ev.max){ return }
//if(used < ev.max){ return }
console.STAT && console.STAT('evict memory:', hused.toFixed(), used.toFixed(), ev.max.toFixed());
GC();//setTimeout(GC, 1);
}
function GC(){
var S = +new Date;
var souls = Object.keys(root.graph||empty);
var toss = Math.ceil(souls.length * 0.01);
//var S = +new Date;
Gun.list.map(souls, function(soul){
if(--toss < 0){ return }
root.$.get(soul).off();
});
root.dup.drop(1000 * 9); // clean up message tracker
console.STAT && console.STAT(S, +new Date - S, 'evict');
}
/*
root.on('in', function(msg){
this.to.next(msg);
if(msg.get){
return;
}
Gun.graph.is(msg, function(node, soul){
var meta = (root.next||empty)[soul];
if(!meta){ return }
Gun.node.is(node, function(data, key){
});
});
});
*/
});
}());