-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevict.js
52 lines (49 loc) · 1.81 KB
/
evict.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
;(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;
var tmp; if(tmp = console.STAT){ tmp.memax = parseFloat(ev.max.toFixed(1)); tmp.memused = parseFloat(used.toFixed(1)); tmp.memhused = parseFloat(hused.toFixed(1)); }
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);
setTimeout.each(souls, function(soul){
if(--toss < 0){ return 1 }
root.$.get(soul).off();
},0,99);
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){
});
});
});
*/
});
}());