fix peer leak + more stats

This commit is contained in:
Mark Nadal 2019-05-15 10:27:57 -07:00
parent 9b820287d6
commit ed30911ba7
2 changed files with 44 additions and 19 deletions

View File

@ -10,15 +10,31 @@
html, body { html, body {
font-family: "Oxygen", sans-serif; font-family: "Oxygen", sans-serif;
} }
.ct-chart * { svg, .ct-chart * {
overflow: visible; overflow: visible;
} }
.ct-series-a .ct-line,
.ct-series-a .ct-point {
stroke: blue;
}
.ct-series-b .ct-line,
.ct-series-b .ct-point {
stroke: green;
}
</style> </style>
<input id="url" class="center" placeholder="enter peer stats source url"> <input id="url" class="center" placeholder="enter peer stats source url">
<div class="leak" style="padding: 0 2em;"> <div class="leak" style="padding: 0 2em;">
<div class="leak ct-chart ct-perfect-fourth" style="height: 20em;"></div> <div class="leak ct-mem ct-perfect-fourth" style="height: 20em;"></div>
</div> </div>
<p id="peers" class="shout center">0</p>
<div class="center"><span class="shout" id="peers">0</span> peers <span class="shout" id="nodes">0</span> nodes</div>
<div class="center leak" style="padding: 0 2em;">
<div class="unit col leak ct-cpu ct-perfect-fourth" style="height: 20em;"></div>
</div>
<script src="./jquery.js"></script> <script src="./jquery.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.css">
<script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script> <script src="https://cdn.jsdelivr.net/chartist.js/latest/chartist.min.js"></script>
@ -27,21 +43,29 @@ var stats = {slide: [0,0,0,0,0], peers: [0,0,0,0,0]};
setInterval(function(){ setInterval(function(){
stats.show(); stats.show();
}, 1000 * 15); }, 1000 * 15);
stats.show = function(){ $.getJSON(url.value||(location.origin+'/gun/stats.radata'), function(data){ console.log(data) stats.show = function(){ $.getJSON(url.value||(location.origin+'/gun/stats.radata'), function(data){ console.log(data);
$('#peers').text(data.peers.count);
$('#nodes').text(data.node.count);
var mb = data.memory.heapTotal / 1024 / 1024; var mb = data.memory.heapTotal / 1024 / 1024;
stats.slide.push(mb); stats.slide = stats.slide.slice(1); stats.slide.push(mb); stats.slide = stats.slide.slice(1);
$(peers).text(data.peers.count); new Chartist.Line('.ct-mem', {
var data = {
// A labels array that can contain any sort of values // A labels array that can contain any sort of values
labels: ['-1min', '-45s', '-30s', '-15s', '0'], labels: ['-1min', '-45s', '-30s', '-15s', '0'],
// Our series array that contains series objects or in this case series data arrays // Our series array that contains series objects or in this case series data arrays
series: [stats.slide] series: [stats.slide]
}; }, {fullWidth: true, low: 0, axisY: {
labelInterpolationFnc: function(v) { return v+'MB' }
}});
// Create a new line chart object where as first parameter we pass in a selector new Chartist.Line('.ct-cpu', {
// that is resolving to our chart container element. The Second parameter // A labels array that can contain any sort of values
// is the actual data object. labels: ['-15min', '-5min', '1min'],
new Chartist.Line('.ct-chart', data); // Our series array that contains series objects or in this case series data arrays
series: [data.cpu.loadavg.reverse()]
}, {fullWidth: true, low: 0, axisY: {
labelInterpolationFnc: function(v) { return v+'cpu' }
}});
}) } }) }
stats.show(); stats.show();
</script> </script>

17
gun.js
View File

@ -2013,16 +2013,16 @@
var tomap = function(k,i,m){m(k,true)}; var tomap = function(k,i,m){m(k,true)};
;(function(){ ;(function(){
var message;
function each(peer){ mesh.say(message, peer) }
mesh.say = function(msg, peer, o){ mesh.say = function(msg, peer, o){
/* /*
TODO: Plenty of performance optimizations TODO: Plenty of performance optimizations
that can be made just based off of ordering, that can be made just based off of ordering,
and reducing function calls for cached writes. and reducing function calls for cached writes.
*/ */
if(!peer){ if(!peer){ message = msg;
Type.obj.map(opt.peers, function(peer){ Type.obj.map(opt.peers, each);
mesh.say(msg, peer);
});
return; return;
} }
var tmp, wire = peer.wire || ((opt.wire) && opt.wire(peer)), msh, raw;// || open(peer, ctx); // TODO: Reopen! var tmp, wire = peer.wire || ((opt.wire) && opt.wire(peer)), msh, raw;// || open(peer, ctx); // TODO: Reopen!
@ -2126,10 +2126,9 @@
mesh.hi = function(peer){ mesh.hi = function(peer){
var tmp = peer.wire || {}; var tmp = peer.wire || {};
if(peer.id || peer.url){ if(peer.id || peer.url){
Type.obj.del(opt.peers, tmp.pid);
opt.peers[peer.url || peer.id] = peer; opt.peers[peer.url || peer.id] = peer;
} else { } else {
tmp = peer.id = peer.id || Type.text.random(9); tmp = peer.id = tmp.pid = peer.id || Type.text.random(9);
mesh.say({dam: '?'}, opt.peers[tmp] = peer); mesh.say({dam: '?'}, opt.peers[tmp] = peer);
} }
if(!tmp.hied){ ctx.on(tmp.hied = 'hi', peer) } if(!tmp.hied){ ctx.on(tmp.hied = 'hi', peer) }
@ -2155,8 +2154,10 @@
return; return;
} }
if(!peer.wire){ return } if(!peer.wire){ return }
if(peer.wire.pid){ return } // they already set their ID! if(!peer.wire.pid){ return } // only run code below if wire.pid exists
peer.id = peer.wire.pid = msg.pid; Type.obj.del(opt.peers, peer.wire.pid || peer.id);
delete peer.wire.pid;
peer.id = msg.pid;
mesh.hi(peer); mesh.hi(peer);
} }
return mesh; return mesh;