优化client
This commit is contained in:
parent
988ccb1a39
commit
9f1337a668
92
client.go
92
client.go
@ -9,6 +9,7 @@ package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/logger/wrapper"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -22,6 +23,97 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
// Client mysql客户端
|
||||
Client *client
|
||||
)
|
||||
|
||||
func init() {
|
||||
Client = &client{
|
||||
lock: &sync.RWMutex{},
|
||||
clientTable: make(map[string]*DBClient),
|
||||
}
|
||||
}
|
||||
|
||||
type client struct {
|
||||
lock *sync.RWMutex
|
||||
clientTable map[string]*DBClient
|
||||
}
|
||||
|
||||
// AddWithConfigFile 使用文件生成新的客户端,文件名去掉后缀作为flag
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:19 2022/6/5
|
||||
func (c *client) AddWithConfigFile(cfgFile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BatchAddWithConfigDir 自动读取目录下配置文件, 生成客户端
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:19 2022/6/5
|
||||
func (c *client) BatchAddWithConfigDir(cfgDir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDBClient 获取db client
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:32 2022/6/5
|
||||
func (c *client) GetDBClient(ctx *gin.Context, dbFlag string) (*DBClient, error) {
|
||||
c.lock.RLock()
|
||||
defer c.lock.RUnlock()
|
||||
var (
|
||||
exist bool
|
||||
dbClient *DBClient
|
||||
)
|
||||
if dbClient, exist = c.clientTable[dbFlag]; !exist {
|
||||
return nil, fmt.Errorf("%s 标识的数据库实例不存在! ", dbFlag)
|
||||
}
|
||||
return dbClient, nil
|
||||
}
|
||||
|
||||
// GetMasterClient 获取主库客户端
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:36 2022/6/5
|
||||
func (c *client) GetMasterClient(ctx *gin.Context, dbFlag string) (*gorm.DB, error) {
|
||||
var (
|
||||
err error
|
||||
dbClient *DBClient
|
||||
)
|
||||
if dbClient, err = c.GetDBClient(ctx, dbFlag); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session := dbClient.master.Session(&gorm.Session{})
|
||||
session.Logger = dbClient.getLogger(ctx, session, dbFlag+"-master")
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// GetSlaveClient 获取从库客户端
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 19:37 2022/6/5
|
||||
func (c *client) GetSlaveClient(ctx *gin.Context, dbFlag string) (*gorm.DB, error) {
|
||||
var (
|
||||
err error
|
||||
dbClient *DBClient
|
||||
)
|
||||
if dbClient, err = c.GetDBClient(ctx, dbFlag); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
session := dbClient.slave.Session(&gorm.Session{})
|
||||
session.Logger = dbClient.getLogger(ctx, session, dbFlag+"-slave")
|
||||
return session, nil
|
||||
}
|
||||
|
||||
// BatchNewDBClient 批量设置数据库实例, 读取指定目录下的yaml文件, 文件名作为标识, 内容是数据库具体配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
Loading…
Reference in New Issue
Block a user