|
@@ -59,13 +59,17 @@ func (s *Component) Init() {
|
|
item := dbGroup.GetConfig(i)
|
|
item := dbGroup.GetConfig(i)
|
|
|
|
|
|
var (
|
|
var (
|
|
- enable = item.GetBool("enable", true)
|
|
|
|
- id = item.GetString("db_id")
|
|
|
|
- dbName = item.GetString("db_name")
|
|
|
|
- uri = item.GetString("uri")
|
|
|
|
- timeout = time.Duration(item.GetInt64("timeout", 3)) * time.Second
|
|
|
|
- tlsEnable = item.GetInt("tls")
|
|
|
|
- maxClient = item.GetInt("maxClient")
|
|
|
|
|
|
+ enable = item.GetBool("enable", true)
|
|
|
|
+ id = item.GetString("db_id")
|
|
|
|
+ dbName = item.GetString("db_name")
|
|
|
|
+ uri = item.GetString("uri")
|
|
|
|
+ timeout = time.Duration(item.GetInt64("timeout", 3)) * time.Second
|
|
|
|
+ tlsEnable = item.GetInt("tls")
|
|
|
|
+ maxPoolSize = item.GetInt("maxPoolSize")
|
|
|
|
+ minPoolSize = item.GetInt("minPoolSize")
|
|
|
|
+ maxConnIdleTime = item.GetInt("maxConnIdleTime")
|
|
|
|
+ connectTimeout = item.GetInt("connectTimeout")
|
|
|
|
+ socketTimeout = item.GetInt("socketTimeout")
|
|
)
|
|
)
|
|
|
|
|
|
for _, key := range mongoIdList.Keys() {
|
|
for _, key := range mongoIdList.Keys() {
|
|
@@ -78,7 +82,7 @@ func (s *Component) Init() {
|
|
panic(fmt.Sprintf("[dbName = %s] is disabled!", dbName))
|
|
panic(fmt.Sprintf("[dbName = %s] is disabled!", dbName))
|
|
}
|
|
}
|
|
|
|
|
|
- db, err := CreateDatabase(uri, dbName, tlsEnable, uint64(maxClient), timeout)
|
|
|
|
|
|
+ db, err := CreateDatabase(uri, dbName, tlsEnable, uint64(maxPoolSize), uint64(minPoolSize), maxConnIdleTime, connectTimeout, socketTimeout, timeout)
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(fmt.Sprintf("[dbName = %s] create mongodb fail. error = %s", dbName, err))
|
|
panic(fmt.Sprintf("[dbName = %s] create mongodb fail. error = %s", dbName, err))
|
|
}
|
|
}
|
|
@@ -90,7 +94,7 @@ func (s *Component) Init() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func CreateDatabase(uri, dbName string, tlsEnable int, maxClient uint64, timeout ...time.Duration) (*mongo.Database, error) {
|
|
|
|
|
|
+func CreateDatabase(uri, dbName string, tlsEnable int, maxPoolSize uint64, minPoolSize uint64, maxConnIdleTime int, connectTimeout, socketTimeout int, timeout ...time.Duration) (*mongo.Database, error) {
|
|
tt := 5 * time.Second
|
|
tt := 5 * time.Second
|
|
|
|
|
|
if len(timeout) > 0 && timeout[0].Seconds() > 3 {
|
|
if len(timeout) > 0 && timeout[0].Seconds() > 3 {
|
|
@@ -104,23 +108,19 @@ func CreateDatabase(uri, dbName string, tlsEnable int, maxClient uint64, timeout
|
|
PreferServerCipherSuites: true,
|
|
PreferServerCipherSuites: true,
|
|
}
|
|
}
|
|
|
|
|
|
- o = options.Client().ApplyURI(uri).SetMaxPoolSize(500). //最大连接
|
|
|
|
- SetMinPoolSize(20). //最小连接
|
|
|
|
- SetMaxConnIdleTime(3 * time.Minute). //连接空闲时间
|
|
|
|
- SetMaxConnIdleTime(5 * time.Minute). //连接生命周期
|
|
|
|
- SetConnectTimeout(10 * time.Second). //连接超时时间
|
|
|
|
- SetSocketTimeout(10 * time.Second).SetTLSConfig(tlsConfig) //套接字超时时间
|
|
|
|
|
|
+ o = options.Client().ApplyURI(uri).SetMaxPoolSize(maxPoolSize). //最大连接
|
|
|
|
+ SetMinPoolSize(minPoolSize). //最小连接
|
|
|
|
+ SetMaxConnIdleTime(time.Duration(maxConnIdleTime) * time.Second). //连接空闲时间
|
|
|
|
+ SetConnectTimeout(time.Duration(connectTimeout) * time.Second). //连接超时时间
|
|
|
|
+ SetSocketTimeout(time.Duration(socketTimeout) * time.Second).SetTLSConfig(tlsConfig) //套接字超时时间
|
|
} else {
|
|
} else {
|
|
- o = options.Client().ApplyURI(uri).SetMaxPoolSize(500). //最大连接
|
|
|
|
- SetMinPoolSize(20). //最小连接
|
|
|
|
- SetMaxConnIdleTime(3 * time.Minute). //连接空闲时间
|
|
|
|
- SetMaxConnIdleTime(5 * time.Minute). //连接生命周期
|
|
|
|
- SetConnectTimeout(10 * time.Second). //连接超时时间
|
|
|
|
- SetSocketTimeout(10 * time.Second)
|
|
|
|
|
|
+ o = options.Client().ApplyURI(uri).SetMaxPoolSize(maxPoolSize). //最大连接
|
|
|
|
+ SetMinPoolSize(minPoolSize). //最小连接
|
|
|
|
+ SetMaxConnIdleTime(time.Duration(maxConnIdleTime) * time.Second). //连接空闲时间
|
|
|
|
+ SetConnectTimeout(time.Duration(connectTimeout) * time.Second). //连接超时时间
|
|
|
|
+ SetSocketTimeout(time.Duration(socketTimeout) * time.Second) //套接字超时时间
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
if err := o.Validate(); err != nil {
|
|
if err := o.Validate(); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|