lib/druid.js
function initZookeeper(client, connectionString) {
  var zk = zookeeper.createClient(connectionString, client.options.zookeeper)
  var error = false
  var connected = false

  client.ready = false
  client.zk = zk

  zk.connect()
  zk.on('connected', onconnected)
  zk.on('expired', setError)
  zk.on('authenticationFailed', setError)
  zk.on('disconnected', ondisconnected)
  zk.on('error', onerror)

  function onconnected() {
    if (!connected) {
      connected = true;

      client.updateBrokerList()
    }

    error = false;
  }

  function setError() {
    error = true;
  }

  function ondisconnected() {
    if (client.closed) {
      return
    }

    debug('Lost connection with ZooKeeper. Reconnecting...');

    if (error) {
      zk.removeListener('error', onerror)
      zk.removeListener('disconnected', ondisconnected)
      zk.removeListener('expired', setError)
      zk.removeListener('authenticationFailed', setError)
      zk.close()

      initZookeeper(client, connectionString);
    }
  }

  function onerror(err) {
    client.emit('error', err)
  }
}
function initZookeeper(client, connectionString) {
  var zk = zookeeper.createClient(connectionString, client.options.zookeeper)
  var error = false
  var connected = false

  client.ready = false
  client.zk = zk

  zk.connect()
  zk.on('connected', onconnected)
  zk.on('expired', setError)
  zk.on('authenticationFailed', setError)
  zk.on('disconnected', ondisconnected)
  zk.on('error', onerror)

  function onconnected() {
    if (!connected) {
      connected = true;

      client.updateBrokerList()
    }

    error = false;
  }

  function setError() {
    error = true;
  }

  function ondisconnected() {
    if (client.closed) {
      return
    }

    debug('Lost connection with ZooKeeper. Reconnecting...');

    if (error) {
      zk.removeListener('error', onerror)
      zk.removeListener('disconnected', ondisconnected)
      zk.removeListener('expired', setError)
      zk.removeListener('authenticationFailed', setError)
      zk.close()

      initZookeeper(client, connectionString);
    }
  }

  function onerror(err) {
    client.emit('error', err)
  }
}
function initZookeeper ( client , connectionString ) { var zk = zookeeper . createClient ( connectionString , client . options . zookeeper ) var error = false var connected = false client . ready = false client . zk = zk zk . connect ( ) zk . on ( 'connected' , onconnected ) zk . on ( 'expired' , setError ) zk . on ( 'authenticationFailed' , setError ) zk . on ( 'disconnected' , ondisconnected ) zk . on ( 'error' , onerror ) function onconnected ( ) { if ( ! connected ) { connected = true ; client . updateBrokerList ( ) } error = false ; } function setError ( ) { error = true ; } function ondisconnected ( ) { if ( client . closed ) { return } debug ( 'Lost connection with ZooKeeper. Reconnecting...' ) ; if ( error ) { zk . removeListener ( 'error' , onerror ) zk . removeListener ( 'disconnected' , ondisconnected ) zk . removeListener ( 'expired' , setError ) zk . removeListener ( 'authenticationFailed' , setError ) zk . close ( ) initZookeeper ( client , connectionString ) ; } } function onerror ( err ) { client . emit ( 'error' , err ) } }
Initiate ZooKeeper client

@param {Druid} client
@param {string} connectionString ZooKeeper connection string
Initiate ZooKeeper client