优化回收连接方法
This commit is contained in:
parent
082a0f69f7
commit
517571fd5d
|
@ -123,9 +123,6 @@ func directOutput(writer http.ResponseWriter, code int, content []byte) {
|
||||||
func httpAfterDo(c *Context) error {
|
func httpAfterDo(c *Context) error {
|
||||||
c.session.SessionRelease(c.response)
|
c.session.SessionRelease(c.response)
|
||||||
|
|
||||||
// 回收数据库连接
|
|
||||||
go database.PullChanDB()
|
|
||||||
|
|
||||||
// 最后输出body
|
// 最后输出body
|
||||||
c.response.Write(c.responseBody.Bytes())
|
c.response.Write(c.responseBody.Bytes())
|
||||||
c.responseBody.Reset()
|
c.responseBody.Reset()
|
||||||
|
|
|
@ -10,7 +10,7 @@ type DataBase interface {
|
||||||
RegisterAll()
|
RegisterAll()
|
||||||
Register(string)
|
Register(string)
|
||||||
insertPool(string, pool.Pool)
|
insertPool(string, pool.Pool)
|
||||||
getDB(string) (interface{}, error)
|
getDB(string) (interface{},func(), error)
|
||||||
putDB(string, interface{}) error
|
putDB(string, interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,7 +100,7 @@ func (my *MysqlDB) Register(name string) {
|
||||||
my.insertPool(name, p)
|
my.insertPool(name, p)
|
||||||
}
|
}
|
||||||
|
|
||||||
// insertPool 将连接池插入map
|
// insertPool 将连接池插入map,支持多个不同mysql链接
|
||||||
func (my *MysqlDB) insertPool(name string, p pool.Pool) {
|
func (my *MysqlDB) insertPool(name string, p pool.Pool) {
|
||||||
if my.connPool == nil {
|
if my.connPool == nil {
|
||||||
my.connPool = make(map[string]pool.Pool, 0)
|
my.connPool = make(map[string]pool.Pool, 0)
|
||||||
|
@ -112,20 +112,23 @@ func (my *MysqlDB) insertPool(name string, p pool.Pool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getDB 从连接池获取一个连接
|
// 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 {
|
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()
|
conn, err = my.connPool[name].Get()
|
||||||
if err != nil {
|
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() {
|
put = func() {
|
||||||
my.connList.RPush(dbChan{"mysql", name, conn})
|
my.connPool[name].Put(conn)
|
||||||
}()
|
}
|
||||||
|
|
||||||
return conn, nil
|
return conn, put,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// putDB 将连接放回连接池
|
// putDB 将连接放回连接池
|
||||||
|
@ -139,15 +142,16 @@ func (my *MysqlDB) putDB(name string, db interface{}) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMysql 获取一个mysql db连接
|
// 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 {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil,put, err
|
||||||
}
|
}
|
||||||
db = conn.(*gorm.DB)
|
db = conn.(*gorm.DB)
|
||||||
return db, nil
|
return db, put,nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,20 +126,20 @@ func (re *RedisDB) insertPool(name string, p pool.Pool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getDB 从连接池获取一个连接
|
// 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 {
|
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()
|
conn, err = re.connPool[name].Get()
|
||||||
if err != nil {
|
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() {
|
return conn, put,nil
|
||||||
re.connList.RPush(dbChan{"redis", name, conn})
|
|
||||||
}()
|
|
||||||
|
|
||||||
return conn, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// putDB 将连接放回连接池
|
// putDB 将连接放回连接池
|
||||||
|
@ -153,14 +153,15 @@ func (re *RedisDB) putDB(name string, db interface{}) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRedis 获取一个mysql db连接
|
// 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 {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil,put, err
|
||||||
}
|
}
|
||||||
db = conn.(redis.Conn)
|
db = conn.(redis.Conn)
|
||||||
return db, nil
|
return db, put,nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,13 +24,13 @@
|
||||||
debug = true
|
debug = true
|
||||||
[database.mysql]
|
[database.mysql]
|
||||||
[database.mysql.default]
|
[database.mysql.default]
|
||||||
addr = "192.168.137.100:3306"
|
addr = "127.0.0.1:3306"
|
||||||
username = "zhuqi"
|
username = "zhuqi"
|
||||||
password = "123456"
|
password = "123456"
|
||||||
dbname = "weixin"
|
dbname = "douya"
|
||||||
[database.redis]
|
[database.redis]
|
||||||
[database.redis.default]
|
[database.redis.default]
|
||||||
addr = "192.168.137.100:6379"
|
addr = "192.168.1.108:6379"
|
||||||
password = ""
|
password = "123456"
|
||||||
dbnum = 5
|
dbnum = 5
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitee.com/zhucheer/orange/app"
|
"gitee.com/zhucheer/orange/app"
|
||||||
"gitee.com/zhucheer/orange/captcha"
|
"gitee.com/zhucheer/orange/captcha"
|
||||||
|
"gitee.com/zhucheer/orange/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Welcome(c *app.Context) error {
|
func Welcome(c *app.Context) error {
|
||||||
|
@ -46,3 +47,40 @@ func VerifyImg(c *app.Context) error {
|
||||||
"result": "upload success",
|
"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",
|
||||||
|
})
|
||||||
|
}
|
|
@ -20,6 +20,9 @@ func (s *Route) ServeMux() {
|
||||||
|
|
||||||
commonGp.GET("/captcha", controller.Captcha)
|
commonGp.GET("/captcha", controller.Captcha)
|
||||||
commonGp.GET("/verifyimg", controller.VerifyImg)
|
commonGp.GET("/verifyimg", controller.VerifyImg)
|
||||||
|
|
||||||
|
commonGp.GET("/selectMysql", controller.SelectMySql)
|
||||||
|
commonGp.GET("/selectRedis", controller.SelectRedis)
|
||||||
}
|
}
|
||||||
|
|
||||||
authGp := app.GroupRouter("/auth", middleware.NewAuth())
|
authGp := app.GroupRouter("/auth", middleware.NewAuth())
|
||||||
|
|
Loading…
Reference in New Issue