Last active
December 27, 2020 15:14
-
-
Save HeCorr/1cb443233874b7a5d037b95f11cb96da to your computer and use it in GitHub Desktop.
Redigo connection pool with AUTH and PING
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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