From 516f69e778021d8529310e739fec773ff64dea97 Mon Sep 17 00:00:00 2001 From: yoyofx Date: Tue, 8 Dec 2020 00:52:56 +0800 Subject: [PATCH] fixed readiness bugs for object pool --- Internal/datasources/mysql.go | 87 ++++++++++++++++++----------------- Internal/datasources/redis.go | 5 +- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/Internal/datasources/mysql.go b/Internal/datasources/mysql.go index ab8458b9..bbba6408 100644 --- a/Internal/datasources/mysql.go +++ b/Internal/datasources/mysql.go @@ -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 } @@ -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链接 diff --git a/Internal/datasources/redis.go b/Internal/datasources/redis.go index 7762e56f..e7c07a1e 100644 --- a/Internal/datasources/redis.go +++ b/Internal/datasources/redis.go @@ -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 }