Giter VIP home page Giter VIP logo

callagain's Introduction

CallAgain

This package help you to deal with function that:

  • should be called again with same arguments in case of some specific errors;
  • should not be called more than N time in some time unit;
  • should not be called simultaneously more then N times;

Installing

$ npm install callagain

Basic usage example

// Here is function, it makes direct call to some API
const {loadItemsFromRemoteServer} = requires('./lib');

// Import helper
const {CallAgain} = require('callagain');

// Create instance
const c = new CallAgain({
    // Concurrency settings
    maxConcurrentCalls: 10, // Extrernal API allows only 10 concurrent calls

    // Retry on error settings
    maxRetryAttempts: 5, // Try 5 time before throw error
    delayOnRetry: 5000, // If some call fails, wait 5 sec and try again
    
    // Rate limit settings
    maxCallsPerInterval: 50, // External API allows 50 calls per minute
    intervalLength: 60000 // External API allows 50 calls per minute
});

// Use helper's *wrap* method to create safe version of needed function
const safeLoadItems = c.wrap(loadItemsFromRemoteServer);

// Now we can simply call our function
for (int page=0; page<100; page++) {
    safeLoadItems(page)
        .then(items => {
    		// ...    
	    })
    	.catch(e => console.error(e.message))
}

Error handling

You can specify error handlers

const c = new CallAgain({...});

// wrapping axios get call
const getItems = c.wrap(url => axios.get);


c.onError((err, next) => {
    // here err is axios error
    
    // this function must return boolean value
    // indicating should CallAgain retry or not
    
    
    if (err.response.status >= 500) {
            // Unknown server error, retry in *delayOnRetry* milliseconds
            return true
    }  
        
    
    // if you do not know, how to deal with error, you should pass it to next handler
    return next(err)
});

//handlers han be chained
c.onError(...).onError(...).onError(...)

callagain's People

Contributors

atomaltera avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.