Skip to content
This repository has been archived by the owner on Jun 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #92 from soichih/gh-pages
Browse files Browse the repository at this point in the history
updated broadcasting mechanism to make it more error resilient
  • Loading branch information
yarikoptic committed Jun 27, 2020
2 parents f5c0165 + 2e86ab5 commit d7bab93
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
69 changes: 46 additions & 23 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
#!/usr/bin/env node
const express = require('express');
const uuid = require('uuid');
const fs = require('fs');
const app = express();
const expressWs = require('express-ws')(app);

function log(){
let d = new Date();
console.log(d.toLocaleString("en-US"), ...arguments);
};

var aWss = expressWs.getWss('/');

/*
app.use(function (req, res, next) {
console.log('middleware');
req.testing = 'testing';
return next();
});
*/

app.get('/', function(req, res, next){
log('get route', req.testing);
res.end();
});
*/

let ips = {};
let counts = {};
let connections = {};
app.ws('/', function(ws, req) {
ws.id = uuid.v4();
connections[ws.id] = ws;
ws.on('close',function() {
delete connections[ws.id];
})
ws.on('message', function(data) {
//console.log("message received", msg);
try {
//console.debug(data);
let msg = JSON.parse(data);
Expand All @@ -51,19 +51,15 @@ app.ws('/', function(ws, req) {
}
console.dir(ips[msg.id]);
counts[msg.id] = ips[msg.id].length;
aWss.clients.forEach(function (client) {
client.send(JSON.stringify({update: {id: msg.id, count: counts[msg.id]}}));
});
broadcast({update: {id: msg.id, count: counts[msg.id]}});
break;
case "jitclose":
log("jitclose:", ip, msg.id);
if(!ips[msg.id]) ips[msg.id] = []; //could hapen
if(!ips[msg.id]) ips[msg.id] = []; //did happen..
entry = ips[msg.id].find(c=>ip == ip);
ips[msg.id].splice(ips[msg.id].indexOf(entry), 1);
counts[msg.id] = ips[msg.id].length;
aWss.clients.forEach(function (client) {
client.send(JSON.stringify({update: {id: msg.id, count: counts[msg.id]}}));
});
broadcast({update: {id: msg.id, count: counts[msg.id]}});
if(counts[msg.id] == 0) delete counts[msg.id];
break;
}
Expand All @@ -73,11 +69,20 @@ app.ws('/', function(ws, req) {
});
});

function broadcast(msg) {
for(let id in connections) {
if(connections[id].readyState == 1) connections[id].send(JSON.stringify(msg));
}
}

setInterval(()=>{
log("running periodic maintenance");

//clean up old connections (client disappeard without jitclose?)
for(let id in ips) {
let recents = [];
let recent = new Date();
recent.setTime(recent.getTime() - 1000*60*5); //we can shorten this now that clients are polling
recent.setTime(recent.getTime() - 1000*60*120);
ips[id].forEach(rec=>{
if(rec.date > recent) recents.push(rec);
});
Expand All @@ -86,12 +91,30 @@ setInterval(()=>{
log("expire:", id);
//need to update the counts
counts[id] = ips[id].length;
aWss.clients.forEach(function (client) {
client.send(JSON.stringify({update: {id, count: counts[id]}}));
});
broadcast({update: {id, count: counts[id]}});
if(counts[id] == 0) delete counts[id];
}
}
}, 1000*60);


log("writing out sensu metrics");

//output sensu metrics
let d = new Date();
const time = Math.round(d.getTime()/1000);
let metrics = "";
for(let id in counts) {
metrics += "prod.ohbm2020.poster.people."+id+" "+counts[id]+" "+time+"\n";
}
console.log(metrics);
fs.writeFileSync("/tmp/metrics.sensu", metrics);
log("done maintenance");

}, 1000*60*5);

log("--------------------------------------------------------------------------");
log("listening");
log("--------------------------------------------------------------------------");
app.listen(3000);



5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"axios": "^0.19.2",
"express": "^4.17.1",
"express-ws": "^4.0.0",
"reconnecting-websocket": "^4.4.0"
"reconnecting-websocket": "^4.4.0",
"uuid": "^8.2.0"
}
}

0 comments on commit d7bab93

Please sign in to comment.