mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	@@ -1,73 +1,81 @@
 | 
			
		||||
package rdm
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"mayfly-go/internal/machine/mcm"
 | 
			
		||||
	"mayfly-go/internal/pkg/consts"
 | 
			
		||||
	"mayfly-go/pkg/cache"
 | 
			
		||||
	"mayfly-go/pkg/logx"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"context"
 | 
			
		||||
	"mayfly-go/pkg/pool"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// redis客户端连接缓存,指定时间内没有访问则会被关闭
 | 
			
		||||
var connCache = cache.NewTimedCache(consts.RedisConnExpireTime, 5*time.Second).
 | 
			
		||||
	WithUpdateAccessTime(true).
 | 
			
		||||
	OnEvicted(func(key any, value any) {
 | 
			
		||||
		logx.Info(fmt.Sprintf("remove the redis connection cache id = %s", key))
 | 
			
		||||
		value.(*RedisConn).Close()
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	mcm.AddCheckSshTunnelMachineUseFunc(func(machineId int) bool {
 | 
			
		||||
		// 遍历所有redis连接实例,若存在redis实例使用该ssh隧道机器,则返回true,表示还在使用中...
 | 
			
		||||
		items := connCache.Items()
 | 
			
		||||
		for _, v := range items {
 | 
			
		||||
			if v.Value.(*RedisConn).Info.SshTunnelMachineId == machineId {
 | 
			
		||||
				return true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return false
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var mutex sync.Mutex
 | 
			
		||||
var connPool = make(map[string]pool.Pool)
 | 
			
		||||
 | 
			
		||||
func getPool(redisId uint64, db int, getRedisInfo func() (*RedisInfo, error)) (pool.Pool, error) {
 | 
			
		||||
	connId := getConnId(redisId, db)
 | 
			
		||||
	// 获取连接池,如果没有,则创建一个
 | 
			
		||||
	if p, ok := connPool[connId]; !ok {
 | 
			
		||||
		var err error
 | 
			
		||||
		p, err = pool.NewChannelPool(&pool.Config{
 | 
			
		||||
			InitialCap:  1,                //资源池初始连接数
 | 
			
		||||
			MaxCap:      10,               //最大空闲连接数
 | 
			
		||||
			MaxIdle:     10,               //最大并发连接数
 | 
			
		||||
			IdleTimeout: 10 * time.Minute, // 连接最大空闲时间,过期则失效
 | 
			
		||||
			Factory: func() (interface{}, error) {
 | 
			
		||||
				// 若缓存中不存在,则从回调函数中获取RedisInfo
 | 
			
		||||
				ri, err := getRedisInfo()
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					return nil, err
 | 
			
		||||
				}
 | 
			
		||||
				// 连接数据库
 | 
			
		||||
				return ri.Conn()
 | 
			
		||||
			},
 | 
			
		||||
			Close: func(v interface{}) error {
 | 
			
		||||
				v.(*RedisConn).Close()
 | 
			
		||||
				return nil
 | 
			
		||||
			},
 | 
			
		||||
			Ping: func(v interface{}) error {
 | 
			
		||||
				_, err := v.(*RedisConn).Cli.Ping(context.Background()).Result()
 | 
			
		||||
				return err
 | 
			
		||||
			},
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		connPool[connId] = p
 | 
			
		||||
		return p, nil
 | 
			
		||||
	} else {
 | 
			
		||||
		return p, nil
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func PutRedisConn(c *RedisConn) {
 | 
			
		||||
	if nil == c {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if p, ok := connPool[getConnId(c.Info.Id, c.Info.Db)]; ok {
 | 
			
		||||
		p.Put(c)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 从缓存中获取redis连接信息, 若缓存中不存在则会使用回调函数获取redisInfo进行连接并缓存
 | 
			
		||||
func GetRedisConn(redisId uint64, db int, getRedisInfo func() (*RedisInfo, error)) (*RedisConn, error) {
 | 
			
		||||
	connId := getConnId(redisId, db)
 | 
			
		||||
 | 
			
		||||
	// connId不为空,则为需要缓存
 | 
			
		||||
	needCache := connId != ""
 | 
			
		||||
	if needCache {
 | 
			
		||||
		load, ok := connCache.Get(connId)
 | 
			
		||||
		if ok {
 | 
			
		||||
			return load.(*RedisConn), nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	mutex.Lock()
 | 
			
		||||
	defer mutex.Unlock()
 | 
			
		||||
 | 
			
		||||
	// 若缓存中不存在,则从回调函数中获取RedisInfo
 | 
			
		||||
	ri, err := getRedisInfo()
 | 
			
		||||
	p, err := getPool(redisId, db, getRedisInfo)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 连接数据库
 | 
			
		||||
	rc, err := ri.Conn()
 | 
			
		||||
	// 从连接池中获取一个可用的连接
 | 
			
		||||
	c, err := p.Get()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if needCache {
 | 
			
		||||
		connCache.Put(connId, rc)
 | 
			
		||||
	}
 | 
			
		||||
	return rc, nil
 | 
			
		||||
	// 用完后记的放回连接池
 | 
			
		||||
	return c.(*RedisConn), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 移除redis连接缓存并关闭redis连接
 | 
			
		||||
func CloseConn(id uint64, db int) {
 | 
			
		||||
	connCache.Delete(getConnId(id, db))
 | 
			
		||||
	delete(connPool, getConnId(id, db))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user