|
@@ -2,6 +2,7 @@ package mhayaMongo
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "crypto/tls"
|
|
|
"fmt"
|
|
|
"time"
|
|
|
|
|
@@ -58,11 +59,13 @@ func (s *Component) Init() {
|
|
|
item := dbGroup.GetConfig(i)
|
|
|
|
|
|
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
|
|
|
+ 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")
|
|
|
)
|
|
|
|
|
|
for _, key := range mongoIdList.Keys() {
|
|
@@ -75,7 +78,7 @@ func (s *Component) Init() {
|
|
|
panic(fmt.Sprintf("[dbName = %s] is disabled!", dbName))
|
|
|
}
|
|
|
|
|
|
- db, err := CreateDatabase(uri, dbName, timeout)
|
|
|
+ db, err := CreateDatabase(uri, dbName, tlsEnable, uint64(maxClient), timeout)
|
|
|
if err != nil {
|
|
|
panic(fmt.Sprintf("[dbName = %s] create mongodb fail. error = %s", dbName, err))
|
|
|
}
|
|
@@ -87,21 +90,24 @@ func (s *Component) Init() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func CreateDatabase(uri, dbName string, timeout ...time.Duration) (*mongo.Database, error) {
|
|
|
+func CreateDatabase(uri, dbName string, tlsEnable int, maxClient uint64, timeout ...time.Duration) (*mongo.Database, error) {
|
|
|
tt := 5 * time.Second
|
|
|
|
|
|
if len(timeout) > 0 && timeout[0].Seconds() > 3 {
|
|
|
tt = timeout[0]
|
|
|
}
|
|
|
|
|
|
- // todo tls开启
|
|
|
- //tlsConfig := &tls.Config{
|
|
|
- // MinVersion: tls.VersionTLS12,
|
|
|
- // PreferServerCipherSuites: true,
|
|
|
- //}
|
|
|
- //o := options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig)
|
|
|
+ var o *options.ClientOptions
|
|
|
+ if tlsEnable == 1 {
|
|
|
+ tlsConfig := &tls.Config{
|
|
|
+ MinVersion: tls.VersionTLS12,
|
|
|
+ PreferServerCipherSuites: true,
|
|
|
+ }
|
|
|
+ o = options.Client().ApplyURI(uri).SetTLSConfig(tlsConfig).SetMaxPoolSize(maxClient)
|
|
|
+ } else {
|
|
|
+ o = options.Client().ApplyURI(uri).SetMaxPoolSize(maxClient)
|
|
|
+ }
|
|
|
|
|
|
- o := options.Client().ApplyURI(uri).SetMaxPoolSize(500)
|
|
|
if err := o.Validate(); err != nil {
|
|
|
return nil, err
|
|
|
}
|