lib/synchronization/slave.js
function sync() {
    // retrieve latency, then wait 1 sec
    function getLatencyAndWait() {
      var result = null;

      if (isDestroyed) {
        return Promise.resolve(result);
      }

      return getLatency(slave)
          .then(function (latency) { result = latency })  // store the retrieved latency
          .catch(function (err)    { console.log(err) })  // just log failed requests
          .then(function () { return util.wait(DELAY) })  // wait 1 sec
          .then(function () { return result});            // return the retrieved latency
    }

    return util
        .repeat(getLatencyAndWait, REPEAT)
        .then(function (all) {
          debug('latencies', all);

          // filter away failed requests
          var latencies = all.filter(function (latency) {
            return latency !== null;
          });

          // calculate the limit for outliers
          var limit = stat.median(latencies) + stat.std(latencies);

          // filter away outliers: all latencies largereq than the mean+std
          var filtered = latencies.filter(function (latency) {
            return latency < limit;
          });

          // return the mean latency
          return (filtered.length > 0) ? stat.mean(filtered) : null;
        })
        .then(function (latency) {
          if (isDestroyed) {
            return Promise.resolve(null);
          }
          else {
            return slave.request('time').then(function (timestamp) {
              var time = timestamp + latency;
              slave.emit('change', time);
              return time;
            });
          }
        })
        .catch(function (err) {
          slave.emit('error', err)
        });
  }
function sync() {
    // retrieve latency, then wait 1 sec
    function getLatencyAndWait() {
      var result = null;

      if (isDestroyed) {
        return Promise.resolve(result);
      }

      return getLatency(slave)
          .then(function (latency) { result = latency })  // store the retrieved latency
          .catch(function (err)    { console.log(err) })  // just log failed requests
          .then(function () { return util.wait(DELAY) })  // wait 1 sec
          .then(function () { return result});            // return the retrieved latency
    }

    return util
        .repeat(getLatencyAndWait, REPEAT)
        .then(function (all) {
          debug('latencies', all);

          // filter away failed requests
          var latencies = all.filter(function (latency) {
            return latency !== null;
          });

          // calculate the limit for outliers
          var limit = stat.median(latencies) + stat.std(latencies);

          // filter away outliers: all latencies largereq than the mean+std
          var filtered = latencies.filter(function (latency) {
            return latency < limit;
          });

          // return the mean latency
          return (filtered.length > 0) ? stat.mean(filtered) : null;
        })
        .then(function (latency) {
          if (isDestroyed) {
            return Promise.resolve(null);
          }
          else {
            return slave.request('time').then(function (timestamp) {
              var time = timestamp + latency;
              slave.emit('change', time);
              return time;
            });
          }
        })
        .catch(function (err) {
          slave.emit('error', err)
        });
  }
function sync ( ) { // retrieve latency, then wait 1 sec function getLatencyAndWait ( ) { var result = null ; if ( isDestroyed ) { return Promise . resolve ( result ) ; } return getLatency ( slave ) . then ( function ( latency ) { result = latency } ) // store the retrieved latency . catch ( function ( err ) { console . log ( err ) } ) // just log failed requests . then ( function ( ) { return util . wait ( DELAY ) } ) // wait 1 sec . then ( function ( ) { return result } ) ; // return the retrieved latency } return util . repeat ( getLatencyAndWait , REPEAT ) . then ( function ( all ) { debug ( 'latencies' , all ) ; // filter away failed requests var latencies = all . filter ( function ( latency ) { return latency !== null ; } ) ; // calculate the limit for outliers var limit = stat . median ( latencies ) + stat . std ( latencies ) ; // filter away outliers: all latencies largereq than the mean+std var filtered = latencies . filter ( function ( latency ) { return latency < limit ; } ) ; // return the mean latency return ( filtered . length > 0 ) ? stat . mean ( filtered ) : null ; } ) . then ( function ( latency ) { if ( isDestroyed ) { return Promise . resolve ( null ) ; } else { return slave . request ( 'time' ) . then ( function ( timestamp ) { var time = timestamp + latency ; slave . emit ( 'change' , time ) ; return time ; } ) ; } } ) . catch ( function ( err ) { slave . emit ( 'error' , err ) } ) ; }
Sync with the time of the master. Emits a 'change' message
@private
Sync with the time of the master . Emits a change message