Last active
August 29, 2015 14:03
-
-
Save rjrodger/c3717b2b21a8339a47de to your computer and use it in GitHub Desktop.
outline of loadbalance transport
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function( options ) { | |
var seneca = this | |
var plugin = 'loadbalance-transport' | |
var so = seneca.options() | |
options = seneca.util.deepextend( | |
{ | |
loadbalance: { | |
workers: [ ... ] | |
}, | |
}, | |
so.transport, | |
options) | |
var tu = seneca.export('transport/utils') | |
seneca.add({role:'transport',hook:'listen',type:'loadbalance'}, hook_listen_loadbalance) | |
seneca.add({role:'transport',hook:'client',type:'loadbalance'}, hook_client_loadbalance) | |
function hook_listen_loadbalance( args, done ) { | |
done( seneca.fail("cant-loadbalance-listen") ) | |
} | |
function hook_client_redis( args, clientdone ) { | |
var seneca = this | |
var type = args.type | |
var client_options = seneca.util.clean(_.extend({},options[type],args)) | |
tu.make_client( make_send, client_options, clientdone ) | |
// set up the workers - this is pseudo-ish code | |
var senecamap = {} | |
workers.forEach(function( spec ){ | |
// load the underlying transport | |
senecamap[workerid] = require('seneca')().use( spec.name?, spec.options?) | |
ping( senecamap[workerid] ) | |
}) | |
function ping( seneca ) { | |
seneca.act( 'role:transport,cmd:ping', function(err,out){ | |
if( timeout err ) { | |
// KILL the worker and try to reload | |
} | |
setTimeout(ping) | |
} ) | |
} | |
function make_send( spec, topic, send_done ) { | |
send_done( null, function( args, done ) { | |
var seneca = next round robin entry from senecamap | |
seneca.act( args, done ) | |
}) | |
} | |
// in this case, close gobally, not per topic, as there is no pre topic connection here | |
seneca.add('role:seneca,cmd:close',function( close_args, done ) { | |
var closer = this | |
forEach senecamap: seneca.close() | |
closer.prior(close_args,done) | |
}) | |
} | |
function handle_events( redisclient ) { | |
// Die if you can't connect initially | |
redisclient.on('ready', function() { | |
redisclient.on('error', function(err){ | |
seneca.log.error('transport','redis',err) | |
}) | |
}) | |
} | |
return { | |
name: plugin, | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment