/
config.go
236 lines (194 loc) · 4.5 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
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 gosql
import (
"fmt"
"net/url"
"time"
)
func ProvideConfig(config *YamlConfig) (*Config, error) {
var ss []ShardingTableConfig
for _, c := range config.Cluster.Sharding.Table {
if 0 != c.Total%config.Cluster.Sharding.DbTotal {
return nil, fmt.Errorf("table total must be a multiple of db total")
}
ss = append(ss, ShardingTableConfig{
Name: c.Name,
Total: c.Total,
})
}
var scs []ShardConfig
for _, shard := range config.Shards {
sc := ShardConfig{
MasterConfig: DbConfig{
Driver: config.Cluster.Driver,
Dbname: config.Cluster.Dbname,
Host: shard.Master.Host,
User: shard.Master.User,
Password: shard.Master.Password,
Protocol: shard.Master.Protocol,
Params: shard.Master.Params,
Conn: ConnConfig{
MaxLifeTime: config.Cluster.Conn.MaxLifetime,
MaxIdleConns: config.Cluster.Conn.MaxIdleConns,
MaxOpenConns: config.Cluster.Conn.MaxOpenConns,
},
},
}
for _, replica := range shard.Replicas.Hosts {
sc.ReplicasConfig = append(sc.ReplicasConfig, DbConfig{
Driver: config.Cluster.Driver,
Dbname: config.Cluster.Dbname,
Host: replica,
User: shard.Replicas.User,
Password: shard.Replicas.Password,
Protocol: shard.Replicas.Protocol,
Params: shard.Replicas.Params,
Conn: ConnConfig{
MaxLifeTime: config.Cluster.Conn.MaxLifetime,
MaxIdleConns: config.Cluster.Conn.MaxIdleConns,
MaxOpenConns: config.Cluster.Conn.MaxOpenConns,
},
})
}
scs = append(scs, sc)
}
if len(scs) != config.Cluster.Sharding.DbTotal {
return nil, fmt.Errorf("Shards total unequal db instance total")
}
return &Config{
ShardsConfig: scs,
ShardingConfig: ShardingConfig{
Dbname: config.Cluster.Dbname,
Type: config.Cluster.Type,
Total: config.Cluster.Sharding.DbTotal,
Table: ss,
}}, nil
}
type Config struct {
ShardingConfig ShardingConfig
ShardsConfig []ShardConfig
}
func (c Config) GetShardsConfig() []ShardConfig {
return c.ShardsConfig
}
func (c Config) GetShardingConfig() ShardingConfig {
return c.ShardingConfig
}
type ShardingConfig struct {
Dbname string
Total int
Type string
Table []ShardingTableConfig
}
func (s ShardingConfig) GetDbname() string {
return s.Dbname
}
func (s ShardingConfig) GetType() string {
return s.Type
}
func (s ShardingConfig) GetTotal() int {
return s.Total
}
func (s ShardingConfig) GetTableConfig() []ShardingTableConfig {
return s.Table
}
type ShardingTableConfig struct {
Name string
Total int
}
func (s *ShardingTableConfig) GetTableName() string {
return s.Name
}
func (s *ShardingTableConfig) GetTotal() int {
return s.Total
}
type ShardConfig struct {
MasterConfig DbConfig
ReplicasConfig []DbConfig
}
func (c ShardConfig) GetMasterConfig() DbConfig {
return c.MasterConfig
}
func (c ShardConfig) GetReplicasConfig() []DbConfig {
return c.ReplicasConfig
}
type DbConfig struct {
Driver string
Dbname string
Protocol string
Host string
User string
Password string
Params map[string]string
Conn ConnConfig
}
func (c DbConfig) GetDriver() string {
return c.Driver
}
func (c DbConfig) GetDbname() string {
return c.Dbname
}
func (c DbConfig) GetProtocol() string {
return c.Protocol
}
func (c DbConfig) GetHost() string {
return c.Host
}
func (c DbConfig) GetUser() string {
return c.User
}
func (c DbConfig) GetPassword() string {
return c.Password
}
func (c DbConfig) GetParams() map[string]string {
return c.Params
}
func (c DbConfig) GetConn() ConnConfig {
return c.Conn
}
type ConnConfig struct {
MaxLifeTime int
MaxIdleConns int
MaxOpenConns int
}
func (c ConnConfig) GetMaxLifeTime() time.Duration {
return time.Duration(c.MaxLifeTime) * time.Millisecond
}
func (c ConnConfig) GetMaxIdleConns() int {
return c.MaxIdleConns
}
func (c ConnConfig) GetMaxOpenConns() int {
return c.MaxOpenConns
}
func (c DbConfig) GetUrl(dbname string) string {
b := AcquireBuffer()
defer ReleaseBuffer(b)
b.WriteString(c.User)
b.WriteByte(':')
b.WriteString(c.Password)
b.WriteByte('@')
if len(c.Protocol) > 0 {
b.WriteString(c.Protocol)
} else {
b.WriteString("tcp")
}
b.WriteByte('(')
b.WriteString(c.Host)
b.WriteByte(')')
b.WriteByte('/')
if 0 == len(dbname) {
b.WriteString(c.Dbname)
} else {
b.WriteString(dbname)
}
if total := len(c.Params); total > 0 {
params := make(url.Values, total)
for key, v := range c.Params {
params.Set(key, v)
}
if p := params.Encode(); 0 != len(p) {
b.WriteByte('?')
b.WriteString(p)
}
}
return b.String()
}