pgtk 0.30.10 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/pgtk/pool.rb +16 -4
- data/lib/pgtk/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5bce43b422e6baef8427e57d0635f613f2af93eea4a2f783b2fa62c85520b565
|
|
4
|
+
data.tar.gz: ea73771e5ea9c4f17919d257316d548280cf623e77f50266f6283e8f0ed71d8b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 908b1a4e77e794a5d91aae82169969bac770d3638771356f65b7f5ac7b2b490b14a99f5cc7caba4e63e99e09678d423f51944faf52f6c368040a6f4d55eb9b78
|
|
7
|
+
data.tar.gz: 5a15b9c10cb8f238b00888d761436633d8381e7b52c731af3c8fa67c5c4c48cd27274dc3975d9b3eaf612f189ffa871c026eca758991924b4d0aa3b3a4b9099a
|
data/lib/pgtk/pool.rb
CHANGED
|
@@ -49,16 +49,22 @@ require_relative 'wire'
|
|
|
49
49
|
# Copyright:: Copyright (c) 2019-2026 Yegor Bugayenko
|
|
50
50
|
# License:: MIT
|
|
51
51
|
class Pgtk::Pool
|
|
52
|
+
# Raised when no connection becomes available from the pool within
|
|
53
|
+
# the configured timeout. Indicates that all connections are currently
|
|
54
|
+
# taken by other threads and none was returned in time.
|
|
55
|
+
class Busy < StandardError; end
|
|
56
|
+
|
|
52
57
|
# Constructor.
|
|
53
58
|
#
|
|
54
59
|
# @param [Pgtk::Wire] wire The wire
|
|
55
60
|
# @param [Integer] max Total amount of PostgreSQL connections in the pool
|
|
61
|
+
# @param [Numeric] timeout Max seconds to wait for a free connection
|
|
56
62
|
# @param [Object] log The log
|
|
57
|
-
def initialize(wire, max: 8, log: Loog::NULL)
|
|
63
|
+
def initialize(wire, max: 8, timeout: 1, log: Loog::NULL)
|
|
58
64
|
@wire = wire
|
|
59
65
|
@max = max
|
|
60
66
|
@log = log
|
|
61
|
-
@pool = IterableQueue.new(max)
|
|
67
|
+
@pool = IterableQueue.new(max, timeout)
|
|
62
68
|
@started = false
|
|
63
69
|
end
|
|
64
70
|
|
|
@@ -231,8 +237,9 @@ class Pgtk::Pool
|
|
|
231
237
|
# the internal array but is marked as "taken". When returned, it's placed
|
|
232
238
|
# back in its original slot and marked as available.
|
|
233
239
|
class IterableQueue
|
|
234
|
-
def initialize(size)
|
|
240
|
+
def initialize(size, timeout)
|
|
235
241
|
@size = size
|
|
242
|
+
@timeout = timeout
|
|
236
243
|
@items = []
|
|
237
244
|
@taken = []
|
|
238
245
|
@mutex = Mutex.new
|
|
@@ -259,7 +266,12 @@ class Pgtk::Pool
|
|
|
259
266
|
|
|
260
267
|
def pop
|
|
261
268
|
@mutex.synchronize do
|
|
262
|
-
|
|
269
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout
|
|
270
|
+
while @taken.all? || @items.empty?
|
|
271
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
272
|
+
raise(Busy, "No free connection appeared in the pool after #{@timeout}s of waiting") if remaining <= 0
|
|
273
|
+
@condition.wait(@mutex, remaining)
|
|
274
|
+
end
|
|
263
275
|
index = @taken.index(false)
|
|
264
276
|
@taken[index] = true
|
|
265
277
|
@items[index]
|
data/lib/pgtk/version.rb
CHANGED