Skip to content

Instantly share code, notes, and snippets.

@HeCorr
Last active December 27, 2020 15:14
Show Gist options
  • Save HeCorr/1cb443233874b7a5d037b95f11cb96da to your computer and use it in GitHub Desktop.
Save HeCorr/1cb443233874b7a5d037b95f11cb96da to your computer and use it in GitHub Desktop.
Redigo connection pool with AUTH and PING
// How to use:
//
// var rpool = newPool() // can be global
// c := rpool.Get()
// defer c.Close()
// c.Do("GET", "my:data")
func newPool() *redis.Pool {
return &redis.Pool{
MaxIdle: 10, // number of idle connections to keep for reusing
MaxActive: 1000, // maximum amount of active connections
IdleTimeout: 600 * time.Second, // amount of time to close idle connections
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", os.Getenv("REDIS_ADDR")) // connect
if err != nil {
log.Println(err) // might be redundant
return nil, err
}
_, err = c.Do("AUTH", os.Getenv("REDIS_PASS")) // authentication
if err != nil {
log.Println(err) // might be redundant
return nil, err
}
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING") // ping. will be executed everytime a new connection is created ("test on borrow")
return err
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment