src/MasterElector.js
function _requestVotes () {
    let { host, coordinationPort } = node.settings
    let message = {
      type: 'voteRequest',
      data: {}
    }
    return new Promise((resolve, reject) => {
      let resolveStart = Date.now()
      dns.resolve4(host, (err, addresses) => {
        if (err) {
          debug(`cannot resolve host '${host}'. Check DNS infrastructure.`)
          return reject(err)
        }
        debug(`resolved ${addresses.length} IP(s) for host '${host}' in ${Date.now() - resolveStart} ms`)
        debug(`requesting votes from ${addresses.length} nodes`)
        Promise.all(
          addresses.map(address => new Promise((resolve, reject) => {
            let voteRequestTime = Date.now()
            let messenger = zmq.socket('req')
            messenger.connect(`tcp://${address}:${coordinationPort}`)
            messenger.send(JSON.stringify({
              ...message,
              toAddress: address,
              from: node.name
            }))

            let _resolved = false
            function onEnd (candidateBuffer) {
              if (_resolved) return
              _resolved = true
              messenger.removeListener('message', onEnd)
              messenger.close()

              let candidate = candidateBuffer && JSON.parse(candidateBuffer)
              if (candidate) {
                let elapsed = Date.now() - voteRequestTime
                candidate.candidate && debug(`received vote by ${candidate.name === node.name ? 'myself' : candidate.name}${candidate.isMaster ? ' (master)' : ''} in ${elapsed} ms`)
              } else {
                debug(`missed vote by peer at ${address}`)
              }
              resolve(candidate)
            }

            messenger.on('message', onEnd)
            setTimeout(onEnd, node.settings.voteTimeout)
          }))
        )
        .then(nodes => compact(nodes))
        .then(nodes => resolve(nodes.filter(({candidate}) => candidate)))
      })
    })
  }
function _requestVotes () {
    let { host, coordinationPort } = node.settings
    let message = {
      type: 'voteRequest',
      data: {}
    }
    return new Promise((resolve, reject) => {
      let resolveStart = Date.now()
      dns.resolve4(host, (err, addresses) => {
        if (err) {
          debug(`cannot resolve host '${host}'. Check DNS infrastructure.`)
          return reject(err)
        }
        debug(`resolved ${addresses.length} IP(s) for host '${host}' in ${Date.now() - resolveStart} ms`)
        debug(`requesting votes from ${addresses.length} nodes`)
        Promise.all(
          addresses.map(address => new Promise((resolve, reject) => {
            let voteRequestTime = Date.now()
            let messenger = zmq.socket('req')
            messenger.connect(`tcp://${address}:${coordinationPort}`)
            messenger.send(JSON.stringify({
              ...message,
              toAddress: address,
              from: node.name
            }))

            let _resolved = false
            function onEnd (candidateBuffer) {
              if (_resolved) return
              _resolved = true
              messenger.removeListener('message', onEnd)
              messenger.close()

              let candidate = candidateBuffer && JSON.parse(candidateBuffer)
              if (candidate) {
                let elapsed = Date.now() - voteRequestTime
                candidate.candidate && debug(`received vote by ${candidate.name === node.name ? 'myself' : candidate.name}${candidate.isMaster ? ' (master)' : ''} in ${elapsed} ms`)
              } else {
                debug(`missed vote by peer at ${address}`)
              }
              resolve(candidate)
            }

            messenger.on('message', onEnd)
            setTimeout(onEnd, node.settings.voteTimeout)
          }))
        )
        .then(nodes => compact(nodes))
        .then(nodes => resolve(nodes.filter(({candidate}) => candidate)))
      })
    })
  }
function _requestVotes ( ) { let { host , coordinationPort } = node . settings let message = { type : 'voteRequest' , data : { } } return new Promise ( ( resolve , reject ) => { let resolveStart = Date . now ( ) dns . resolve4 ( host , ( err , addresses ) => { if ( err ) { debug ( ` ${ host } ` ) return reject ( err ) } debug ( ` ${ addresses . length } ${ host } ${ Date . now ( ) - resolveStart } ` ) debug ( ` ${ addresses . length } ` ) Promise . all ( addresses . map ( address => new Promise ( ( resolve , reject ) => { let voteRequestTime = Date . now ( ) let messenger = zmq . socket ( 'req' ) messenger . connect ( ` ${ address } ${ coordinationPort } ` ) messenger . send ( JSON . stringify ( { ... message , toAddress : address , from : node . name } ) ) let _resolved = false function onEnd ( candidateBuffer ) { if ( _resolved ) return _resolved = true messenger . removeListener ( 'message' , onEnd ) messenger . close ( ) let candidate = candidateBuffer && JSON . parse ( candidateBuffer ) if ( candidate ) { let elapsed = Date . now ( ) - voteRequestTime candidate . candidate && debug ( ` ${ candidate . name === node . name ? 'myself' : candidate . name } ${ candidate . isMaster ? ' (master)' : '' } ${ elapsed } ` ) } else { debug ( ` ${ address } ` ) } resolve ( candidate ) } messenger . on ( 'message' , onEnd ) setTimeout ( onEnd , node . settings . voteTimeout ) } ) ) ) . then ( nodes => compact ( nodes ) ) . then ( nodes => resolve ( nodes . filter ( ( { candidate } ) => candidate ) ) ) } ) } ) }
requests the voting identity of all the eligible dnsNodes
@return {promise} An list of objects representing nodes eligible as master
requests the voting identity of all the eligible dnsNodes