GithubHelp home page GithubHelp logo

silverbucket / redis-connection-pool Goto Github PK

View Code? Open in Web Editor NEW
60.0 7.0 30.0 1004 KB

A node.js connection manager for Redis

Home Page: https://silverbucket.github.io/redis-connection-pool

TypeScript 100.00%
generic-pool pool redis redis-client

redis-connection-pool's Introduction

redis-connection-pool

A node.js connection pool for Redis.

https://silverbucket.github.io/redis-connection-pool

Build Status license downloads

About

A high-level redis connection pooling object. It manages a number of connections in a pool, using them as needed and keeping all aspects of releasing active connections internal to the object.

Installation

npm install redis-connection-pool

Usage

import redisPoolFactory from 'redis-connection-pool';
const redisPool = await redisPoolFactory('myRedisPool', {
    max_clients: 5, // default
    redis: {
      url: 'redis://localhost:6379'
    }
  });


await redisPool.set('test-key', 'foobar');
const foo = await redisPool.get('test-key');
// returns 'foobar'

Or you can create a pool instance directly

import RedisConnectionPool from 'redis-connection-pool';
const redisPool = new RedisConnectionPool();
await redisPool.init();

When you are done

redisPool.shutdown();

Implemented Redis methods

  • blpop
  • brpop
  • del
  • expire
  • get
  • hdel
  • hget
  • hgetall
  • hset
  • incr
  • keys
  • lpush
  • rpush
  • sendCommand
  • set
  • ttl

Additional methods

  • init
  • shutdown

API Documentation

For the full documentation on the RedisConnectionPool class, see https://silverbucket.github.io/redis-connection-pool/classes/RedisConnectionPool.html

License

MIT

redis-connection-pool's People

Contributors

bitdeli-chef avatar chiangkeith avatar davefinster avatar dependabot[bot] avatar golbin avatar jlewitt avatar kantai avatar renovate-bot avatar renovate[bot] avatar silverbucket avatar srirampandikasala avatar vishnukt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

redis-connection-pool's Issues

How to set pool options

I want customise redis-connection-pool options in my nodejs-express project.

Let me know what are the parameters in redis-pool and how can I set.

Thanks in advance.

ReplyError: Ready check failed: NOAUTH Authentication required.

hi,when i use pm2 start app.js,The following error occurred

2|app | YD For pcweb server listening on port 4000
2|app | plugins load completed
2|app | ReplyError: Ready check failed: NOAUTH Authentication required.
2|app | at parseError (/opt/node-publish/www/node_modules/redis-parser/lib/parser.js:167:12)
2|app | at parseType (/opt/node-publish/www/node_modules/redis-parser/lib/parser.js:228:14)

but node app.js everything is ok。Beg God help😭

Incompatibility with fakeredis mock

Hello,

I have encountered a very minor trouble when using fakeredis for testing in my application.
In redisCheck function, the ready handler looks like this:

function () {
      self.version_string = client.server_info.redis_version;
      self.version_array = client.server_info.versions;
      if (self.version_array[0] < 2) {
        self.blocking_support = false;
      }
      client.quit();
      q.resolve(self.version_string);
    }

Now, the fakeredis mock does not supply it with server_info, which means self.version_array will be undefined, which in turn will cause JS to blow up on getting zeroth element from an undefined variable.

Proposed solution would be super simple:

function () {
      client.server_info = client.server_info || {};
      self.version_string = client.server_info.redis_version;
      self.version_array = client.server_info.versions;
      if (!self.version_array || self.version_array[0] < 2) {
        self.blocking_support = false;
      }
      client.quit();
      q.resolve(self.version_string);
    }

However - I am not sure whether this is an issue of fakeredis or redis-connection-pool; but I have a slight preference towards foolproof code hence why am I writing here first. Naturally, fakeredis should also mimic redis properly, but who knows what is wrong with other redis mocks?

What do you think?

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update dependency @types/proxyquire to v1.3.31
  • Update dependency @types/sinon to v10.0.20
  • Update dependency chai to v4.3.10 (chai, @types/chai)
  • Update dependency ts-node to v10.9.2
  • Update dependency c8 to v7.14.0
  • Update dependency generic-pool to v3.9.0
  • Update dependency redis to v4.6.11
  • Update superchargejs/redis-github-action action to v1.8.0
  • Update actions/checkout action to v4
  • Update actions/setup-node action to v4
  • Update dependency @types/node to v20
  • Update dependency c8 to v8
  • Update dependency jsdoc-to-markdown to v8
  • Update dependency sinon to v17 (sinon, @types/sinon)
  • Update dependency typescript to v5
  • Update github/codeql-action action to v3
  • Update typescript-eslint monorepo to v6 (major) (@typescript-eslint/eslint-plugin, @typescript-eslint/parser)
  • 🔐 Create all rate-limited PRs at once 🔐

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v3
  • github/codeql-action v2
  • github/codeql-action v2
  • github/codeql-action v2
.github/workflows/compliance.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v1
  • actions/setup-node v3
  • superchargejs/redis-github-action 1.4.0
.github/workflows/release-drafter.yml
  • release-drafter/release-drafter v5.20.1
  • release-drafter/release-drafter v5
npm
package.json
  • debug ^4.3.4
  • generic-pool ^3.8.2
  • redis ^4.3.0
  • @babel/cli 7.18.10
  • @babel/core 7.18.13
  • @babel/preset-env 7.18.10
  • @babel/preset-typescript 7.18.6
  • @types/chai 4.3.3
  • @types/debug 4.1.7
  • @types/eslint 8.4.6
  • @types/generic-pool 3.1.11
  • @types/mocha 9.1.1
  • @types/node 17.0.23
  • @types/proxyquire 1.3.28
  • @types/redis 4.0.11
  • @types/sinon 10.0.13
  • @typescript-eslint/eslint-plugin 5.36.1
  • @typescript-eslint/parser 5.36.1
  • c8 7.12.0
  • chai 4.3.6
  • eslint 8.23.0
  • eslint-plugin-security-node 1.1.1
  • jsdoc-babel 0.5.0
  • jsdoc-to-markdown 7.1.1
  • mocha 10.0.0
  • proxyquire 2.1.3
  • sinon 14.0.0
  • ts-node 10.9.1
  • typedoc 0.23.13
  • typescript 4.8.2

  • Check this box to trigger a request for Renovate to run again on this repository

on event is not work

I want to listening status when redis start, So I call on.('ready', () => {}), but this function will create new instance without paramters every time, not use instance created above.
RedisConnectionPool.prototype.on = function (type, cb) { const client = redis.createClient(); client.on(type, cb); };
I think it's a bug, event "on" function is not useful.

Not get the host i set

Hi,
I set the address as '192.168.2.221',but i still got the error 'Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379'

i dont know why createClient no options

createClient why no options?

redis-connection-pool.js
144-146
RedisConnectionPool.prototype.on = function(type, cb) {
var client = redis.createClient();
client.on(type, cb);
};

Unable to set TTL

This package does not have the feature to set TTL (Time To Live) for key values stored in redis, even though the dependency package 'redis' allows to set TTL.

[Bug] Error on executing send_command method


name: "🐞 Bug Report"
about: "send_command method"
title: "[Bug] Error on executing send_command method"
labels: "Type: Bug"


🐞 Bug Report

Describe the bug

The following error is getting thrown on executing any redis command through the send_command.

               res = yield client[funcName](key, val);
                                            ^

TypeError: client[funcName] is not a function
    at RedisConnectionPool.<anonymous> (d:\Sri\Coding\OpenSource\node-redis-connection-pool\dist\index.js:429:45)
    at Generator.next (<anonymous>)
    at fulfilled (d:\Sri\Coding\OpenSource\node-redis-connection-pool\dist\index.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

To Reproduce

  • After initialising redispool ( redisPoolObj.init() ), the following code was added for which the aforementioned error was thrown
redisPoolObj.send_command('HSET', ['firstRedisKey', 'key1', 'Hello, Redis']).then(resp => {
    console.log(resp)
});
  • The same error is getting thrown for other commands like HMGET, HGET, etc.

Expected behaviour

  • It should get executed as per the command provided and not throw any error

Your environment

  • OS: Windows 10
  • Node version: 16.15
  • Npm version: 8.5.5
  • Redis version: 7.0.2 (Docker)

ERROR : uncaughtException: pool.getPoolSize is not a function

Hi,

I'm currently facing a production error: with message :
uncaughtException: pool.getPoolSize is not a function

in node_modules/redis-connection-pool/src/redis-connection-pool.js:127:40

debug('REDIS POOL: [size: ' + pool.getPoolSize() +
                ' avail:' + pool.availableObjectsCount() +
                ' waiting:' + pool.waitingClientsCount() + ']');

NOAUTH Authentication required

Using "redis-connection-pool": "^4.0.1" and redisPoolFactory I am not sure anymore how to authenticate to the redis server.

Connecting to Redis in an Express application causes TypeError

I am developing an API with ExpressJS where Redis is needed to store look-up information. I have set-up pools in redispools.js as shown below.

var redisPool = require('redis-connection-pool');
//
var pool0 = redisPool('A', { database: 0 });
var pool1 = redisPool('B', { database: 1 });
var pool2 = redisPool('C', { database: 2 });
//
pool0.on('error', function(err) {console.log(err)});
pool1.on('error', function(err) {console.log(err)});
pool2.on('error', function(err) {console.log(err)});
//
var pools = { A: pool0, B: pool1, C: pool2 };
module.exports = pools;

In lookup.js I have the following:

var express = require('express');
var router = express.Router();
var redisPools = require('../app/redispools');

// Process payload
router.post('/process/*', function (req, res, next) {
  var redisConfigConn = redisPools.A
  redisConfigConn.set('key', req.body, function (err) {
    if (err) {
      console.log(err);
    } else {
      console.log('Redis inserted.')
    }
  });
  var resp = {
    'IP-Address': req.ip,
    'Path': req.path
  }
  res.status(200);
  res.send(resp);
});

module.exports = router;

Once started with pm2, I have this in logs:

TypeError: Cannot read property 'name' of undefined
    at RedisClient.internal_send_command (/home/ubuntu/code/src/server/node_modules/redis/index.js:914:128)
    at RedisClient.set (/home/ubuntu/code/src/server/node_modules/redis/lib/commands.js:58:25)
    at pool.acquire.then (/home/ubuntu/code/src/server/node_modules/redis-connection-pool/src/redis-connection-pool.js:523:23)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)

What is missing?

different databases

One thing I am confused about with this library - what if you are using different Redis databases (perhaps all of the 16 defaults?)

If I retrieve a connection from the pool, can I tell it what database to do the gets/sets with? Can I retrieve a connection that is connected to one particular database?

I really don't get it.

Connects with 127.0.0.1 instead of host parameter value when running in a container.

I am running my container as follows:

docker run --rm -p 80:3001 -it $(pwd)/conf/server.json:/app/conf/server.json -v $(pwd)/conf/redis.json:/app/conf/redis.json username/reponame:tag

Before making the connection, the options object for Redis connection does have the values as defined in $(pwd)/conf/redis.json. The connection fails with the following error.

Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379

How to get around this problem?

Update Readme

When using nodejs, you need to do:

const { RedisConnectionPool } = require("redis-connection-pool");

const RedisSettings= {
    redis: { //standard "node-redis" aka "redis" settings
        password: redis.password, 
        url: redis.url,
        retry_strategy: () => 1000
    },
    max_clients: 50,
}
// Invoke a new connectionpool class
const redisClient = new RedisConnectionPool('myRedisPool', RedisSettings);
// Initialize it
redisClient.init().then(async (_) => {
   console.log('Redis Client ready');
   //NOW it's ready and now you can call redis functions
}).catch(e => console.error('FAILED FOR Redis Client', e))

How To Use .on Property

When creating a pool, where can I use the .on property for the clients? can you give an example?

Use of `hiredis`

In the package.json you have included hiredis as dependency. But you haven't used it anywhere. Will you remove it? On windows machine, hiredis requires lots of things to build. Thanks.

Option to close the pool

What?

Method to close all the pool connections at once

Why?

Currently there is no method to close the entire pool for gracefully shutdown
By extension this can also be used to dynamically control the number of active connections

Handling connection errors.

What is the correct way to handle error while connecting or otherwise? I am running the code below against a un-started Redis on localhost.

const redisPool = require('redis-connection-pool')
let registryPool
//
function connect(options, callback) {
    registryPool = redisPool('registryPool', options)
    registryPool.on('error', (err) => {
        return callback(err, null)
    })
    registryPool.on('connect', () => {
        return callback(null, registryPool)
    })
}
//
module.exports = {
    connect
}

I get an error as follows:

(node:8566) UnhandledPromiseRejectionWarning: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
(node:8566) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:8566) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

And, .catch() or .then() do not seem to be supported.

So, how do we handle errors?

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.