Example #1
0
function HyperEmitter (db, codecs, opts) {
  if (!(this instanceof HyperEmitter)) {
    return new HyperEmitter(db, codecs, opts)
  }

  this._opts = xtend(defaults, opts)
  this._parallel = fastparallel({ results: false })
  this._db = db
  this._hyperlog = hyperlog(db)

  this._closed = false
  this._listening = false

  this._peers = {}
  this._lastPeerId = 0
  this._listeners = {}

  this.status = new EventEmitter()
  this.codecs = initializeCodecs(codecs)
  this.messages = this.codecs

  createServer.call(this)
  connectToKnownPeers.call(this)
  handleNewPeers.call(this)

  var that = this
  this._hyperlog.ready(function () {
    if (that._closed) return

    that.changeStream = createChangeStream.call(that)
    that.changes = that.changeStream
    that.status.emit('ready')
  })
}
Example #2
0
function Aedes (opts) {
  var that = this

  if (!(this instanceof Aedes)) {
    return new Aedes(opts)
  }

  opts = xtend(defaultOptions, opts)

  this.id = shortid()
  this.counter = 0
  this.connectTimeout = opts.connectTimeout
  this.mq = opts.mq || mqemitter(opts)
  this.handle = function handle (conn) {
    conn.setMaxListeners(opts.concurrency * 2)
    // return, just to please standard
    return new Client(that, conn)
  }
  this.persistence = opts.persistence || memory()
  this.persistence.broker = this
  this._parallel = parallel()
  this._series = series()
  this._enqueuers = reusify(DoEnqueues)

  this.authenticate = opts.authenticate
  this.authorizePublish = opts.authorizePublish
  this.authorizeSubscribe = opts.authorizeSubscribe
  this.published = opts.published

  this.clients = {}
  this.brokers = {}

  var heartbeatTopic = '$SYS/' + that.id + '/heartbeat'
  this._heartbeatInterval = setInterval(heartbeat, opts.heartbeatInterval)

  var bufId = new Buffer(that.id, 'utf8')

  function heartbeat () {
    that.publish({
      topic: heartbeatTopic,
      payload: bufId
    }, noop)
  }

  function deleteOldBrokers (broker) {
    if (that.brokers[broker] + 3 * opts.heartbeatInterval < Date.now()) {
      delete that.brokers[broker]
    }
  }

  this._clearWillInterval = setInterval(function () {
    Object.keys(that.brokers).forEach(deleteOldBrokers)

    that.persistence
      .streamWill(that.brokers)
      .pipe(bulk.obj(receiveWills))
  }, opts.heartbeatInterval * 4)

  function receiveWills (chunks, done) {
    that._parallel(that, checkAndPublish, chunks, done)
  }

  function checkAndPublish (will, done) {
    var needsPublishing =
      !that.brokers[will.brokerId] ||
      that.brokers[will.brokerId] + 3 * opts.heartbeatInterval <
      Date.now()

    if (needsPublishing) {
      // randomize this, so that multiple brokers
      // do not publish the same wills at the same time
      that.publish(will, function publishWill (err) {
        if (err) {
          return done(err)
        }

        that.persistence.delWill({
          id: will.clientId
        }, done)
      })
    } else {
      done()
    }
  }

  this.mq.on('$SYS/+/heartbeat', function storeBroker (packet, done) {
    that.brokers[packet.payload.toString()] = Date.now()
    done()
  })

  this.mq.on('$SYS/+/new/clients', function closeSameClients (packet, done) {
    var serverId = packet.topic.split('/')[1]
    var clientId = packet.payload.toString()

    if (that.clients[clientId] && serverId !== that.id) {
      that.clients[clientId].close(done)
    } else {
      done()
    }
  })

  // metadata
  this.connectedClients = 0
}
'use strict';

var setImmediate = require('timers').setImmediate;

var async = require('async');
var runParallel = require('run-parallel');
var collectParallel = require('../array.js');
var fastparallel = require('fastparallel');

var asyncBenchRunner = require('./async-bench.js');

var fastParallelInstance = fastparallel({
    results: true
});

asyncBenchRunner({
    'runParallel': function testAsync(cb) {
        var tasks = [1, 2, 3, 4, 5];
        var funcs = [];
        for (var i = 0; i < tasks.length; i++) {
            funcs.push(doubleAsyncThunk(tasks[i]));
        }

        runParallel(funcs, cb);
    },
    'async': function testAsync(cb) {
        var tasks = [1, 2, 3, 4, 5];
        async.map(tasks, doubleAsync, cb);
    },
    'collectParallel': function testAsync(cb) {
        var tasks = [1, 2, 3, 4, 5];