forked from rethinkdb/rethinkdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
200 lines (160 loc) · 3.68 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
package gorethink
import (
"errors"
"fmt"
"net"
"sync"
"gopkg.in/fatih/pool.v2"
)
var (
errPoolClosed = errors.New("gorethink: pool is closed")
)
// A Pool is used to store a pool of connections to a single RethinkDB server
type Pool struct {
host Host
opts *ConnectOpts
pool pool.Pool
mu sync.RWMutex // protects following fields
closed bool
}
// NewPool creates a new connection pool for the given host
func NewPool(host Host, opts *ConnectOpts) (*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 = 2
}
p, err := pool.NewChannelPool(initialCap, maxOpen, func() (net.Conn, error) {
conn, err := NewConnection(host.String(), opts)
if err != nil {
return nil, err
}
return conn, err
})
if err != nil {
return nil, err
}
return &Pool{
pool: p,
host: host,
opts: opts,
}, nil
}
// Ping verifies a connection to the database is still alive,
// establishing a connection if necessary.
func (p *Pool) Ping() error {
_, pc, err := p.conn()
if err != nil {
return err
}
return pc.Close()
}
// 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 {
p.mu.RLock()
defer p.mu.RUnlock()
if p.closed {
return nil
}
p.pool.Close()
return nil
}
func (p *Pool) conn() (*Connection, *pool.PoolConn, error) {
p.mu.RLock()
defer p.mu.RUnlock()
if p.closed {
return nil, nil, errPoolClosed
}
nc, err := p.pool.Get()
if err != nil {
return nil, nil, err
}
pc, ok := nc.(*pool.PoolConn)
if !ok {
// This should never happen!
return nil, nil, fmt.Errorf("Invalid connection in pool")
}
conn, ok := pc.Conn.(*Connection)
if !ok {
// This should never happen!
return nil, nil, fmt.Errorf("Invalid connection in pool")
}
return conn, pc, 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(q Query) error {
c, pc, err := p.conn()
if err != nil {
return err
}
defer pc.Close()
_, _, err = c.Query(q)
if c.isBad() {
pc.MarkUnusable()
}
return err
}
// Query executes a query and waits for the response
func (p *Pool) Query(q Query) (*Cursor, error) {
c, pc, err := p.conn()
if err != nil {
return nil, err
}
_, cursor, err := c.Query(q)
if err == nil {
cursor.releaseConn = releaseConn(c, pc)
} else if c.isBad() {
pc.MarkUnusable()
}
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, pc, err := p.conn()
if err != nil {
return response, err
}
defer pc.Close()
response, err = c.Server()
if c.isBad() {
pc.MarkUnusable()
}
return response, err
}
func releaseConn(c *Connection, pc *pool.PoolConn) func() error {
return func() error {
if c.isBad() {
pc.MarkUnusable()
}
return pc.Close()
}
}