-
Notifications
You must be signed in to change notification settings - Fork 701
/
mongo.go
46 lines (36 loc) · 1.09 KB
/
mongo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package mongox
import (
"context"
"time"
"github.com/douyu/jupiter/pkg/xlog"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
/*
DB: 返回name定义的mysql DB handler
name: 唯一名称
opts: Open Option, 用于覆盖配置文件中定义的配置
example: DB := DB("StdConfig", orm.RawConfig("jupiter.mongodb.StdConfig"))
*/
func newSession(config Config) *mongo.Client {
// check config param
isConfigErr(config)
mps := uint64(config.PoolLimit)
clientOpts := options.Client()
clientOpts.MaxPoolSize = &mps
clientOpts.SocketTimeout = &config.SocketTimeout
client, err := mongo.Connect(context.Background(), clientOpts.ApplyURI(config.DSN))
if err != nil {
_logger.Panic("dial mongo", xlog.FieldAddr(config.DSN), xlog.Any("error", err))
}
_instances.Store(config.Name, client)
return client
}
func isConfigErr(config Config) {
if config.SocketTimeout == time.Duration(0) {
_logger.Panic("invalid config", xlog.FieldExtMessage("socketTimeout"))
}
if config.PoolLimit == 0 {
_logger.Panic("invalid config", xlog.FieldExtMessage("poolLimit"))
}
}