-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathpool.go
More file actions
214 lines (189 loc) · 4.56 KB
/
pool.go
File metadata and controls
214 lines (189 loc) · 4.56 KB
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package backend
import (
"bytes"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"config"
"xbase/stats"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
poolCounterPing = "#pool.ping"
poolCounterPingBroken = "#pool.ping.broken"
poolCounterHit = "#pool.hit"
poolCounterMiss = "#pool.miss"
poolCounterGet = "#pool.get"
poolCounterPut = "#pool.put"
poolCounterClose = "#pool.close"
poolCounterBackendDialError = "#backend.dial.error"
poolCounterBackendExecuteTimeout = "#backend.execute.timeout"
poolCounterBackendExecuteMaxresult = "#backend.execute.maxresult"
poolCounterBackendExecuteAllError = "#backend.execute.all.error"
poolCounterBackendKilled = "#backend.killed"
)
var (
maxIdleTime = 20 // 20s
errClosed = errors.New("can't get connection from the closed DB")
)
// Poolz ...
// Add replica and normal pool to distribute SQL between
// read and write in some cases for load-balance.
type Poolz struct {
log *xlog.Log
conf *config.BackendConfig
normal *Pool
replica *Pool
}
// NewPoolz create the new Poolz.
func NewPoolz(log *xlog.Log, conf *config.BackendConfig) *Poolz {
return &Poolz{
log: log,
conf: conf,
normal: NewPool(log, conf, conf.Address),
replica: NewPool(log, conf, conf.Replica),
}
}
// Close used to close the poolz.
func (p *Poolz) Close() {
if p.normal != nil {
p.normal.Close()
}
if p.replica != nil {
p.replica.Close()
}
}
// JSON returns the available string.
func (p *Poolz) JSON() string {
str := p.normal.JSON()
if p.replica != nil {
str += ", " + p.replica.JSON()
}
return str
}
// Pool tuple.
type Pool struct {
mu sync.RWMutex
log *xlog.Log
address string
conf *config.BackendConfig
counters *stats.Counters
connections chan Connection
// If maxIdleTime reached, the connection will be closed by get.
maxIdleTime int64
}
// NewPool creates the new Pool.
func NewPool(log *xlog.Log, conf *config.BackendConfig, address string) *Pool {
if address == "" {
return nil
}
return &Pool{
log: log,
address: address,
conf: conf,
connections: make(chan Connection, conf.MaxConnections),
counters: stats.NewCounters(conf.Name + "@" + address),
maxIdleTime: int64(maxIdleTime),
}
}
func (p *Pool) reconnect() (Connection, error) {
log := p.log
c := NewConnection(log, p)
if err := c.Dial(); err != nil {
log.Error("pool.reconnect.dial.error:%+v", err)
return nil, err
}
c.SetTimestamp(time.Now().Unix())
return c, nil
}
// Get used to get a connection from the pool.
func (p *Pool) Get() (Connection, error) {
counters := p.counters
counters.Add(poolCounterGet, 1)
conns := p.getConns()
if conns == nil {
return nil, errClosed
}
select {
case conn, more := <-conns:
if !more {
return nil, errClosed
}
// If the idle time more than 1s,
// we will do a ping to check the connection is OK or NOT.
now := time.Now().Unix()
elapsed := (now - conn.Timestamp())
if elapsed > 1 {
// If elapsed time more than 20s, we create new one.
if elapsed > atomic.LoadInt64(&p.maxIdleTime) {
conn.Close()
return p.reconnect()
}
if err := conn.Ping(); err != nil {
counters.Add(poolCounterPingBroken, 1)
return p.reconnect()
}
counters.Add(poolCounterPing, 1)
}
counters.Add(poolCounterHit, 1)
return conn, nil
default:
counters.Add(poolCounterMiss, 1)
return p.reconnect()
}
}
// Put used to put a connection to pool.
func (p *Pool) Put(conn Connection) {
p.put(conn, true)
}
func (p *Pool) put(conn Connection, updateTs bool) {
p.counters.Add(poolCounterPut, 1)
p.mu.Lock()
defer p.mu.Unlock()
if p.connections == nil {
return
}
if updateTs {
conn.SetTimestamp(time.Now().Unix())
}
select {
case p.connections <- conn:
default:
conn.Close()
}
}
// Close used to close the pool.
func (p *Pool) Close() {
p.counters.Add(poolCounterClose, 1)
p.mu.Lock()
defer p.mu.Unlock()
if p.connections == nil {
return
}
close(p.connections)
for conn := range p.connections {
conn.Close()
}
p.connections = nil
}
func (p *Pool) getConns() chan Connection {
p.mu.RLock()
defer p.mu.RUnlock()
return p.connections
}
// JSON returns the available string.
// available is the number of currently unused connections.
func (p *Pool) JSON() string {
b := bytes.NewBuffer(make([]byte, 0, 256))
fmt.Fprintf(b, "{'name': '%s@%s', 'capacity': %d, 'counters': %s}", p.conf.Name, p.address, p.conf.MaxConnections, p.counters.String())
return b.String()
}