forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_pool.go
158 lines (135 loc) · 3.65 KB
/
conn_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
// Copyright 2012, 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 tabletserver
import (
"sync"
"time"
"github.com/youtube/vitess/go/pools"
"github.com/youtube/vitess/go/stats"
)
// ConnectionPool re-exposes ResourcePool as a pool of DBConnection objects
type ConnectionPool struct {
mu sync.Mutex
connections *pools.ResourcePool
capacity int
idleTimeout time.Duration
}
func NewConnectionPool(name string, capacity int, idleTimeout time.Duration) *ConnectionPool {
cp := &ConnectionPool{capacity: capacity, idleTimeout: idleTimeout}
if name == "" {
return cp
}
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 *ConnectionPool) pool() (p *pools.ResourcePool) {
cp.mu.Lock()
p = cp.connections
cp.mu.Unlock()
return p
}
func (cp *ConnectionPool) Open(connFactory CreateConnectionFunc) {
cp.mu.Lock()
defer cp.mu.Unlock()
f := func() (pools.Resource, error) {
c, err := connFactory()
if err != nil {
return nil, err
}
return &pooledConnection{c, cp}, nil
}
cp.connections = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout)
}
func (cp *ConnectionPool) Close() {
// We should not hold the lock while calling Close
// because it could be long-running.
cp.pool().Close()
cp.mu.Lock()
cp.connections = nil
cp.mu.Unlock()
}
// You must call Recycle on the PoolConnection once done.
func (cp *ConnectionPool) Get() PoolConnection {
r, err := cp.pool().Get()
if err != nil {
panic(NewTabletErrorSql(FATAL, err))
}
return r.(*pooledConnection)
}
// You must call Recycle on the PoolConnection once done.
func (cp *ConnectionPool) SafeGet() (PoolConnection, error) {
r, err := cp.pool().Get()
if err != nil {
return nil, err
}
return r.(*pooledConnection), nil
}
// You must call Recycle on the PoolConnection once done.
func (cp *ConnectionPool) TryGet() PoolConnection {
r, err := cp.pool().TryGet()
if err != nil {
panic(NewTabletErrorSql(FATAL, err))
}
if r == nil {
return nil
}
return r.(*pooledConnection)
}
func (cp *ConnectionPool) Put(conn PoolConnection) {
cp.pool().Put(conn)
}
func (cp *ConnectionPool) SetCapacity(capacity int) (err error) {
cp.mu.Lock()
defer cp.mu.Unlock()
err = cp.connections.SetCapacity(capacity)
if err != nil {
return err
}
cp.capacity = capacity
return nil
}
func (cp *ConnectionPool) SetIdleTimeout(idleTimeout time.Duration) {
cp.mu.Lock()
defer cp.mu.Unlock()
cp.connections.SetIdleTimeout(idleTimeout)
cp.idleTimeout = idleTimeout
}
func (cp *ConnectionPool) StatsJSON() string {
return cp.pool().StatsJSON()
}
func (cp *ConnectionPool) Capacity() int64 {
return cp.pool().Capacity()
}
func (cp *ConnectionPool) Available() int64 {
return cp.pool().Available()
}
func (cp *ConnectionPool) MaxCap() int64 {
return cp.pool().MaxCap()
}
func (cp *ConnectionPool) WaitCount() int64 {
return cp.pool().WaitCount()
}
func (cp *ConnectionPool) WaitTime() time.Duration {
return cp.pool().WaitTime()
}
func (cp *ConnectionPool) IdleTimeout() time.Duration {
return cp.pool().IdleTimeout()
}
// pooledConnection re-exposes DBConnection as a PoolConnection
type pooledConnection struct {
*DBConnection
pool *ConnectionPool
}
func (pc *pooledConnection) Recycle() {
if pc.IsClosed() {
pc.pool.Put(nil)
} else {
pc.pool.Put(pc)
}
}