mirror of
https://github.com/xxjwxc/public.git
synced 2025-09-26 20:01:19 +08:00
fix redis pool
This commit is contained in:
@@ -12,7 +12,7 @@ const keepTTL = -1
|
||||
// MyRedis redis配置项
|
||||
type MyRedis struct {
|
||||
conf *redisOptions
|
||||
con redis.Conn
|
||||
// con redis.Conn
|
||||
pool *redis.Pool
|
||||
mtx sync.Mutex
|
||||
once sync.Once
|
||||
|
@@ -16,14 +16,6 @@ func (mc *redisConPool) Destory() {
|
||||
mc.mtx.Lock()
|
||||
defer mc.mtx.Unlock()
|
||||
|
||||
if mc.con != nil {
|
||||
err := mc.con.Close()
|
||||
if err != nil {
|
||||
mylog.Error(err)
|
||||
}
|
||||
mc.con = nil
|
||||
}
|
||||
|
||||
if mc.pool != nil {
|
||||
mc.pool.Close()
|
||||
}
|
||||
|
@@ -140,45 +140,35 @@ func (mc *base) ping(con redis.Conn) bool {
|
||||
}
|
||||
|
||||
// Dial 获取一个链接
|
||||
func (mc *base) build() error {
|
||||
func (mc *base) build() (con redis.Conn, err error) {
|
||||
err = fmt.Errorf("not fond ")
|
||||
index := mc.conf.addrIdex
|
||||
len := len(mc.conf.addrs)
|
||||
b := false
|
||||
var err error
|
||||
for i := 0; i < len; i++ {
|
||||
index = (mc.conf.addrIdex + i) % len
|
||||
mc.con, err = redis.Dial("tcp", mc.conf.addrs[index], redis.DialClientName(mc.conf.clientName),
|
||||
con, err = redis.Dial("tcp", mc.conf.addrs[index], redis.DialClientName(mc.conf.clientName),
|
||||
redis.DialConnectTimeout(mc.conf.timeout), redis.DialDatabase(mc.conf.db),
|
||||
redis.DialPassword(mc.conf.pwd), redis.DialReadTimeout(mc.conf.readTimeout), redis.DialWriteTimeout(mc.conf.writeTimeout),
|
||||
)
|
||||
if err != nil {
|
||||
mylog.Error(err)
|
||||
}
|
||||
if mc.ping(mc.con) {
|
||||
if mc.ping(con) {
|
||||
b = true
|
||||
}
|
||||
}
|
||||
if b {
|
||||
mc.conf.addrIdex = (index + 1) % len
|
||||
}
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
// Dial 获取一个链接
|
||||
func (mc *base) Dial() (redis.Conn, error) {
|
||||
mc.mtx.Lock()
|
||||
defer mc.mtx.Unlock()
|
||||
if mc.con == nil { // 创建连接
|
||||
err := mc.build()
|
||||
return mc.con, err
|
||||
}
|
||||
|
||||
if mc.ping(mc.con) {
|
||||
return mc.con, nil
|
||||
}
|
||||
|
||||
err := mc.build()
|
||||
return mc.con, err
|
||||
return mc.build() // 创建连接
|
||||
}
|
||||
|
||||
func (mc *base) Do(con redis.Conn, commandName string, args ...interface{}) (reply interface{}, err error) {
|
||||
@@ -220,6 +210,7 @@ func (mc *base) fixKeyGroupName(key string) string {
|
||||
|
||||
type redisConOlny struct {
|
||||
base
|
||||
con redis.Conn
|
||||
}
|
||||
|
||||
// Destory 析构
|
||||
@@ -238,8 +229,11 @@ func (mc *redisConOlny) Destory() {
|
||||
|
||||
// GetRedisClient ...
|
||||
func (mc *redisConOlny) GetRedisClient() redis.Conn {
|
||||
con, _ := mc.Dial()
|
||||
return con
|
||||
if mc.con == nil {
|
||||
con, _ := mc.Dial()
|
||||
mc.con = con
|
||||
}
|
||||
return mc.con
|
||||
}
|
||||
|
||||
// Ping 判断是否能ping通
|
||||
|
@@ -140,7 +140,6 @@ func UnicodeEmojiCode(s string) string {
|
||||
if len(string(rs[i])) == 4 {
|
||||
u := `[\u` + strconv.FormatInt(int64(rs[i]), 16) + `]`
|
||||
ret += u
|
||||
|
||||
} else {
|
||||
ret += string(rs[i])
|
||||
}
|
||||
|
Reference in New Issue
Block a user