evict.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. ;(function(){
  2. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  3. var ev = {}, empty = {}, u;
  4. Gun.on('opt', function(root){
  5. this.to.next(root);
  6. if(root.once){ return }
  7. if(typeof process == 'undefined'){ return }
  8. var util = process.memoryUsage;
  9. if(!util){ return }
  10. ev.max = parseFloat(root.opt.memory || process.env.WEB_MEMORY || 1399) * 0.8; // max_old_space_size defaults to 1400 MB. Note: old space !== memory space though.
  11. setInterval(check, 1000);
  12. function check(){
  13. var used = ev.used = util().rss / 1024 / 1024;
  14. if(used < ev.max){ return }
  15. setTimeout(GC, 1);
  16. }
  17. function GC(){
  18. var souls = Object.keys(root.graph||empty);
  19. var toss = Math.ceil(souls.length * 0.01);
  20. //var S = +new Date;
  21. Gun.list.map(souls, function(soul){
  22. if(--toss < 0){ return }
  23. root.gun.get(soul).off();
  24. });
  25. //console.log(+new Date - S, 'gc');
  26. }
  27. /*
  28. root.on('in', function(msg){
  29. this.to.next(msg);
  30. if(msg.get){
  31. return;
  32. }
  33. Gun.graph.is(msg, function(node, soul){
  34. var meta = (root.next||empty)[soul];
  35. if(!meta){ return }
  36. Gun.node.is(node, function(data, key){
  37. });
  38. });
  39. });
  40. */
  41. });
  42. }());