discover-providers.js
function retryDiscoverProvider(opts, innerDiscoverProvider) {
    if (!opts) {
        return innerDiscoverProvider;
    }

    return function retryingDiscoverProvider(callback) {
        var policy;
        var defaultPolicy = {
            minDelay: 500,
            maxDelay: 15000,
            timeout: 60000
        };

        if (opts === true) {
            policy = defaultPolicy;
        } else {
            policy = _.defaults({}, opts, defaultPolicy);
        }
        var backoff = opts.backoff || new Backoff(policy);
        var lastError = null;

        attempt(null);

        function attempt(fail) {
            if (fail) {
                fail.lastError = lastError;
                return setImmediate(callback, fail);
            }

            innerDiscoverProvider(function onTry(err, hosts) {
                // We got results!
                if (!err && hosts !== null && hosts.length > 0) {
                    return setImmediate(callback, null, hosts);
                }

                lastError = err;
                backoff.retry(attempt);
            });
        }
    };
}
function retryDiscoverProvider(opts, innerDiscoverProvider) {
    if (!opts) {
        return innerDiscoverProvider;
    }

    return function retryingDiscoverProvider(callback) {
        var policy;
        var defaultPolicy = {
            minDelay: 500,
            maxDelay: 15000,
            timeout: 60000
        };

        if (opts === true) {
            policy = defaultPolicy;
        } else {
            policy = _.defaults({}, opts, defaultPolicy);
        }
        var backoff = opts.backoff || new Backoff(policy);
        var lastError = null;

        attempt(null);

        function attempt(fail) {
            if (fail) {
                fail.lastError = lastError;
                return setImmediate(callback, fail);
            }

            innerDiscoverProvider(function onTry(err, hosts) {
                // We got results!
                if (!err && hosts !== null && hosts.length > 0) {
                    return setImmediate(callback, null, hosts);
                }

                lastError = err;
                backoff.retry(attempt);
            });
        }
    };
}
function retryDiscoverProvider ( opts , innerDiscoverProvider ) { if ( ! opts ) { return innerDiscoverProvider ; } return function retryingDiscoverProvider ( callback ) { var policy ; var defaultPolicy = { minDelay : 500 , maxDelay : 15000 , timeout : 60000 } ; if ( opts === true ) { policy = defaultPolicy ; } else { policy = _ . defaults ( { } , opts , defaultPolicy ) ; } var backoff = opts . backoff || new Backoff ( policy ) ; var lastError = null ; attempt ( null ) ; function attempt ( fail ) { if ( fail ) { fail . lastError = lastError ; return setImmediate ( callback , fail ) ; } innerDiscoverProvider ( function onTry ( err , hosts ) { // We got results! if ( ! err && hosts !== null && hosts . length > 0 ) { return setImmediate ( callback , null , hosts ) ; } lastError = err ; backoff . retry ( attempt ) ; } ) ; } } ; }
Wrap a innerDiscoverProvider with a retrying mechanism.

@param {Object} opts the options used as a Backoff policy {@see Backoff}
@param {Object} [opts.backoff] a mocked backoff for testing
@param {DiscoverProvider} innerDiscoverProvider The discover provider to wrap

@returns {DiscoverProvider} The retrying discover provider.
Wrap a innerDiscoverProvider with a retrying mechanism .