How to use the backoff.call 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 frandallfarmer / neohabitat / db / populateModels.js View on Github external
fs.readFile(objectPath, 'utf8', function (err, data) {
          if (err) {
            console.error('Could not read file:', objectPath);
            return console.error(err);
          }
          var templatedJSON = '';
          try {
            // Templates and attempts to parse the object's JSON.
            templatedJSON = templateHabitatObject(data);
            var habitatObject = JSON.parse(templatedJSON);

            // Figures out how we're going to write the object to Mongo.
            var updateWithRetries = null;
            if (habitatObject instanceof Array) {
              updateWithRetries = backoff.call(
                eupdateArray, db, habitatObject, habitatObjectUpdateFinished);
            } else {
              updateWithRetries = backoff.call(
                eupdateOne, db, habitatObject, habitatObjectUpdateFinished);
            }

            // Sets retry parameters.
            updateWithRetries.retryIf(function(err) { return err != null; });
            updateWithRetries.setStrategy(new backoff.ExponentialStrategy());
            updateWithRetries.failAfter(10);

            // Starts the Habitat object update process.
            updatesInFlight++;
            updateWithRetries.start();
          } catch (e) {
            console.error('Failed to parse file:', objectPath);
github node-opcua / node-opcua / packages / node-opcua-secure-channel / source / client / client_secure_channel_layer.ts View on Github external
this.lastError = undefined;

        if (this.connectionStrategy.maxRetry === 0) {
            debugLog(chalk.cyan("max Retry === 0 =>  No backoff required -> call the _connect function directly"));
            this.__call = 0;
            return this._connect(transport, endpointUrl, callback);
        }

        const connectFunc = (callback2: ErrorCallback) => {
            return this._connect(transport, endpointUrl, callback2);
        };
        const completionFunc = (err?: Error) => {
            return this._backoff_completion(err, this.lastError, transport, callback);
        };

        this.__call = backoff.call(connectFunc, completionFunc);

        if (this.connectionStrategy.maxRetry >= 0) {
            const maxRetry = Math.max(this.connectionStrategy.maxRetry, 1);
            debugLog(chalk.cyan("backoff will failed after "), maxRetry);
            this.__call.failAfter(maxRetry);
        } else {
            // retry will be infinite
            debugLog(chalk.cyan("backoff => starting a infinite retry"));
        }

        const onBackoffFunc = (retryCount: number, delay: number) => {
            debugLog(chalk.bgWhite.cyan(" Backoff #"), retryCount, "delay = ", delay,
                " ms", " maxRetry ", this.connectionStrategy.maxRetry);
            // Do something when backoff starts, e.g. show to the
            // user the delay before next reconnection attempt.
            /**
github node-opcua / node-opcua / packages / node-opcua-secure-channel / src / client / client_secure_channel_layer.js View on Github external
if (self.__call) {
                // console log =
                transport.numberOfRetry = transport.numberOfRetry || 0;
                transport.numberOfRetry += self.__call.getNumRetries();
                self.__call.removeAllListeners();
                self.__call = null;

                if (err) {
                    callback(last_err|| err);
                } else {
                    callback();
                }
            }
        }

        self.__call = backoff.call(_connect, _backoff_completion);

        if ( self.connectionStrategy.maxRetry >= 0){
            const maxRetry = Math.max(self.connectionStrategy.maxRetry, 1);
            self.__call.failAfter(maxRetry);
            debugLog("backoff will failed after ".cyan, maxRetry);
        } else {
            // retry will be infinite
            debugLog("backoff => starting a infinite retry".cyan, self.connectionStrategy.initialDelay);
        }


        self.__call.on("backoff", function (number, delay) {

            debugLog(" Backoff #".bgWhite.cyan, number, "delay = ", delay, self.connectionStrategy.maxRetry);
            // Do something when backoff starts, e.g. show to the
            // user the delay before next reconnection attempt.
github mcavage / node-fast / lib / client.js View on Github external
Client.prototype.connect = function connect() {
    if (this._fast_retry)
        throw new Error('already connecting');

    var self = this;
    this.closed = false;
    var max = Infinity;
    var opts = this._options;
    var retry = backoff.call(this._createSocket.bind(this), {},
    function (err, conn) {
        // Starting with backoff 2.5.0, the backoff callback is called when the
        // backoff instance is aborted. In this case, the _onConnection callback
        // should not be called, since its purpose is to handle the result of
        // the bind call that is being backed off, not events in the backoff
        // process itself.
        if (!retry.isAborted()) {
            self._onConnection(err, conn);
        }
    });

    retry.on('backoff', this.emit.bind(this, 'connectAttempt'));
    retry.setStrategy(new backoff.ExponentialStrategy({
        initialDelay: opts.retry.minTimeout || 1000,
        maxDelay: opts.retry.maxTimeout || Infinity
    }));
github science / openthermo / server / node_modules / restify / lib / clients / http_client.js View on Github external
HttpClient.prototype.request = function request(opts, cb) {
        assert.object(opts, 'options');
        assert.func(cb, 'callback');

        cb = once(cb);

        var call;
        var retry = cloneRetryOptions(opts.retry);

        call = backoff.call(rawRequest, opts, cb);
        call.setStrategy(new backoff.ExponentialStrategy({
                initialDelay: retry.minTimeout,
                maxDelay: retry.maxTimeout
        }));
        call.failAfter(retry.retries);
        call.on('backoff', this.emit.bind(this, 'attempt'));

        call.start();
};
github whois-api-llc / node-email-verifier / index.js View on Github external
verify(email, opts, cb) {
    if (!cb) {
      cb = opts;
      opts = {};
    }

    let call = backoff.call(request, {
      uri: VERIFY_URI,
      headers: { "User-Agent": "node-email-verifier/" + VERSION },
      qs: {
        apiKey: this.apiKey,
        emailAddress: email,
        validateDNS: (this.opts.validateDNS === false ? false : true),
        validateSMTP: (this.opts.validateSMTP === false ? false : true),
        checkCatchAll: (this.opts.checkCatchAll === false ? false : true),
        checkFree: (this.opts.checkFree === false ? false : true),
        checkDisposable: (this.opts.checkDisposable === false ? false : true),
        _hardRefresh: (opts.hardRefresh === true ? 1 : 0),
        outputFormat: "json"
      }
    }, (err, resp, body) => {
      if (err) {
        return cb(err);
github restify / node-restify / lib / clients / http_client.js View on Github external
HttpClient.prototype.request = function request(opts, cb) {
    assert.object(opts, 'options');
    assert.func(cb, 'callback');

    cb = once(cb);

    if (opts.retry === false) {
        rawRequest(opts, cb);
        return;
    }

    var call;
    var retry = cloneRetryOptions(opts.retry);

    opts._keep_alive = this._keep_alive;
    call = backoff.call(rawRequest, opts, cb);
    call.setStrategy(new backoff.ExponentialStrategy({
        initialDelay: retry.minTimeout,
        maxDelay: retry.maxTimeout
    }));
    call.failAfter(retry.retries);
    call.on('backoff', this.emit.bind(this, 'attempt'));

    call.start();
};
github joyent / manatee / client / manatee.js View on Github external
_cb(null, client);
        }

        function onError(err) {
            client.removeListener('connect', onConnect);
            _cb(err);
        }


        client.once('connect', onConnect);
        client.once('error', onError);

        client.connect();
    }

    var retry = backoff.call(_createClient, null, cb);
    retry.failAfter(Infinity);
    retry.setStrategy(new backoff.ExponentialStrategy({
        initialDelay: 1000,
        maxDelay: 30000
    }));

    retry.on('backoff', function (number, delay) {
        var level;
        if (number === 0) {
            level = 'info';
        } else if (number < 5) {
            level = 'warn';
        } else {
            level = 'error';
        }
        log[level]({

backoff

Fibonacci and exponential backoffs.

MIT
Latest version published 8 years ago

Package Health Score

65 / 100
Full package analysis