6

using redigo, I create a pool, something like so:

&redis.Pool{
    MaxIdle:   80,
    MaxActive: 12000, // max number of connections
    Dial: func() (redis.Conn, error) {
        c, err := redis.Dial("tcp", host+":"+port)
        if err != nil {
            panic(err.Error())
        }
        return c, err
    }

the problem I have is that for each time I get a new connection, I need to set the db, as I use different db's of redis since I host a number of sites on the VPS.

So, something like this:

conn := pool.Get()
defer conn.Close()

conn.Do("SELECT", dbNumber)  //this is the call I want to avoid

Having to select the db each time I work with redis seems redundant and also poses a problem since I use it for sessions i.e. having code that is not mine working with my redis connection from my pool makes it "impossible" to set the correct db for it.

What I would like to do is to set the dbno for the pool so that whenever somebody asks for a new connection from the pool, it comes with the correct db already set i.e. not setting it explicitly each time.

How did you solve this in your applications?

Thanks.

Adergaard
  • 760
  • 10
  • 24

4 Answers4

9

You can use redis.DialOption: redis.DialDatabase, redis.DialPassword

conn, err := redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(1))
if err != nil {
    panic(err)
}
defer conn.Close()
Liu Zhihui
  • 241
  • 3
  • 6
  • You saved my day. Can't find this piece of info anywhere in the their doc! Maybe it's hiding somewhere but really frustrating in that sense. – Shawn Jul 21 '20 at 02:38
5

Select the database in your dial function:

&redis.Pool{
    MaxIdle:   80,
    MaxActive: 12000, // max number of connections
    Dial: func() (redis.Conn, error) {
        c, err := redis.Dial("tcp", host+":"+port)
        if err != nil {
            return nil, err
        }
        _, err := c.Do("SELECT", dbNum)
        if err != nil {
           c.Close()
           return nil, err
        }
        return c, nil
    }

Also, return the error from dial instead of panicking.

Simon Fox
  • 5,995
  • 1
  • 18
  • 22
2

If these libs don't support it, then you have two options:

  1. submit a patch to automate this (the python lib does that, but be careful when keeping the state).

  2. Wrap your redis pool with your own custom pool that automates this, something like (untested code, but you'll get the idea):

        // a pool embedding the original pool and adding adbno state
        type DbnoPool struct {
           redis.Pool
           dbno int
        }
    
    
        // "overriding" the Get method
        func (p *DbnoPool)Get() Connection {
           conn := p.Pool.Get()
           conn.Do("SELECT", p.dbno)
           return conn
        }
    
        pool := &DbnoPool {
            redis.Pool{
                MaxIdle:   80,
                MaxActive: 12000, // max number of connections
                Dial: func() (redis.Conn, error) {
                c, err := redis.Dial("tcp", host+":"+port)
                if err != nil {
                    panic(err.Error())
                }
                return c, err
            },
            3, // the db number
        }
    
        //now you call it normally
        conn := pool.Get()
        defer conn.Close()
    
Not_a_Golfer
  • 47,012
  • 14
  • 126
  • 92
0

Best way is to use DialOptions like DialDatabase:

redisPool = &redis.Pool{

    MaxIdle:     AppConfig.DefaultInt("RedisMaxPool", 10),
    IdleTimeout: 240 * time.Second,

    Dial: func() (redis.Conn, error) {
        c, err := redis.Dial(
            "tcp",
            AppConfig.DefaultString("RedisPath", ":6379"),
            redis.DialDatabase(AppConfig.DefaultInt("RedisDB", 1)),
        )
        if err != nil {
            return nil, err
        }
        return c, err
    },

    TestOnBorrow: func(c redis.Conn, t time.Time) error {
        _, err := c.Do("PING")
        return err
    },
}
hamidfzm
  • 4,595
  • 8
  • 48
  • 80