-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
46 lines (38 loc) · 828 Bytes
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package redis_pack
import (
"log"
"time"
"github.com/gomodule/redigo/redis"
)
var pool *redis.Pool
func initPool(addr, password string) {
pool = &redis.Pool{
MaxIdle: 10,
MaxActive: 200,
IdleTimeout: 180 * time.Second,
Wait: true,
Dial: func() (redis.Conn, error) {
return setDialog(addr, password)
},
}
}
func initPoolByOld(old *redis.Pool) {
pool = old
}
func setDialog(addr, password string) (redis.Conn, error) {
conn, err := redis.Dial("tcp", addr)
if err != nil {
return nil, err
}
if len(password) != 0 {
if _, err := conn.Do("AUTH", password); err != nil {
_ = conn.Close()
return nil, err
}
}
r, err := redis.String(conn.Do("PING"))
if err != nil || r != "PONG" {
log.Fatalf("failed to connect redis: %v, err: %v", addr, RedigoConn)
}
return conn, nil
}