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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pgtk/pool.rb +16 -4
  3. data/lib/pgtk/version.rb +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eef46168e2daa0ddc6721aac48a99dc20f8aa80f078cfc76a31fa4ec70759af6
4
- data.tar.gz: 13dc7a6f18fcb34385c3580700e8e192b3bb38daab86f64b85fa64ac3f3dcce0
3
+ metadata.gz: 5bce43b422e6baef8427e57d0635f613f2af93eea4a2f783b2fa62c85520b565
4
+ data.tar.gz: ea73771e5ea9c4f17919d257316d548280cf623e77f50266f6283e8f0ed71d8b
5
5
  SHA512:
6
- metadata.gz: 18489070b6f37d64338982c7ade356881ffe7a6fc01f6cc1b837f2433ed2779a4838388ec60468ec80c3a21342b7f9c19b52bb87792c7bf0dc7011fb7163d9cd
7
- data.tar.gz: bc115bdd5e53ff555e1e1d383d90f9ecc80d33b282cd57e89b03540a14f35792e718b5f74c64a637c761da7762791edcc9534f49612ebe5984836817c1e1c314
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
- @condition.wait(@mutex) while @taken.all? || @items.empty?
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
@@ -10,5 +10,5 @@ require_relative '../pgtk'
10
10
  # Copyright:: Copyright (c) 2019-2026 Yegor Bugayenko
11
11
  # License:: MIT
12
12
  module Pgtk
13
- VERSION = '0.30.10' unless defined?(VERSION)
13
+ VERSION = '0.31.0' unless defined?(VERSION)
14
14
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.10
4
+ version: 0.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko