From 2015a01b6532ddbb534d1a6268c001beb4fe3a88 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Tue, 15 May 2018 22:31:42 -0700 Subject: [PATCH] experimental GC --- lib/debug.js | 12 +++++++++++- lib/evict.js | 27 +++++++++++++++++++++++---- lib/server.js | 1 + lib/store.js | 2 +- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/debug.js b/lib/debug.js index 336b80b9..bd4f657d 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -7,14 +7,17 @@ global.DEBUG = 1; setInterval(function(){ var mem = process.memoryUsage(); - var used = mem.heapUsed / 1024 / 1024; + var used = mem.heapTotal / 1024 / 1024; var print = ''; used = used.toFixed(1); print += used +' MB. ' if(db.root){ db.concurrency = Object.keys(db.peers||{}).length; print += db.concurrency +' peers. '; + db.nodes = Object.keys(db.root.graph||{}).length; + print += db.nodes + ' nodes. '; } + if(db.count){ print += db.count + ' msgs. '} console.log(print); }, 2500); @@ -24,6 +27,13 @@ if(root.once){ return } db.root = root; db.peers = root.opt.peers; + + db.count = 0; + root.on('in', function(msg){ + this.to.next(msg); + db.last = msg; + db.count++; + }) }) }()); \ No newline at end of file diff --git a/lib/evict.js b/lib/evict.js index 048a575a..4f2b5817 100644 --- a/lib/evict.js +++ b/lib/evict.js @@ -1,13 +1,32 @@ ;(function(){ var Gun = (typeof window !== 'undefined')? window.Gun : require('../gun'); - var LRU = 1, empty = {}, u; + var ev = {}, empty = {}, u; Gun.on('opt', function(root){ this.to.next(root); if(root.once){ return } - root.on('get', function(msg){ - - }) + if(typeof process == 'undefined'){ return } + var util = process.memoryUsage; + if(!util){ return } + + ev.max = parseFloat(root.opt.memory || process.env.WEB_MEMORY || 512) * 0.8; + + setInterval(check, 250); + function check(){ + var used = ev.used = util().heapTotal / 1024 / 1024; + if(used < ev.max){ return } + setTimeout(GC, 1); + } + function GC(){ + var souls = Object.keys(root.graph||empty); + var toss = Math.ceil(souls.length * 0.1); + //var start = Gun.state(), i = toss; + Gun.list.map(souls, function(soul){ + if(--toss < 0){ return } + root.gun(soul).off(); + }); + //console.log("evicted", i, 'nodes in', ((Gun.state() - start)/1000).toFixed(2), 'sec.'); + } /* root.on('in', function(msg){ this.to.next(msg); diff --git a/lib/server.js b/lib/server.js index 5f6d6c68..f46972e5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -9,6 +9,7 @@ require('./verify'); require('./file'); require('./bye'); + require('./evict'); if('debug' === process.env.GUN_ENV){ require('./debug') } module.exports = Gun; }()); \ No newline at end of file diff --git a/lib/store.js b/lib/store.js index 943e0ac0..359c8607 100644 --- a/lib/store.js +++ b/lib/store.js @@ -10,7 +10,7 @@ Gun.on('opt', function(ctx){ if(ctx.once){ return } if(false !== opt.localStorage && !process.env.AWS_S3_BUCKET){ return } // TODO: Remove this after migration. if(false === opt.radisk){ return } - console.log("BUG WARNING: Radix Storage Engine (RSE) has a known rare edge case, if data gets split between file chunks, a GET may only return the first chunk!!!"); + console.log("BUG WARNING: Radix Storage Engine (RAD) has a known rare edge case, if data gets split between file chunks, a GET may only return the first chunk!!!"); opt.store = opt.store || Store(opt); var rad = Radisk(opt);