1.0.8 • Published 3 years ago

x-retry v1.0.8

Weekly downloads
30
License
MIT
Repository
github
Last release
3 years ago

Retry callback based or async function

Example

  • Operation
const BusyService = {
  call_times : 0,
  getHelloAtEach3Times : function (must_be_hi, callback) {
    this.call_times++;
    
    if (this.call_times % 3 === 0) {
      return callback(null, 'hello');
    }

    let BusyError = new Error('Service is busy');
    BusyError.status = 503;

    return callback(BusyError);
  },
  asyncGetHelloAtEach3Times : async function (must_be_hi) {
    this.call_times++;
    
    if (this.call_times % 3 === 0) {
      return 'hello';
    }

    let BusyError = new Error('Service is busy');
    BusyError.status = 503;

    throw BusyError;
  }
};
  • Async Retry
const { asyncRetry, Timeout } = require('x-retry');

it ('should retry a async function ok after three times', async () => {
  let message = await asyncRetry({
    func      : BusyService.asyncGetHelloAtEach3Times,
    thisArg   : BusyService,
    args      : ['hi'],
    isRetry   : (error) => !(error.status >= 400 && error.status < 500),
    maxRetry  : 3,
    timeout   : Timeout({ minTimeout : 100, maxTimeout : 10000 })
  });

  assert.equal(message, 'hello');
});
  • Callback Retry
const { callbackRetry, Timeout } = require('x-retry');

it ('should retry a callback function ok after three times', (done) => {
  callbackRetry({
    func      : BusyService.getHelloAtEach3Times,
    thisArg   : BusyService,
    args      : ['hi'],
    isRetry   : (error) => !(error.status >= 400 && error.status < 500),
    maxRetry  : 3,
    timeout   : Timeout({ minTimeout : 100, maxTimeout : 10000 }),
    callback  : (err, message) => (!err && message === 'hello') ? done() : done(err) 
  });
});

Functions

asyncRetry(options) ⇒ Promise.<any>

Retry a async function

Kind: function
Returns: Promise.<any> - result of async func
Throws:

ParamTypeDefaultDescription
optionsobject
options.funcfunctionasync function to retry
options.thisArgobjectthis pointer apply to function
options.argsArray.<any>arguments of function
options.isRetryfunction(error) => boolean
options.maxRetrynumber3max retry times
options.timeoutnumber | functiondelay between retry operation, default is generated by Timeout. if is function, must match interface : (retry_count, maxRetry, logs) => number
options.activitystringwhat is this activity name ? default is func.name
options.actorstringwho do this activity ? default is thisArg.name

callbackRetry(options) ⇒ void

Retry an callback based function

Kind: function
Errors: :

ParamTypeDefaultDescription
optionsobject
options.funcfunctioncallback based function to retry
options.thisArgobjectthis pointer apply to function
options.argsArray.<any>arguments of function
options.callbackfunctioncallback of function
options.isRetryfunction(error) => boolean
options.maxRetrynumber3max retry times
options.timeoutnumber | function200delay between retry operation, , default is generated by Timeout. if is function, must match interface : (retry_count, maxRetry, logs) => number
options.activitystringwhat is this activity name ? default is func.name
options.actorstringwho do this activity ? default is thisArg.name

Timeout(options) ⇒ function

Create function that generate timeout by exponential backoff algorithm

Kind: function
Returns: function - generate timeout

ParamTypeDefault
optionsObject
options.minTimeoutNumber20
options.maxTimeoutNumberInfinite
options.factorNumber2
options.randomizeBooleantrue

Timeout~generateTimeout(retryCount) ⇒ number

Generate timeout = Math.min(random minTimeout Math.pow(factor, retryCount), maxTimeout)

Kind: inner method of Timeout
Returns: number - timeout

ParamTypeDefault
retryCountnumber1
1.0.8

3 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago