/
pgproxy.go
273 lines (256 loc) · 5.75 KB
/
pgproxy.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package pgproxy
import (
"log"
"net/url"
"strconv"
"strings"
"sync"
"time"
pg "github.com/go-pg/pg/v9"
)
// DBProxyCallback pg数据库操作的回调函数
type dbProxyCallback func(dbCli *pg.DB) error
// DBProxy 数据库客户端的代理
type dbProxy struct {
proxyLock sync.RWMutex //代理的锁
Options *pg.Options
conn *pg.DB
callBacks []dbProxyCallback
}
// New 创建一个新的数据库客户端代理
func New() *dbProxy {
proxy := new(dbProxy)
proxy.proxyLock = sync.RWMutex{}
return proxy
}
// IsOk 检查代理是否已经可用
func (proxy *dbProxy) IsOk() bool {
if proxy.conn == nil {
return false
}
return true
}
//GetConn 获取被代理的连接
func (proxy *dbProxy) GetConn() (*pg.DB, error) {
if !proxy.IsOk() {
return proxy.conn, ErrProxyNotInited
}
proxy.proxyLock.RLock()
defer proxy.proxyLock.RUnlock()
return proxy.conn, nil
}
// Close 关闭pg
func (proxy *dbProxy) Close() {
if proxy.IsOk() {
proxy.conn.Close()
proxy.proxyLock.Lock()
proxy.conn = nil
proxy.proxyLock.Unlock()
}
}
//SetConnect 设置连接的客户端
func (proxy *dbProxy) SetConnect(cli *pg.DB) {
proxy.proxyLock.Lock()
proxy.conn = cli
proxy.proxyLock.Unlock()
for _, cb := range proxy.callBacks {
err := cb(proxy.conn)
if err != nil {
log.Println("regist callback get error", err)
} else {
log.Println("regist callback done")
}
}
}
// 将url解析为pg的初始化参数
func parseDBURL(address string) (*pg.Options, error) {
result := &pg.Options{}
u, err := url.Parse(address)
if err != nil {
return result, err
}
if u.Scheme != "postgres" {
return result, ErrURLSchemaWrong
}
user := u.User.Username()
if user == "" {
result.User = "postgres"
} else {
result.User = user
}
password, has := u.User.Password()
if has == false {
result.Password = "postgres"
} else {
result.Password = password
}
result.Addr = u.Host
result.Database = u.Path[1:]
if u.RawQuery != "" {
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
log.Fatal(err)
return result, nil
}
for key, value := range v {
switch strings.ToLower(key) {
case "maxretries":
{
maxretries, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.MaxRetries = maxretries
}
}
case "retrystatementtimeout":
{
bv := strings.ToLower(value[0])
switch bv {
case "true":
result.RetryStatementTimeout = true
case "false":
result.RetryStatementTimeout = false
default:
log.Fatal("unknown value for RetryStatementTimeout")
}
}
case "minretrybackoff":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.MinRetryBackoff = time.Duration(number) * time.Second
}
}
case "maxretrybackoff":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.MaxRetryBackoff = time.Duration(number) * time.Second
}
}
case "dialtimeout":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.DialTimeout = time.Duration(number) * time.Second
}
}
case "readtimeout":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.ReadTimeout = time.Duration(number) * time.Second
}
}
case "writetimeout":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.WriteTimeout = time.Duration(number) * time.Second
}
}
case "maxconnage":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.MaxConnAge = time.Duration(number) * time.Second
}
}
case "pooltimeout":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.PoolTimeout = time.Duration(number) * time.Second
}
}
case "idletimeout":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.IdleTimeout = time.Duration(number) * time.Second
}
}
case "idlecheckfrequency":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.IdleCheckFrequency = time.Duration(number) * time.Second
}
}
case "PoolSize":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.PoolSize = number
}
}
case "MinIdleConns":
{
number, err := strconv.Atoi(value[0])
if err != nil {
log.Fatal(err)
} else {
result.MinIdleConns = number
}
}
}
}
}
return result, nil
}
// Init 给代理赋值客户端实例
func (proxy *dbProxy) Init(cli *pg.DB) error {
if proxy.IsOk() {
return ErrProxyAlreadyInited
}
proxy.SetConnect(cli)
return nil
}
// InitFromOptions 从配置条件初始化代理对象
func (proxy *dbProxy) InitFromOptions(options *pg.Options) error {
proxy.Options = options
db := pg.Connect(options)
return proxy.Init(db)
}
// InitFromURL 使用配置给代理赋值客户端实例
func (proxy *dbProxy) InitFromURL(address string) error {
options, err := parseDBURL(address)
if err != nil {
return err
}
err = proxy.InitFromOptions(options)
return err
}
func (proxy *dbProxy) Exec(query interface{}, params ...interface{}) (res pg.Result, err error) {
if !proxy.IsOk() {
return nil, ErrProxyNotInited
}
return proxy.conn.Exec(query, params)
}
// Regist 注册回调函数,在init执行后执行回调函数
func (proxy *dbProxy) Regist(cb dbProxyCallback) {
proxy.callBacks = append(proxy.callBacks, cb)
}
//Proxy 默认的pg代理对象
var Proxy = New()