Skip to content

Instantly share code, notes, and snippets.

@jayjanssen
Created July 11, 2011 17:14
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 jayjanssen/1076304 to your computer and use it in GitHub Desktop.
Save jayjanssen/1076304 to your computer and use it in GitHub Desktop.
node-zookeeper 100% cpu (curl against it 3 times)
require.paths.unshift('../');
var ZK = require ('zookeeper').ZooKeeper;
var connect = require('connect');
var Storage = (function() {
var that = {};
// 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 ) {
var timeoutId;
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} );
}
// what to do if we get a connection
var on_connected = function( zkk ) {
console.log( 'zk session established, id=' + 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)
var handle_zk_error = function( rc, error ) {
if( rc !== ZK.ZCLOSING ) { // ZK is not already closing
console.log( "Zookeeper error: " + error + " (" + rc + "), disconnecting..." );
connection_manager.invalidate_handle();
}
};
// Get the requested key. Callback called with results
// callback( data, version ). Possible values for data:
// - undefined: if there was a Zkpr error
// - false: if the node simply doesn't exist
// - value: contents of the node
// - version: version of the node (only if data is not falsey)
that.get = function( key, callback ) {
connection_manager.get_handle( function( zk ) {
if( zk === undefined ) {
callback( undefined );
return;
}
console.log( "Doing a_get on " + key );
zk.a_get( key, undefined, function( rc, error, stat, data ) {
if( rc === ZK.ZOK ) {
callback( data, stat.version );
} else if( rc === ZK.ZNONODE ) {
callback( false );
} else {
handle_zk_error( rc, error );
callback( undefined );
}
});
});
};
return that;
}());
module.exports = connect.createServer(function (req, resp) {
// Creating the QuorumRequest will generate an exception on bad input
Storage.get( req.url, function( result ) {
resp.statusCode = 200;
resp.end( "Result: " + result + "\n" );
});
}).listen( 4080 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment