forked from vitessio/vitess
/
pool.go
224 lines (201 loc) · 5.41 KB
/
pool.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
// Copyright 2015, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package connpool
import (
"sync"
"time"
"github.com/youtube/vitess/go/pools"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/vt/dbconnpool"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/tabletenv"
"github.com/youtube/vitess/go/vt/vterrors"
"golang.org/x/net/context"
)
// ErrConnPoolClosed is returned when the connection pool is closed.
var ErrConnPoolClosed = vterrors.New(vtrpcpb.Code_INTERNAL, "internal error: unexpected: conn pool is closed")
// usedNames is for preventing expvar from panicking. Tests
// create pool objects multiple time. If a name was previously
// used, expvar initialization is skipped.
// TODO(sougou): Find a way to still crash if this happened
// through non-test code.
var usedNames = make(map[string]bool)
type mysqlChecker interface {
CheckMySQL()
}
// Pool implements a custom connection pool for tabletserver.
// It's similar to dbconnpool.ConnPool, but the connections it creates
// come with built-in ability to kill in-flight queries. These connections
// also trigger a CheckMySQL call if we fail to connect to MySQL.
// Other than the connection type, ConnPool maintains an additional
// pool of dba connections that are used to kill connections.
type Pool struct {
mu sync.Mutex
connections *pools.ResourcePool
capacity int
idleTimeout time.Duration
dbaPool *dbconnpool.ConnectionPool
checker mysqlChecker
}
// New creates a new Pool. The name is used
// to publish stats only.
func New(
name string,
capacity int,
idleTimeout time.Duration,
checker mysqlChecker) *Pool {
cp := &Pool{
capacity: capacity,
idleTimeout: idleTimeout,
dbaPool: dbconnpool.NewConnectionPool("", 1, idleTimeout),
checker: checker,
}
if name == "" || usedNames[name] {
return cp
}
usedNames[name] = true
stats.Publish(name+"Capacity", stats.IntFunc(cp.Capacity))
stats.Publish(name+"Available", stats.IntFunc(cp.Available))
stats.Publish(name+"MaxCap", stats.IntFunc(cp.MaxCap))
stats.Publish(name+"WaitCount", stats.IntFunc(cp.WaitCount))
stats.Publish(name+"WaitTime", stats.DurationFunc(cp.WaitTime))
stats.Publish(name+"IdleTimeout", stats.DurationFunc(cp.IdleTimeout))
return cp
}
func (cp *Pool) pool() (p *pools.ResourcePool) {
cp.mu.Lock()
p = cp.connections
cp.mu.Unlock()
return p
}
// Open must be called before starting to use the pool.
func (cp *Pool) Open(appParams, dbaParams *sqldb.ConnParams) {
cp.mu.Lock()
defer cp.mu.Unlock()
f := func() (pools.Resource, error) {
return NewDBConn(cp, appParams, dbaParams)
}
cp.connections = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout)
cp.dbaPool.Open(dbconnpool.DBConnectionCreator(dbaParams, tabletenv.MySQLStats))
}
// Close will close the pool and wait for connections to be returned before
// exiting.
func (cp *Pool) Close() {
p := cp.pool()
if p == nil {
return
}
// We should not hold the lock while calling Close
// because it waits for connections to be returned.
p.Close()
cp.mu.Lock()
cp.connections = nil
cp.mu.Unlock()
cp.dbaPool.Close()
}
// Get returns a connection.
// You must call Recycle on DBConn once done.
func (cp *Pool) Get(ctx context.Context) (*DBConn, error) {
p := cp.pool()
if p == nil {
return nil, ErrConnPoolClosed
}
r, err := p.Get(ctx)
if err != nil {
return nil, err
}
return r.(*DBConn), nil
}
// Put puts a connection into the pool.
func (cp *Pool) Put(conn *DBConn) {
p := cp.pool()
if p == nil {
panic(ErrConnPoolClosed)
}
if conn == nil {
p.Put(nil)
} else {
p.Put(conn)
}
}
// SetCapacity alters the size of the pool at runtime.
func (cp *Pool) SetCapacity(capacity int) (err error) {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.connections != nil {
err = cp.connections.SetCapacity(capacity)
if err != nil {
return err
}
}
cp.capacity = capacity
return nil
}
// SetIdleTimeout sets the idleTimeout on the pool.
func (cp *Pool) SetIdleTimeout(idleTimeout time.Duration) {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.connections != nil {
cp.connections.SetIdleTimeout(idleTimeout)
}
cp.dbaPool.SetIdleTimeout(idleTimeout)
cp.idleTimeout = idleTimeout
}
// StatsJSON returns the pool stats as a JSON object.
func (cp *Pool) StatsJSON() string {
p := cp.pool()
if p == nil {
return "{}"
}
return p.StatsJSON()
}
// Capacity returns the pool capacity.
func (cp *Pool) Capacity() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.Capacity()
}
// Available returns the number of available connections in the pool
func (cp *Pool) Available() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.Available()
}
// MaxCap returns the maximum size of the pool
func (cp *Pool) MaxCap() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.MaxCap()
}
// WaitCount returns how many clients are waiting for a connection
func (cp *Pool) WaitCount() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.WaitCount()
}
// WaitTime return the pool WaitTime.
func (cp *Pool) WaitTime() time.Duration {
p := cp.pool()
if p == nil {
return 0
}
return p.WaitTime()
}
// IdleTimeout returns the idle timeout for the pool.
func (cp *Pool) IdleTimeout() time.Duration {
p := cp.pool()
if p == nil {
return 0
}
return p.IdleTimeout()
}