-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcomponent.go
146 lines (118 loc) · 3.27 KB
/
component.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cherryMongo
import (
"context"
"fmt"
"time"
cfacade "github.com/cherry-game/cherry/facade"
clog "github.com/cherry-game/cherry/logger"
cprofile "github.com/cherry-game/cherry/profile"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
const (
Name = "mongo_component"
)
type (
Component struct {
cfacade.Component
dbMap map[string]map[string]*mongo.Database
}
// HashDb hash by group id
HashDb func(dbMaps map[string]*mongo.Database) string
)
func NewComponent() *Component {
return &Component{
dbMap: make(map[string]map[string]*mongo.Database),
}
}
func (*Component) Name() string {
return Name
}
func (s *Component) Init() {
// load only the database contained in the `db_id_list`
mongoIdList := s.App().Settings().Get("mongo_id_list")
if mongoIdList.LastError() != nil || mongoIdList.Size() < 1 {
clog.Warnf("[nodeId = %s] `mongo_id_list` property not exists.", s.App().NodeId())
return
}
mongoConfig := cprofile.GetConfig("mongo")
if mongoConfig.LastError() != nil {
panic("`mongo` property not exists in profile file.")
}
for _, groupId := range mongoConfig.Keys() {
s.dbMap[groupId] = make(map[string]*mongo.Database)
dbGroup := mongoConfig.GetConfig(groupId)
for i := 0; i < dbGroup.Size(); i++ {
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
)
for j := 0; j < mongoIdList.Size(); j++ {
dbId := mongoIdList.Get(j).ToString()
if id != dbId {
continue
}
if !enable {
panic(fmt.Sprintf("[dbName = %s] is disabled!", dbName))
}
db, err := CreateDatabase(uri, dbName, timeout)
if err != nil {
panic(fmt.Sprintf("[dbName = %s] create mongodb fail. error = %s", dbName, err))
}
s.dbMap[groupId][id] = db
clog.Infof("[dbGroup =%s, dbName = %s] is connected.", groupId, id)
}
}
}
}
func CreateDatabase(uri, dbName string, timeout ...time.Duration) (*mongo.Database, error) {
tt := 3 * time.Second
if len(timeout) > 0 && timeout[0].Seconds() > 3 {
tt = timeout[0]
}
o := options.Client().ApplyURI(uri)
if err := o.Validate(); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), tt)
defer cancel()
client, err := mongo.Connect(ctx, o)
if err != nil {
return nil, err
}
err = client.Ping(context.Background(), readpref.Primary())
if err != nil {
return nil, err
}
clog.Infof("ping db [uri = %s] is ok", uri)
return client.Database(dbName), nil
}
func (s *Component) GetDb(id string) *mongo.Database {
for _, group := range s.dbMap {
for k, v := range group {
if k == id {
return v
}
}
}
return nil
}
func (s *Component) GetHashDb(groupId string, hashFn HashDb) (*mongo.Database, bool) {
dbGroup, found := s.GetDbMap(groupId)
if !found {
clog.Warnf("groupId = %s not found.", groupId)
return nil, false
}
dbId := hashFn(dbGroup)
db, found := dbGroup[dbId]
return db, found
}
func (s *Component) GetDbMap(groupId string) (map[string]*mongo.Database, bool) {
dbGroup, found := s.dbMap[groupId]
return dbGroup, found
}