Skip to content

Repository files navigation

Redis functions balancer

NPM

Balance executes of NodeJs-functions or anything with redis.

For example, if you have several functions (A, B, C) doing the same things (http requests, long-running code), and you want to execute it evenly.

Working in clusters (PM2, NodeJs Cluster).

Uses Redis list with rank and Javascript iterators.

Ready to use with TypeScript and JavaScript.

Installation

npm install redis-functions-balancer --save-prod

Example of usage

importRedisBalancerfrom"redis-functions-balancer";constredis=require("redis");constredisClient=redis.createClient(6379,'redis');// Your functions here// ... //constA=()=>{};constB=()=>{};constC=()=>{};// ... //letbalancer=newRedisBalancer([A,B,C],redisClient,'example-redis-key');// or reuse balancer variable with another functionsbalancer.setData([A,B]);// ... //// Get async iterator {done, value}letiterator=awaitbalancer.getAsyncIterator();while((foo=awaititerator.next())&&!foo.done){// Your function A|B|C will be here evenlyletfunc=foo.value;try{// Executing on your way (func();}catch(e){// something happen badly and you want to postpone executes of the function next 10 runsbalancer.increaseRank(func,10);}}

About

Balance NodeJs functions with Redis

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages