/
pool.go
137 lines (123 loc) · 2.9 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package redigos
import (
"net"
"sync"
"time"
"github.com/astaxie/beego"
"github.com/garyburd/redigo/redis"
)
var redisPools map[string]*RedisPool
var poolLock sync.RWMutex
func init() {
redisPools = make(map[string]*RedisPool)
}
func newRedisPool(addr string, maxIdle int, idleTimeout time.Duration) *RedisPool {
return &RedisPool{
addr: addr,
MaxIdle: maxIdle,
IdleTimeout: idleTimeout,
pools: make(map[string]*redis.Pool),
}
}
// RedisPool redis pool
type RedisPool struct {
pools map[string]*redis.Pool
addr string
MaxIdle int
IdleTimeout time.Duration
lock sync.RWMutex
}
func (p *RedisPool) get(db string) (pool *redis.Pool) {
p.lock.RLock()
pool = p.pools[db]
p.lock.RUnlock()
return
}
// var redisDialCount int64
func (p *RedisPool) register(db string) (pool *redis.Pool) {
var ok bool
p.lock.Lock()
if pool, ok = p.pools[db]; !ok {
pool = &redis.Pool{
MaxIdle: p.MaxIdle,
MaxActive: p.MaxIdle,
IdleTimeout: p.IdleTimeout,
Wait: true,
Dial: func() (redis.Conn, error) {
tcpAddr, err := net.ResolveTCPAddr("tcp", p.addr)
if err != nil {
beego.Info("ResolveTCPAddr Error" + err.Error())
return nil, err
}
tc, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
beego.Info("DialTCP Error" + err.Error())
return nil, err
}
if err = tc.SetKeepAlive(true); err != nil {
beego.Info("SetKeepAlive Error" + err.Error())
return nil, err
}
if err = tc.SetKeepAlivePeriod(2 * time.Hour); err != nil {
beego.Info("SetKeepAlivePeriod Error" + err.Error())
return nil, err
}
c := redis.NewConn(tc, 10*time.Second, 10*time.Second)
_, err = c.Do("SELECT", db)
if err != nil {
beego.Info("cant select" + db + err.Error())
return nil, err
}
beego.Info("RedisDialAgain")
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := c.Do("PING")
return err
},
}
p.pools[db] = pool
}
p.lock.Unlock()
return pool
}
// Get get
func (p *RedisPool) Get(db string) (pool *redis.Pool) {
pool = p.get(db)
if pool != nil {
return pool
}
return p.register(db)
}
func getRedisPool(addr, db string) (pool *redis.Pool) {
poolLock.RLock()
if p, ok := redisPools[addr]; ok {
pool = p.Get(db)
}
poolLock.RUnlock()
return nil
}
func registerRedisPool(addr string, maxIdle int, idleTimeout time.Duration) *RedisPool {
var (
p *RedisPool
ok bool
)
poolLock.Lock()
if p, ok = redisPools[addr]; !ok {
p = newRedisPool(addr, maxIdle, idleTimeout)
redisPools[addr] = p
}
poolLock.Unlock()
return p
}
// GetRedisPool get redis pool
func GetRedisPool(addr, db string, maxIdle int, idleTimeout time.Duration) *redis.Pool {
p := getRedisPool(addr, db)
if p != nil {
return p
}
return registerRedisPool(addr, maxIdle, idleTimeout).Get(db)
}