Skip to content

Instantly share code, notes, and snippets.

@myhrvold
Created February 16, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myhrvold/0a88fa0331cc27b88c05 to your computer and use it in GitHub Desktop.
Save myhrvold/0a88fa0331cc27b88c05 to your computer and use it in GitHub Desktop.
Handle-or-Forward Pattern Example for Ringpop
module.exports = function handleOrForward(ringpop, key, req, res, arg2, arg3, handle) {
var dest = ringpop.lookup(key);
if (dest === ringpop.whoami()) {
// Handle request on local node
handle(ringpop, key, res, arg2, arg3);
return;
}
// Forward request
var channel = ringpop.channel;
var peer = channel.peers.add(dest);
var outreq = new RelayRequest(channel, peer, req, function buildRes(options) {
res.headers = options.headers;
res.code = options.code;
res.ok = options.ok;
return res;
});
outreq.createOutRequest();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment