Skip to content

Instantly share code, notes, and snippets.

@laser
Last active December 30, 2015 12:29
Show Gist options
  • Save laser/7829321 to your computer and use it in GitHub Desktop.
Save laser/7829321 to your computer and use it in GitHub Desktop.
scheduler
app.get('/home', prioritize(prioritizer, responder));
var queues = [];
function flush() {
if (!flush.polling) {
flush.polling = setInterval(function() {
var x;
for (var i = 0; i < queues.length; i++) {
for (var j = 0; j < queues[i].length; j++) {
if (x = queues[i].shift()) {
x();
break;
}
}
if (x) {
break;
}
}
}, 0);
}
}
function done(req, resp, priority, action) {
queues[priority].queue.push(function() {
action(req, resp);
});
flush();
}
function prioritizer(req) {
for (var i = 0; i < queues.length; i++) {
if (queues[i].heuristic()) {
return i;
}
}
return priorities.length;
}
function prioritize(prioritizer, action) {
queues.push({
"heuristic": function() {
return req.xhr
},
"queue": []
});
queues.push({
"heuristic": function() {
return true;
},
"queue": []
});
return function(req, res) {
done(req, resp, prioritizer(req), action);
};
}
function responder(req, res){
var body = 'Homepage';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', body.length);
res.end(body);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment