How to use the backoff.fibonacci function in backoff

To help you get started, we’ve selected a few backoff examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mtth / avsc / packages / services / lib / channel.js View on Github external
constructor(provider, opts) {
    super();
    opts = opts || {};
    const refreshBackoff = opts.refreshBackoff || backoff.fibonacci();

    this._channelProvider = provider;
    this._activeChannel = null; // Activated below.
    this._refreshAttempts = 0;
    this._refreshBackoff = refreshBackoff
      .on('backoff', (num, delay) => {
        d('Scheduling refresh in %sms.', delay);
        this._refreshAttempts++;
      })
      .on('ready', () => {
        d('Starting refresh attempt #%s...', this._refreshAttempts);
        this._refreshChannel();
      })
      .on('fail', () => {
        d('Exhausted refresh attempts, giving up.');
        this.emit('error', new Error('exhausted refresh attempts'));
github rtsao / unitest / lib / run-electron.js View on Github external
function startServer(startport, cb) {
  let port = startport;
  const server = http.createServer((req, res) => {
    res.setHeader('content-type', 'text/html');
    res.setHeader('Access-Control-Allow-Origin', '*');
    fs.createReadStream(INDEX_PATH).pipe(res);
    try {
      server.close();
    } catch (e) {}
  });
  const fibonacci = backoff.fibonacci({
    randomisationFactor: 0.1,
    initialDelay: 1,
    factor: 1.1,
    maxDelay: 3000,
  });
  server.on('error', err =>
    findPort((portErr, newport) => {
      if (portErr) {
        return cb('could not find new port');
      }
      port = newport;
      fibonacci.backoff();
    })
  );
  fibonacci.on('ready', (number, delay) => {
    server.listen(port, err => {
github rtsao / unitest / lib / run-electron.js View on Github external
function findPort(cb) {
  const fibonacci = backoff.fibonacci({
    randomisationFactor: 0.1,
    initialDelay: 1,
    factor: 1.1,
    maxDelay: 3000,
  });
  fibonacci.on('ready', (number, delay) => {
    getport(getRandomInt(3000, 10000), (err, port) => {
      if (err) {
        return fibonacci.backoff();
      }
      return cb(null, port);
    });
  });
  fibonacci.on('fail', () => cb('could not find port'));
  fibonacci.backoff();
}
github sastraxi / pgsh / src / util / wait-for.js View on Github external
const backoff = require('backoff');
const connectionCountTask = require('../task/connection-count');

const fibonacciBackoff = backoff.fibonacci({
  randomisationFactor: 0,
  initialDelay: 300,
  maxDelay: 12000,
});

/**
 * Waits until no other sessions are accessing the given database.
 *
 * @param {*} db a Database connection
 * @param {*} target the name of the database to monitor
 * @param {*} interruptHandler if we need to abandon waiting for any reason, call this
 * @param {*} failFast give up immediately if the database is not available
 */
const waitFor = (db, target, interruptHandler, failFast = false) =>
  async (resolve) => {
    const connectionCount = connectionCountTask(db);
github iarna / abraxas / client-reconnect.js View on Github external
var ClientReconnect = module.exports = function (options,newConnection) {
    this.socket = null;
    this.connect = backoff.fibonacci({
        initialDelay: 10,
        randomisationFactor: 0.15,
        maxDelay: 10000,
    });
    this.newConnection = newConnection;
    this.options = options;
    var self = this;
    this.connect.on('ready', function () { self._readyToConnect() });
    this.backoff = function () { self.connect.backoff() }
    this._readyToConnect();
    events.EventEmitter.call(this);
}
util.inherits(ClientReconnect, events.EventEmitter);
github hughsk / s3-write-stream / index.js View on Github external
function flushChunk(next) {
      var lastErr = null
      var chunk = part++
      var uploading = buffer.slice()
      var bo = backoff.fibonacci(boSettings)

      buffer._bufs.length = 0
      buffer.length = 0
      pending += 1

      if (!uploadId) return stream.once('upload started', uploadPart)

      uploadPart()
      function uploadPart() {
        bo.failAfter(5)
        bo.on('backoff', function() {
          s3.uploadPart({
              Body: uploading
            , Bucket: dest.Bucket
            , Key: dest.Key
            , UploadId: uploadId
github pgte / node-patterns-code / 04-work-queues / memory_queue / domotic_queue.js View on Github external
function ensureConnected(cb) {
  if (connected) {
    return cb();
  } else {
    var backoff = Backoff.fibonacci();
    backoff.on('backoff', connect);
    backoff.backoff();
  }

  function connect() {
    domotic.connect(connected);
  }

  function connect(err) {
    if (err) {
      backoff.backoff();
    } else {
      connected = true;
      cb();
    }
  }
github spacekit / spacekit / lib / index.js View on Github external
constructor (config) {
    super();

    this.config = config;

    this.proxyUrl = `wss://${this.config.service}.${this.config.host}/`;
    this.hostname = `${this.config.relay}.${this.config.username}.${this.config.host}`;
    this.outgoingSockets = new Map();

    this.backoff = Backoff.fibonacci({
      randomisationFactor: 0.4,
      initialDelay: 1000,
      maxDelay: 5 * 60000
    });
    this.backoff.on('backoff', (number, delay) => {
      log.debug(`Reconnecting in ${delay}ms...`);
    });
    this.backoff.on('ready', this.connect.bind(this));

    this.connect();
  }
github hughsk / s3-sync / index.js View on Github external
function uploadFile(details, next) {
    var absolute = details.fullPath
      , relative = prefix + details.path
      , lasterr
      , off = backoff.fibonacci({
        initialDelay: 1000
      })

    relative = relative.replace(/\\/g, '/')
    details.fresh = true

    off.failAfter(options.retries)
    off.on('fail', function() {
      next(lasterr || new Error('unknown error'))
    }).on('ready', function() {
      var headers = xtend({
          'x-amz-acl': options.acl
        , 'x-amz-meta-syncfilehash': details.md5
        , 'Content-Type': mime.lookup(absolute)
      }, options.headers)

backoff

Fibonacci and exponential backoffs.

MIT
Latest version published 9 years ago

Package Health Score

65 / 100
Full package analysis