优化回收连接方法

This commit is contained in:
zhucheer 2019-11-16 19:14:06 +08:00
parent 082a0f69f7
commit 517571fd5d
7 changed files with 77 additions and 34 deletions

View File

@ -123,9 +123,6 @@ func directOutput(writer http.ResponseWriter, code int, content []byte) {
func httpAfterDo(c *Context) error {
c.session.SessionRelease(c.response)
// 回收数据库连接
go database.PullChanDB()
// 最后输出body
c.response.Write(c.responseBody.Bytes())
c.responseBody.Reset()

View File

@ -10,7 +10,7 @@ type DataBase interface {
RegisterAll()
Register(string)
insertPool(string, pool.Pool)
getDB(string) (interface{}, error)
getDB(string) (interface{},func(), error)
putDB(string, interface{}) error
}

View File

@ -100,7 +100,7 @@ func (my *MysqlDB) Register(name string) {
my.insertPool(name, p)
}
// insertPool 将连接池插入map
// insertPool 将连接池插入map,支持多个不同mysql链接
func (my *MysqlDB) insertPool(name string, p pool.Pool) {
if my.connPool == nil {
my.connPool = make(map[string]pool.Pool, 0)
@ -112,20 +112,23 @@ func (my *MysqlDB) insertPool(name string, p pool.Pool) {
}
// getDB 从连接池获取一个连接
func (my *MysqlDB) getDB(name string) (conn interface{}, err error) {
func (my *MysqlDB) getDB(name string) (conn interface{}, put func(), err error) {
put = func() {}
if _, ok := my.connPool[name]; !ok {
return nil, errors.New("no mysql connect")
return nil,put, errors.New("no mysql connect")
}
conn, err = my.connPool[name].Get()
if err != nil {
return nil, errors.New(fmt.Sprintf("mysql get connect err:%v", err))
return nil,put, errors.New(fmt.Sprintf("mysql get connect err:%v", err))
}
go func() {
my.connList.RPush(dbChan{"mysql", name, conn})
}()
put = func() {
my.connPool[name].Put(conn)
}
return conn, nil
return conn, put,nil
}
// putDB 将连接放回连接池
@ -139,15 +142,16 @@ func (my *MysqlDB) putDB(name string, db interface{}) (err error) {
}
// GetMysql 获取一个mysql db连接
func GetMysql(name string) (db *gorm.DB, err error) {
func GetMysql(name string) (db *gorm.DB, put func(), err error) {
put = func() {}
if mysqlConn == nil {
return nil, errors.New("db connect is nil")
return nil,put, errors.New("db connect is nil")
}
conn, err := mysqlConn.getDB(name)
conn,put, err := mysqlConn.getDB(name)
if err != nil {
return nil, err
return nil,put, err
}
db = conn.(*gorm.DB)
return db, nil
return db, put,nil
}

View File

@ -126,20 +126,20 @@ func (re *RedisDB) insertPool(name string, p pool.Pool) {
}
// getDB 从连接池获取一个连接
func (re *RedisDB) getDB(name string) (conn interface{}, err error) {
func (re *RedisDB) getDB(name string) (conn interface{},put func(), err error) {
put = func() {}
if _, ok := re.connPool[name]; !ok {
return nil, errors.New("no redis connect")
return nil,put, errors.New("no redis connect")
}
conn, err = re.connPool[name].Get()
if err != nil {
return nil, errors.New(fmt.Sprintf("redis get connect err:%v", err))
return nil,put, errors.New(fmt.Sprintf("redis get connect err:%v", err))
}
put = func() {
re.connPool[name].Put(conn)
}
go func() {
re.connList.RPush(dbChan{"redis", name, conn})
}()
return conn, nil
return conn, put,nil
}
// putDB 将连接放回连接池
@ -153,14 +153,15 @@ func (re *RedisDB) putDB(name string, db interface{}) (err error) {
}
// GetRedis 获取一个mysql db连接
func GetRedis(name string) (db redis.Conn, err error) {
func GetRedis(name string) (db redis.Conn,put func(), err error) {
put = func() {}
if redisConn == nil {
return nil, errors.New("db connect is nil")
return nil,put, errors.New("db connect is nil")
}
conn, err := redisConn.getDB(name)
conn,put, err := redisConn.getDB(name)
if err != nil {
return nil, err
return nil,put, err
}
db = conn.(redis.Conn)
return db, nil
return db, put,nil
}

View File

@ -24,13 +24,13 @@
debug = true
[database.mysql]
[database.mysql.default]
addr = "192.168.137.100:3306"
addr = "127.0.0.1:3306"
username = "zhuqi"
password = "123456"
dbname = "weixin"
dbname = "douya"
[database.redis]
[database.redis.default]
addr = "192.168.137.100:6379"
password = ""
addr = "192.168.1.108:6379"
password = "123456"
dbnum = 5

View File

@ -4,6 +4,7 @@ import (
"fmt"
"gitee.com/zhucheer/orange/app"
"gitee.com/zhucheer/orange/captcha"
"gitee.com/zhucheer/orange/database"
)
func Welcome(c *app.Context) error {
@ -46,3 +47,40 @@ func VerifyImg(c *app.Context) error {
"result": "upload success",
})
}
type WxUsers struct {
ID uint `gorm:"primary_key"`
Name string `gorm:"nick_name"`
} // 默认表名是`users`
func SelectMySql(c *app.Context) error {
db,put, err := database.GetMysql("default")
defer put()
if err != nil {
fmt.Println("db connect error", err)
return nil
}
info := &WxUsers{}
db.Table("wx_users").Where("id = ?", "1").First(&info)
fmt.Println("db find====")
return c.ToJson(map[string]interface{}{
"result": "select success",
})
}
func SelectRedis(c *app.Context) error {
db,put, err := database.GetRedis("default")
if err != nil {
fmt.Println("db connect error", err)
return nil
}
defer put()
db.Do("SET", "tttx", "rrrrr")
return c.ToJson(map[string]interface{}{
"result": "select success",
})
}

View File

@ -20,6 +20,9 @@ func (s *Route) ServeMux() {
commonGp.GET("/captcha", controller.Captcha)
commonGp.GET("/verifyimg", controller.VerifyImg)
commonGp.GET("/selectMysql", controller.SelectMySql)
commonGp.GET("/selectRedis", controller.SelectRedis)
}
authGp := app.GroupRouter("/auth", middleware.NewAuth())