-
Notifications
You must be signed in to change notification settings - Fork 124
/
config.go
59 lines (52 loc) · 1.12 KB
/
config.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
package gorose
import (
"database/sql"
"time"
)
type Config struct {
Driver string
DSN string
Prefix string
Weight int8 // 权重越高,选中的概率越大
MaxIdleConns int
MaxOpenConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
}
type ConfigCluster struct {
WriteConf []Config
ReadConf []Config
}
//type ConfigMulti map[string]ConfigCluster
func (c ConfigCluster) init() (master []*sql.DB, slave []*sql.DB) {
if len(c.WriteConf) > 0 {
for _, v := range c.WriteConf {
master = append(master, c.initDB(&v))
}
}
if len(c.ReadConf) > 0 {
for _, v := range c.ReadConf {
slave = append(master, c.initDB(&v))
}
}
return
}
func (c ConfigCluster) initDB(v *Config) *sql.DB {
db, err := sql.Open(v.Driver, v.DSN)
if err != nil {
panic(err.Error())
}
if v.MaxIdleConns > 0 {
db.SetMaxIdleConns(v.MaxIdleConns)
}
if v.MaxOpenConns > 0 {
db.SetMaxOpenConns(v.MaxOpenConns)
}
if v.ConnMaxLifetime > 0 {
db.SetConnMaxLifetime(v.ConnMaxLifetime)
}
if v.ConnMaxIdleTime > 0 {
db.SetConnMaxIdleTime(v.ConnMaxIdleTime)
}
return db
}