Simple exponential backoff pulled out of Primus by @3rd-Eden
A simple module to be used for creating exponentially weighted backoff attempts. Originally extracted from Primus.
If you were a pre-1.0.0
back user, the API has changed to what is found below.
If you do not like this slightly different abstraction and would prefer the
former, slightly simpler API, it is still available with
The API change thanks to a contribution from @Raynos makes things simpler as you don't have to manage the copying of the options object yourself in order to handle repeated backoff cases.
var http = require'http';var back = require'back';//// Options to use for backoff//// Remark: This object is modified so it should be cloned if you are dealing// with independent backoff attempts and want to use these values as a base.//var options =retries: 3minDelay: 1000 // Defaults to 500msmaxDelay: 10000 // Defaults to infinity// The following option is shown with its default value but you will most// likely never define it as it creates the exponential curve.factor: 2;// Where we will store the backoff instance during a particular backoff attemptvar attempt;var back = attempt || attempt = backoff;return backbackoffif fail// Oh noez we never reconnect :(console.error'Retry failed with ' + errmessage;processexit1;//// Remark: .attempt and .timeout are added to this object internally//console.log'Retry attempt # ' + backsettingsattempt +' being made after ' + backsettingstimeout + 'ms';request;;httpget''console.log'Successful Response that will not happen!';//// If we succeeded, we would set the current to null so the next error// generates a new instance.//attempt = null;on'error' retry;request;
Back constructor function takes your backoff options and saves them as
settings in the internal state of the
back instance has a
backoff method that takes a
callback that is
executed after a
setTimeout. The timeout is what is based on an exponential
backoff of course!
It will repeatedly all this callback based on the backoff options you passed to
the back instance until it exhausts its efforts. When it has exhausted its
attempts, it will return an error as the first argument to the callback.