/
gorm.go
236 lines (226 loc) · 6.51 KB
/
gorm.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package gorm
import (
"fmt"
"github.com/go-ecosystem/mysql"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/gins"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/text/gregex"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/grand"
"github.com/gogf/gf/util/gutil"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
const (
configNodeNameDatabase = "database"
)
func Instance(name ...string) *gorm.DB {
group := DefaultGroupName
if len(name) > 0 && name[0] != "" {
group = name[0]
}
var (
configMap map[string]interface{}
configNodeKey string
)
// It firstly searches the configuration of the instance name.
configNodeKey, _ = gutil.MapPossibleItemByKey(
gins.Config().GetMap("."),
configNodeNameDatabase,
)
if configNodeKey == "" {
configNodeKey = configNodeNameDatabase
}
configMap = gins.Config().GetMap(configNodeKey)
if len(configMap) == 0 && !IsConfigured() {
panic(fmt.Sprintf(`database init failed: "%s" node not found, is config file or configuration missing?`, configNodeNameDatabase))
}
if len(configMap) == 0 {
configMap = make(map[string]interface{})
}
// Parse <m> as map-slice and adds it to gdb's global configurations.
for g, groupConfig := range configMap {
cg := ConfigGroup{}
switch value := groupConfig.(type) {
case []interface{}:
for _, v := range value {
if node := parseDBConfigNode(v); node != nil {
cg = append(cg, *node)
}
}
case map[string]interface{}:
if node := parseDBConfigNode(value); node != nil {
cg = append(cg, *node)
}
}
if len(cg) > 0 {
if GetConfig(group) == nil {
glog.Printf("add configuration for group: %s, %#v", g, cg)
SetConfigGroup(g, cg)
} else {
glog.Printf("ignore configuration as it already exists for group: %s, %#v", g, cg)
glog.Printf("%s, %#v", g, cg)
}
}
}
// Parse <m> as a single node configuration,
// which is the default group configuration.
if node := parseDBConfigNode(configMap); node != nil {
cg := ConfigGroup{}
if node.LinkInfo != "" || node.Host != "" {
cg = append(cg, *node)
}
if len(cg) > 0 {
if GetConfig(group) == nil {
glog.Printf("add configuration for group: %s, %#v", DefaultGroupName, cg)
SetConfigGroup(DefaultGroupName, cg)
} else {
glog.Printf("ignore configuration as it already exists for group: %s, %#v", DefaultGroupName, cg)
glog.Printf("%s, %#v", DefaultGroupName, cg)
}
}
}
gormDb := mysql.GetDBByKey(group)
if gormDb != nil {
return gormDb
}
// Create a new ORM object with given configurations.
if db, err := New(name...); err == nil {
return db
}
return nil
}
// New creates and returns an ORM object with global configurations.
// The parameter <name> specifies the configuration group name,
// which is DefaultGroupName in default.
func New(group ...string) (db *gorm.DB, err error) {
groupName := configs.group
if len(group) > 0 && group[0] != "" {
groupName = group[0]
}
configs.RLock()
defer configs.RUnlock()
if len(configs.config) < 1 {
return nil, gerror.New("empty database configuration")
}
if _, ok := configs.config[groupName]; ok {
if node, err := getConfigNodeByGroup(groupName, true); err == nil {
cnf := mysql.NewConfig(
node.User,
node.Pass,
node.Host,
node.Port,
node.Name,
node.Charset,
logger.Error,
mysql.WithMaxOpenConns(node.MaxOpenConnCount),
mysql.WithMaxIdleConns(node.MaxIdleConnCount))
// register default db
mysql.RegisterByKey(cnf, groupName)
db := mysql.GetDBByKey(groupName)
if db != nil {
return db, nil
} else {
return nil, gerror.Newf(`database node "%s" is not found`, groupName)
}
} else {
return nil, err
}
} else {
return nil, gerror.New(fmt.Sprintf(`database configuration node "%s" is not found`, groupName))
}
}
// getConfigNodeByGroup calculates and returns a configuration node of given group. It
// calculates the value internally using weight algorithm for load balance.
//
// The parameter <master> specifies whether retrieving a master node, or else a slave node
// if master-slave configured.
func getConfigNodeByGroup(group string, master bool) (*ConfigNode, error) {
if list, ok := configs.config[group]; ok {
// Separates master and slave configuration nodes array.
masterList := make(ConfigGroup, 0)
slaveList := make(ConfigGroup, 0)
for i := 0; i < len(list); i++ {
if list[i].Role == "slave" {
slaveList = append(slaveList, list[i])
} else {
masterList = append(masterList, list[i])
}
}
if len(masterList) < 1 {
return nil, gerror.New("at least one master node configuration's need to make sense")
}
if len(slaveList) < 1 {
slaveList = masterList
}
if master {
return getConfigNodeByWeight(masterList), nil
} else {
return getConfigNodeByWeight(slaveList), nil
}
} else {
return nil, gerror.New(fmt.Sprintf("empty database configuration for item name '%s'", group))
}
}
// getConfigNodeByWeight calculates the configuration weights and randomly returns a node.
//
// Calculation algorithm brief:
// 1. If we have 2 nodes, and their weights are both 1, then the weight range is [0, 199];
// 2. Node1 weight range is [0, 99], and node2 weight range is [100, 199], ratio is 1:1;
// 3. If the random number is 99, it then chooses and returns node1;
func getConfigNodeByWeight(cg ConfigGroup) *ConfigNode {
if len(cg) < 2 {
return &cg[0]
}
var total int
for i := 0; i < len(cg); i++ {
total += cg[i].Weight * 100
}
// If total is 0 means all of the nodes have no weight attribute configured.
// It then defaults each node's weight attribute to 1.
if total == 0 {
for i := 0; i < len(cg); i++ {
cg[i].Weight = 1
total += cg[i].Weight * 100
}
}
// Exclude the right border value.
r := grand.N(0, total-1)
min := 0
max := 0
for i := 0; i < len(cg); i++ {
max = min + cg[i].Weight*100
//fmt.Printf("r: %d, min: %d, max: %d\n", r, min, max)
if r >= min && r < max {
return &cg[i]
} else {
min = max
}
}
return nil
}
func parseDBConfigNode(value interface{}) *ConfigNode {
nodeMap, ok := value.(map[string]interface{})
if !ok {
return nil
}
node := &ConfigNode{}
err := gconv.Struct(nodeMap, node)
if err != nil {
panic(err)
}
if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil {
node.LinkInfo = gconv.String(v)
}
// Parse link syntax.
if node.LinkInfo != "" && node.Type == "" {
match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.LinkInfo)
if len(match) == 3 {
node.Type = gstr.Trim(match[1])
node.LinkInfo = gstr.Trim(match[2])
}
}
return node
}