-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
pool.go
203 lines (165 loc) · 4.09 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
package rethinkdb
import (
"errors"
"sync"
"sync/atomic"
"golang.org/x/net/context"
)
var (
errPoolClosed = errors.New("rethinkdb: pool is closed")
)
const (
poolIsNotClosed int32 = 0
poolIsClosed int32 = 1
)
type connFactory func(host string, opts *ConnectOpts) (*Connection, error)
// A Pool is used to store a pool of connections to a single RethinkDB server
type Pool struct {
host Host
opts *ConnectOpts
conns []*Connection
pointer int32
closed int32
connFactory connFactory
mu sync.Mutex // protects lazy creating connections
}
// NewPool creates a new connection pool for the given host
func NewPool(host Host, opts *ConnectOpts) (*Pool, error) {
return newPool(host, opts, NewConnection)
}
func newPool(host Host, opts *ConnectOpts, connFactory connFactory) (*Pool, error) {
initialCap := opts.InitialCap
if initialCap <= 0 {
// Fallback to MaxIdle if InitialCap is zero, this should be removed
// when MaxIdle is removed
initialCap = opts.MaxIdle
}
maxOpen := opts.MaxOpen
if maxOpen <= 0 {
maxOpen = 1
}
conns := make([]*Connection, maxOpen)
var err error
for i := 0; i < opts.InitialCap; i++ {
conns[i], err = connFactory(host.String(), opts)
if err != nil {
return nil, err
}
}
return &Pool{
conns: conns,
pointer: -1,
host: host,
opts: opts,
connFactory: connFactory,
closed: poolIsNotClosed,
}, nil
}
// Ping verifies a connection to the database is still alive,
// establishing a connection if necessary.
func (p *Pool) Ping() error {
_, err := p.conn()
return err
}
// Close closes the database, releasing any open resources.
//
// It is rare to Close a Pool, as the Pool handle is meant to be
// long-lived and shared between many goroutines.
func (p *Pool) Close() error {
if atomic.LoadInt32(&p.closed) == poolIsClosed {
return nil
}
p.mu.Lock()
defer p.mu.Unlock()
if p.closed == poolIsClosed {
return nil
}
p.closed = poolIsClosed
for _, c := range p.conns {
if c != nil {
err := c.Close()
if err != nil {
return err
}
}
}
return nil
}
func (p *Pool) conn() (*Connection, error) {
if atomic.LoadInt32(&p.closed) == poolIsClosed {
return nil, errPoolClosed
}
pos := atomic.AddInt32(&p.pointer, 1)
if pos == int32(len(p.conns)) {
atomic.StoreInt32(&p.pointer, 0)
}
pos = pos % int32(len(p.conns))
var err error
if p.conns[pos] == nil {
p.mu.Lock()
defer p.mu.Unlock()
if p.conns[pos] == nil {
p.conns[pos], err = p.connFactory(p.host.String(), p.opts)
if err != nil {
return nil, err
}
}
} else if p.conns[pos].isBad() {
// connBad connection needs to be reconnected
p.mu.Lock()
defer p.mu.Unlock()
p.conns[pos], err = p.connFactory(p.host.String(), p.opts)
if err != nil {
return nil, err
}
}
return p.conns[pos], nil
}
// SetInitialPoolCap sets the initial capacity of the connection pool.
//
// Deprecated: This value should only be set when connecting
func (p *Pool) SetInitialPoolCap(n int) {
return
}
// SetMaxIdleConns sets the maximum number of connections in the idle
// connection pool.
//
// Deprecated: This value should only be set when connecting
func (p *Pool) SetMaxIdleConns(n int) {
return
}
// SetMaxOpenConns sets the maximum number of open connections to the database.
//
// Deprecated: This value should only be set when connecting
func (p *Pool) SetMaxOpenConns(n int) {
return
}
// Query execution functions
// Exec executes a query without waiting for any response.
func (p *Pool) Exec(ctx context.Context, q Query) error {
c, err := p.conn()
if err != nil {
return err
}
_, _, err = c.Query(ctx, q)
return err
}
// Query executes a query and waits for the response
func (p *Pool) Query(ctx context.Context, q Query) (*Cursor, error) {
c, err := p.conn()
if err != nil {
return nil, err
}
_, cursor, err := c.Query(ctx, q)
return cursor, err
}
// Server returns the server name and server UUID being used by a connection.
func (p *Pool) Server() (ServerResponse, error) {
var response ServerResponse
c, err := p.conn()
if err != nil {
return response, err
}
response, err = c.Server()
return response, err
}