Created
June 27, 2011 14:27
-
-
Save jayjanssen/1048955 to your computer and use it in GitHub Desktop.
node-zookeeper wrapper that handles the zookeeper server coming and going
This file contains 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
// connection manager object is responsible for maintaining the connection to zk | |
var connection_manager = (function() { | |
var cm = {}; | |
// Start connection immediately (global for all instances) | |
var zk_trying = false; | |
var zkclass; | |
var zk; | |
// tries to make a connection (if one isn't already being attempted), waits for timeout | |
// or connected event, whichever comes first and returns handle or undefined accordingly | |
var zk_connect = function( callback ) { | |
if( !zk_trying ) { | |
// console.log( "trying connecting" ); | |
zk_trying = true; | |
zkclass = new ZK(); | |
// connect to zk | |
zkclass.init( {connect:'localhost:2181', timeout: 500, | |
data_as_buffer: false, debug_level:ZK.ZOO_LOG_LEVEL_WARNING} ); | |
} else { | |
// console.log( "waiting on connection") | |
} | |
var timeoutId; | |
// what to do if we get a connection | |
var on_connected = function( zkk ) { | |
console.log( 'zk session established, id=%s', zkk.client_id ); | |
zk_trying = false; | |
zk = zkk; | |
// unregister the timeout event | |
clearTimeout( timeoutId ); | |
callback( zkk ); | |
}; | |
// What to do if we get a timeout | |
var on_timeout = function() { | |
if( zk === undefined ) { | |
console.log( "Couldn't get connection" ); | |
zk_trying = false; | |
// unregister the on_connected event | |
zkclass.removeListener( ZK.on_connected, on_connected ); | |
callback( undefined ); | |
} | |
}; | |
// Register the callbacks | |
zkclass.once( ZK.on_connected, on_connected ); | |
timeoutId = setTimeout( on_timeout, 1000); | |
}; | |
// Wrapper for all client-dependent functions, verifies connection is present | |
// before executing callback | |
cm.get_handle = function( callback ) { | |
if( zk === undefined ) { | |
// try to connect | |
// console.log( "no connection, trying"); | |
zk_connect( callback ); | |
} else { | |
// console.log( "Already got connection"); | |
callback( zk ); | |
} | |
}; | |
// We know the handle is bad, so invalidate it | |
cm.invalidate_handle = function() { | |
// console.log( "Invalidating handle" ); | |
if( zk !== undefined ) { | |
zk.close(); | |
zk = undefined; | |
} | |
}; | |
return cm; | |
}()); | |
// Handle misc ZK errors (esp ZK.ZCONNECTIONLOSS). This must be done | |
// when suspect zookeeper errors come back from API calls. The connection | |
// will not invalidate itself. | |
var handle_zk_error = function( rc ) { | |
connection_manager.invalidate_handle(); | |
}; | |
// Example function, returns undefined if the connection is missing and timed out, | |
// false if the node doesn't exist. Undefined on a misc zookeeper error | |
var get = function( key, callback ) { | |
connection_manager.get_handle( function( zk ) { | |
if( zk === undefined ) { | |
callback( undefined ); | |
return; | |
} | |
zk.a_get( key, undefined, function( rc, error, stat, data ) { | |
if( rc === ZK.ZOK ) { | |
callback( data ); | |
} else if( rc === ZK.ZNONODE ) { | |
callback( false ); | |
} else { | |
handle_zk_error( rc ); | |
callback( undefined ); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment