pgtk 0.30.10 → 0.31.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eef46168e2daa0ddc6721aac48a99dc20f8aa80f078cfc76a31fa4ec70759af6
4
- data.tar.gz: 13dc7a6f18fcb34385c3580700e8e192b3bb38daab86f64b85fa64ac3f3dcce0
3
+ metadata.gz: a15b5d36eab5407bca78c591056966cee9b90167e5bfa0cc98fa36984801622a
4
+ data.tar.gz: bd201e64d30a88836337d86b4b3d202b708109de1676ce5d83593df8f4e4ace4
5
5
  SHA512:
6
- metadata.gz: 18489070b6f37d64338982c7ade356881ffe7a6fc01f6cc1b837f2433ed2779a4838388ec60468ec80c3a21342b7f9c19b52bb87792c7bf0dc7011fb7163d9cd
7
- data.tar.gz: bc115bdd5e53ff555e1e1d383d90f9ecc80d33b282cd57e89b03540a14f35792e718b5f74c64a637c761da7762791edcc9534f49612ebe5984836817c1e1c314
6
+ metadata.gz: 8b58b179b7c85ff2183d7c909da288f9a4be5c67bbe8040350c8706098bac8ccbfe05010d638f28db67cf70b1434744abc6667008f1fcc32bdc6aaa3a4cb1fd8
7
+ data.tar.gz: 0da90a8d6b61c07125bab269833149217f625caadbfbb8e12363ac84f84efa36e321fb6ec18f5b12c0b1bb9dfdab42ad74086cb6300b9fb0b122e2a52e9384cf
data/Gemfile.lock CHANGED
@@ -42,7 +42,7 @@ GEM
42
42
  loog (0.8.0)
43
43
  ellipsized
44
44
  logger (~> 1.0)
45
- minitest (6.0.4)
45
+ minitest (6.0.5)
46
46
  drb (~> 2.0)
47
47
  prism (~> 1.5)
48
48
  minitest-mock (5.27.0)
@@ -108,7 +108,7 @@ GEM
108
108
  rubocop-ast (1.49.1)
109
109
  parser (>= 3.3.7.2)
110
110
  prism (~> 1.7)
111
- rubocop-elegant (0.0.18)
111
+ rubocop-elegant (0.0.20)
112
112
  lint_roller (~> 1.1)
113
113
  rubocop (~> 1.75)
114
114
  rubocop-minitest (0.39.1)
@@ -119,13 +119,19 @@ class Pgtk::Impatient
119
119
  end
120
120
  end
121
121
 
122
- # Run a transaction with a timeout for each query.
122
+ # Run a transaction with a timeout for each query and for idle time inside
123
+ # the transaction. If the transaction stays in the +INTRANS+ state (idle
124
+ # inside transaction) for longer than the configured timeout, PostgreSQL
125
+ # terminates the session, which frees locks and releases the connection
126
+ # slot back to the pool.
123
127
  #
124
128
  # @yield [Pgtk::Impatient] Yields an impatient transaction
125
129
  # @return [Object] Result of the block
126
130
  def transaction
127
131
  @pool.transaction do |t|
128
- t.exec("SET LOCAL statement_timeout = #{Integer((@timeout * 1000).to_s, 10)}")
132
+ ms = Integer((@timeout * 1000).to_s, 10)
133
+ t.exec("SET LOCAL statement_timeout = #{ms}")
134
+ t.exec("SET LOCAL idle_in_transaction_session_timeout = #{ms}")
129
135
  yield(Pgtk::Impatient.new(t, @timeout))
130
136
  end
131
137
  end
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.1' 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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko