-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
pool.cr
309 lines (269 loc) · 8.88 KB
/
pool.cr
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require "weak_ref"
require "./error"
module DB
class Pool(T)
record Options,
# initial number of connections in the pool
initial_pool_size : Int32 = 1,
# maximum amount of connections in the pool (Idle + InUse). 0 means no maximum.
max_pool_size : Int32 = 0,
# maximum amount of idle connections in the pool
max_idle_pool_size : Int32 = 1,
# seconds to wait before timeout while doing a checkout
checkout_timeout : Float64 = 5.0,
# maximum amount of retry attempts to reconnect to the db. See `Pool#retry`
retry_attempts : Int32 = 1,
# seconds to wait before a retry attempt
retry_delay : Float64 = 0.2 do
def self.from_http_params(params : HTTP::Params, default = Options.new)
Options.new(
initial_pool_size: params.fetch("initial_pool_size", default.initial_pool_size).to_i,
max_pool_size: params.fetch("max_pool_size", default.max_pool_size).to_i,
max_idle_pool_size: params.fetch("max_idle_pool_size", default.max_idle_pool_size).to_i,
checkout_timeout: params.fetch("checkout_timeout", default.checkout_timeout).to_f,
retry_attempts: params.fetch("retry_attempts", default.retry_attempts).to_i,
retry_delay: params.fetch("retry_delay", default.retry_delay).to_f,
)
end
end
# Pool configuration
# initial number of connections in the pool
@initial_pool_size : Int32
# maximum amount of connections in the pool (Idle + InUse)
@max_pool_size : Int32
# maximum amount of idle connections in the pool
@max_idle_pool_size : Int32
# seconds to wait before timeout while doing a checkout
@checkout_timeout : Float64
# maximum amount of retry attempts to reconnect to the db. See `Pool#retry`
@retry_attempts : Int32
# seconds to wait before a retry attempt
@retry_delay : Float64
# Pool state
# total of open connections managed by this pool
@total = [] of T
# connections available for checkout
@idle = Set(T).new
# connections waiting to be stablished (they are not in *@idle* nor in *@total*)
@inflight : Int32
# Sync state
# communicate that a connection is available for checkout
@availability_channel : Channel(Nil)
# global pool mutex
@mutex : Mutex
@[Deprecated("Use `#new` with DB::Pool::Options instead")]
def initialize(initial_pool_size = 1, max_pool_size = 0, max_idle_pool_size = 1, checkout_timeout = 5.0,
retry_attempts = 1, retry_delay = 0.2, &factory : -> T)
initialize(
Options.new(
initial_pool_size: initial_pool_size, max_pool_size: max_pool_size,
max_idle_pool_size: max_idle_pool_size, checkout_timeout: checkout_timeout,
retry_attempts: retry_attempts, retry_delay: retry_delay),
&factory)
end
def initialize(pool_options : Options = Options.new, &@factory : -> T)
@initial_pool_size = pool_options.initial_pool_size
@max_pool_size = pool_options.max_pool_size
@max_idle_pool_size = pool_options.max_idle_pool_size
@checkout_timeout = pool_options.checkout_timeout
@retry_attempts = pool_options.retry_attempts
@retry_delay = pool_options.retry_delay
@availability_channel = Channel(Nil).new
@inflight = 0
@mutex = Mutex.new
@initial_pool_size.times { build_resource }
end
# close all resources in the pool
def close : Nil
@total.each &.close
@total.clear
@idle.clear
end
record Stats,
open_connections : Int32,
idle_connections : Int32,
in_flight_connections : Int32,
max_connections : Int32
# Returns stats of the pool
def stats
Stats.new(
open_connections: @total.size,
idle_connections: @idle.size,
in_flight_connections: @inflight,
max_connections: @max_pool_size,
)
end
def checkout : T
res = sync do
resource = nil
until resource
resource = if @idle.empty?
if can_increase_pool?
@inflight += 1
begin
r = unsync { build_resource }
ensure
@inflight -= 1
end
r
else
unsync { wait_for_available }
# The wait for available can unlock
# multiple fibers waiting for a resource.
# Although only one will pick it due to the lock
# in the end of the unsync, the pick_available
# will return nil
pick_available
end
else
pick_available
end
end
@idle.delete resource
resource
end
if res.responds_to?(:before_checkout)
res.before_checkout
end
res
end
def checkout(&block : T ->)
connection = checkout
begin
yield connection
ensure
release connection
end
end
# ```
# selected, is_candidate = pool.checkout_some(candidates)
# ```
# `selected` be a resource from the `candidates` list and `is_candidate` == `true`
# or `selected` will be a new resource and `is_candidate` == `false`
def checkout_some(candidates : Enumerable(WeakRef(T))) : {T, Bool}
sync do
candidates.each do |ref|
resource = ref.value
if resource && is_available?(resource)
@idle.delete resource
resource.before_checkout
return {resource, true}
end
end
end
resource = checkout
{resource, candidates.any? { |ref| ref.value == resource }}
end
def release(resource : T) : Nil
idle_pushed = false
sync do
if resource.responds_to?(:closed?) && resource.closed?
@total.delete(resource)
elsif can_increase_idle_pool
@idle << resource
if resource.responds_to?(:after_release)
resource.after_release
end
idle_pushed = true
else
resource.close
@total.delete(resource)
end
end
if idle_pushed
select
when @availability_channel.send(nil)
else
end
end
end
# :nodoc:
# Will retry the block if a `ConnectionLost` exception is thrown.
# It will try to reuse all of the available connection right away,
# but if a new connection is needed there is a `retry_delay` seconds delay.
def retry
current_available = 0
sync do
current_available = @idle.size
# if the pool hasn't reach the max size, allow 1 attempt
# to make a new connection if needed without sleeping
current_available += 1 if can_increase_pool?
end
(current_available + @retry_attempts).times do |i|
begin
sleep @retry_delay if i >= current_available
return yield
rescue e : PoolResourceLost(T)
# if the connection is lost it will be closed by
# the exception to release resources
# we still need to remove it from the known pool.
# Closed connection will be evicted from statement cache
# in PoolPreparedStatement#clean_connections
sync { delete(e.resource) }
rescue e : PoolResourceRefused
# a ConnectionRefused means a new connection
# was intended to be created
# nothing to due but to retry soon
end
end
raise PoolRetryAttemptsExceeded.new
end
# :nodoc:
def each_resource
sync do
@idle.each do |resource|
yield resource
end
end
end
# :nodoc:
def is_available?(resource : T)
@idle.includes?(resource)
end
# :nodoc:
def delete(resource : T)
@total.delete(resource)
@idle.delete(resource)
end
private def build_resource : T
resource = @factory.call
sync do
@total << resource
@idle << resource
end
resource
end
private def can_increase_pool?
@max_pool_size == 0 || @total.size + @inflight < @max_pool_size
end
private def can_increase_idle_pool
@idle.size < @max_idle_pool_size
end
private def pick_available
@idle.first?
end
private def wait_for_available
select
when @availability_channel.receive
when timeout(@checkout_timeout.seconds)
raise DB::PoolTimeout.new("Could not check out a connection in #{@checkout_timeout} seconds")
end
end
private def sync
@mutex.lock
begin
yield
ensure
@mutex.unlock
end
end
private def unsync
@mutex.unlock
begin
yield
ensure
@mutex.lock
end
end
end
end