Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
outline of loadbalance transport
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
You can’t perform that action at this time.