Skip to content

Commit

Permalink
fixed readiness bugs for object pool
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyofx committed Dec 7, 2020
1 parent 2e55a40 commit 516f69e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
87 changes: 44 additions & 43 deletions Internal/datasources/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,55 +45,18 @@ func NewMysqlDataSource(configuration Abstractions.IConfiguration) *MySqlDataSou
databaseConfig.Unmarshal(&datasourcesConfig)
log := XLog.GetXLogger("MysqlDataSource")

if datasourcesConfig.Pool != nil && (datasourcesConfig.Pool.InitCap == 0 || datasourcesConfig.Pool.MaxCap == 0 || datasourcesConfig.Pool.Idletimeout == 0) {
log.Error("database config is error initCap,maxCap,idleTimeout should be gt 0")
return nil
}

dsnPath := fmt.Sprintf("%s:%s@%s", datasourcesConfig.UserName, datasourcesConfig.Password, datasourcesConfig.Url)

// connMysql 建立连接
connMysql := func() (interface{}, error) {
conn, err := sql.Open("mysql", dsnPath)
return conn, err
}

// closeMysql 关闭连接
closeMysql := func(v interface{}) error {
return v.(*sql.DB).Close()
}

// pingMysql 检测连接连通性
pingMysql := func(v interface{}) error {
conn := v.(*sql.DB)
return conn.Ping()
}

//创建一个连接池: 初始化5,最大连接30
p, err := Pool.NewChannelPool(&Pool.Config{
InitialCap: datasourcesConfig.Pool.InitCap,
MaxCap: datasourcesConfig.Pool.MaxCap,
Factory: connMysql,
Close: closeMysql,
Ping: pingMysql,
//连接最大空闲时间,超过该时间的连接 将会关闭,可避免空闲时连接EOF,自动失效的问题
IdleTimeout: time.Duration(datasourcesConfig.Pool.Idletimeout) * time.Second,
})
if err != nil {
log.Error("register mysql conn [%s] error:%v", datasourcesConfig.Name, err)
return nil
}
p := createMysqlPool(datasourcesConfig, log)

dataSource := &MySqlDataSource{
name: datasourcesConfig.Name,
connectionString: dsnPath,
connectionString: "",
config: configuration,
connPool: make(map[string]Pool.Pool, 0),
log: log,
}

dataSource.insertPool(datasourcesConfig.Name, p)

if p != nil {
dataSource.insertPool(datasourcesConfig.Name, p)
}
return dataSource
}

Expand Down Expand Up @@ -137,8 +100,46 @@ func (datasource *MySqlDataSource) GetConnectionString() string {
return datasource.connectionString
}

func (datasource *MySqlDataSource) createPool() {
func createMysqlPool(datasourcesConfig dataSourceConfig, log XLog.ILogger) Pool.Pool {
if datasourcesConfig.Pool != nil && (datasourcesConfig.Pool.InitCap == 0 || datasourcesConfig.Pool.MaxCap == 0 || datasourcesConfig.Pool.Idletimeout == 0) {
log.Error("database config is error initCap,maxCap,idleTimeout should be gt 0")
return nil
}

dsnPath := fmt.Sprintf("%s:%s@%s", datasourcesConfig.UserName, datasourcesConfig.Password, datasourcesConfig.Url)

// connMysql 建立连接
connMysql := func() (interface{}, error) {
conn, err := sql.Open("mysql", dsnPath)
return conn, err
}

// closeMysql 关闭连接
closeMysql := func(v interface{}) error {
return v.(*sql.DB).Close()
}

// pingMysql 检测连接连通性
pingMysql := func(v interface{}) error {
conn := v.(*sql.DB)
return conn.Ping()
}

//创建一个连接池: 初始化5,最大连接30
p, err := Pool.NewChannelPool(&Pool.Config{
InitialCap: datasourcesConfig.Pool.InitCap,
MaxCap: datasourcesConfig.Pool.MaxCap,
Factory: connMysql,
Close: closeMysql,
Ping: pingMysql,
//连接最大空闲时间,超过该时间的连接 将会关闭,可避免空闲时连接EOF,自动失效的问题
IdleTimeout: time.Duration(datasourcesConfig.Pool.Idletimeout) * time.Second,
})
if err != nil {
log.Error("register mysql conn [%s] error:%v", datasourcesConfig.Name, err)
return nil
}
return p
}

// insertPool 将连接池插入map,支持多个不同mysql链接
Expand Down
5 changes: 3 additions & 2 deletions Internal/datasources/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ func NewRedis(configuration Abstractions.IConfiguration) *RedisDataSource {
connPool: make(map[string]Pool.Pool, 0),
log: log,
}

dataSource.insertPool(redisdatasourcesConfig.Name, p)
if p != nil {
dataSource.insertPool(redisdatasourcesConfig.Name, p)
}

return dataSource
}
Expand Down

0 comments on commit 516f69e

Please sign in to comment.