-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpool.go
234 lines (190 loc) · 4.12 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
225
226
227
228
229
230
231
232
233
234
package sqlpool
import (
"database/sql"
"fmt"
"strings"
"sync"
"time"
"github.com/GitbookIO/syncgroup"
)
type Opts struct {
Max int64
IdleTimeout int64
// Init functions
PreInit func(driver, url string) error
PostInit func(db *sql.DB) error
}
type Pool struct {
opts Opts
rw sync.RWMutex
databases map[string]*Resource
inactive map[string]*Resource
conds *syncgroup.CondGroup
}
type Stats struct {
Total int
Active int
Inactive int
}
func NewPool(opts Opts) *Pool {
return &Pool{
opts: opts,
rw: sync.RWMutex{},
databases: map[string]*Resource{},
inactive: map[string]*Resource{},
conds: syncgroup.NewCondGroup(),
}
}
// What our Pool tracks
type Resource struct {
DB *sql.DB
Driver string
Url string
// Private fields used to track resource usage
users syncgroup.ActiveCounter
lastActive int64
}
func (r *Resource) Key() string {
return key(r.Driver, r.Url)
}
func (p *Pool) Acquire(driver, url string) (*Resource, error) {
// Actually get resource
resource, err := p.open(driver, url)
if err != nil {
return nil, err
} else if resource == nil {
return nil, fmt.Errorf("Failed to open %s://%s for an unknown reason", driver, url)
}
// Update resource's usage
p.acquire(resource)
return resource, nil
}
func (p *Pool) Release(r *Resource) error {
// Update resource's usage
p.release(r)
// Mark as idle
if !r.users.IsActive() {
p.rw.Lock()
p.inactive[r.Key()] = r
p.rw.Unlock()
// Do cleanup
// TODO: lazily
return p.Cleanup()
}
return nil
}
func (p *Pool) Close() error {
return p.close(false)
}
func (p *Pool) ForceClose() error {
return p.close(true)
}
func (p *Pool) close(force bool) error {
p.rw.Lock()
defer p.rw.Unlock()
for key, resource := range p.databases {
// Exit if we're not force closing
if err := resource.DB.Close(); err != nil && !force {
return err
}
p.removeResource(key)
}
return nil
}
// Cleanup removes old/inactive connections
func (p *Pool) Cleanup() error {
// Write lock
p.rw.Lock()
defer p.rw.Unlock()
// Current timestamp
now := time.Now().Unix()
for key, resource := range p.inactive {
// Skip if still valid
if (now - p.opts.IdleTimeout) < resource.lastActive {
continue
}
// Remove from inactive list and databases
delete(p.databases, key)
delete(p.inactive, key)
// Close database
go func(r *Resource) {
p.cleanupResource(r)
}(resource)
}
return nil
}
func (p *Pool) Stats() Stats {
total := len(p.databases)
inactive := len(p.inactive)
active := total - inactive
return Stats{
Total: total,
Active: active,
Inactive: inactive,
}
}
func (p *Pool) cleanupResource(r *Resource) {
// Close database
if err := r.DB.Close(); err != nil {
// TODO: log failure
}
}
func (p *Pool) acquire(r *Resource) {
r.users.Inc()
r.lastActive = time.Now().Unix()
}
func (p *Pool) release(r *Resource) {
r.users.Dec()
r.lastActive = time.Now().Unix()
}
func (p *Pool) open(driver, url string) (*Resource, error) {
// DB already opened
if p.has(driver, url) {
return p.get(driver, url), nil
}
// Open DB: only one should do this, everyone else should wait
if p.conds.Lock(key("open", driver, url)) {
defer p.conds.Unlock(key("open", driver, url))
// Before opening DB
if p.opts.PreInit != nil {
if err := p.opts.PreInit(driver, url); err != nil {
return nil, err
}
}
// Open DB
db, err := sql.Open(driver, url)
if err != nil {
return nil, err
}
// After opening DB
if p.opts.PostInit != nil {
if err := p.opts.PostInit(db); err != nil {
return nil, err
}
}
// Add db resource
p.rw.Lock()
p.databases[key(driver, url)] = &Resource{
DB: db,
Driver: driver,
Url: url,
}
p.rw.Unlock()
}
return p.get(driver, url), nil
}
func (p *Pool) removeResource(key string) {
delete(p.databases, key)
delete(p.inactive, key)
}
func (p *Pool) get(driver, url string) *Resource {
p.rw.RLock()
defer p.rw.RUnlock()
return p.databases[key(driver, url)]
}
func (p *Pool) has(driver, url string) bool {
return p.get(driver, url) != nil
}
func key(strs ...string) string {
return strings.Join(strs, ":")
}