-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_pool.go
124 lines (117 loc) · 3.42 KB
/
redis_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
package frame
import (
"github.com/gomodule/redigo/redis"
"strconv"
"sync"
"time"
)
type redisGroup struct {
Master *redis.Pool
Slaves []*redis.Pool
}
var redisGroupCache map[string]*redisGroup
var redisGroupCacheOnce sync.Once
var redisLock sync.Mutex
type redisHost struct {
Master *redisHostConfig `toml:"master"`
Slaves []*redisHostConfig `toml:"slaves"`
}
type redisHostConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
Password string `toml:"password"`
Timeout int `toml:"timeout"`
MaxIdle int `toml:"MaxIdle"`
MaxActive int `toml:"MaxActive"`
IdleTimeout int `toml:"IdleTimeout"`
MaxConnLifetime int `toml:"MaxConnLifetime"`
}
/**
建立redis连接池
*/
func GetRedis(groupName string) *redisGroup {
if redisGroupCache == nil {
redisGroupCacheOnce.Do(func() {
redisGroupCache = make(map[string]*redisGroup)
})
}
cache, ok := redisGroupCache[groupName]
if ok {
return cache
}
redisLock.Lock()
defer redisLock.Unlock()
redisConfig := &redisHost{}
err := App().Env(groupName, redisConfig)
if err != nil {
panic(RedisConfigError.Error() + ":" + err.Error())
}
defer func() {
redisError(err)
}()
masterConfig := redisConfig.Master
masterPool := &redis.Pool{
MaxIdle: masterConfig.MaxIdle,
MaxActive: masterConfig.MaxActive,
IdleTimeout: time.Duration(masterConfig.IdleTimeout) * time.Second,
Wait: true, //超过最大连接数就阻塞等待
MaxConnLifetime: time.Duration(masterConfig.MaxConnLifetime) * time.Second, //连接生命周期
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", masterConfig.Host+":"+
strconv.Itoa(masterConfig.Port),
redis.DialPassword(masterConfig.Password),
redis.DialDatabase(0),
redis.DialConnectTimeout(time.Duration(masterConfig.Timeout)*time.Second),
redis.DialReadTimeout(time.Duration(masterConfig.Timeout)*time.Second),
redis.DialWriteTimeout(time.Duration(masterConfig.Timeout)*time.Second))
if err != nil {
if c != nil {
c.Close()
}
return nil, err
}
return c, err
},
}
slavesConfig := redisConfig.Slaves
slavesPool := make([]*redis.Pool, 0)
for _, slaveConfig := range slavesConfig {
slave := &redis.Pool{
MaxIdle: slaveConfig.MaxIdle,
MaxActive: slaveConfig.MaxActive,
IdleTimeout: time.Duration(slaveConfig.IdleTimeout) * time.Second,
Wait: true, //超过最大连接数就阻塞等待
MaxConnLifetime: time.Duration(slaveConfig.MaxConnLifetime) * time.Second, //连接生命周期
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", slaveConfig.Host+":"+
strconv.Itoa(slaveConfig.Port),
redis.DialPassword(slaveConfig.Password),
redis.DialDatabase(0),
redis.DialConnectTimeout(time.Duration(slaveConfig.Timeout)*time.Second),
redis.DialReadTimeout(time.Duration(slaveConfig.Timeout)*time.Second),
redis.DialWriteTimeout(time.Duration(slaveConfig.Timeout)*time.Second))
if err != nil {
if c != nil {
c.Close()
}
return nil, err
}
return c, err
},
}
slavesPool = append(slavesPool, slave)
}
redisGroupCache[groupName] = &redisGroup{Master: masterPool, Slaves: slavesPool}
return redisGroupCache[groupName]
}
func closeRedis() {
if redisGroupCache == nil {
return
}
for _, cache := range redisGroupCache {
_ = cache.Master.Close()
for _, v := range cache.Slaves {
_ = v.Close()
}
}
}