pgtk 0.22.1 → 0.23.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0dc255f274b10a4bc7fe0a0e4d6c2a68a918957e571a147062325baf77d91db1
4
- data.tar.gz: 971da231e5ff704bf28d28f3655f170ab986327da714cd3f27bade9ff784c6bd
3
+ metadata.gz: 57546e93aaf2c029a795f61302d943e4f64e7d1e03b77d339636bdfb73d271d5
4
+ data.tar.gz: 467c0493e69e8402b83ea6b537ac69760d5e712ac088b856307d2418449878f9
5
5
  SHA512:
6
- metadata.gz: d65bd86d962357d89353daa86da5cd10f255cd1840198217963fe854cc83805c44c5fbff48e4c109871a29d17f8d6c93932a2a186b4067d6be28348cc631e174
7
- data.tar.gz: ed61c3ae252043956524564e4a4a6b087d9df684b83c509d3f92291d721e2840cdd21fbc0535bbd027bb6b436c3164518b710a8f7ecae68ff4b6a12980e83864
6
+ metadata.gz: 83b61249a1888eed186c7faa7814c9a07f28fa9268d8d7fad1dfc0b9d7f4b304d4c835116fce6e32b6e651d7a2fc12731417c52922daae8ad9759419b32d2344
7
+ data.tar.gz: 5107f90f035a11e3ab17c8718f759c6869ea24d3c195dafb1845d546587244ad1e6f82dca7da5b505055cefdafa97f7b0e6390f17374e2458f773e54f7f9dd75
data/README.md CHANGED
@@ -115,8 +115,8 @@ From inside your app you may find this class useful:
115
115
 
116
116
  ```ruby
117
117
  require 'pgtk/pool'
118
- pgsql = Pgtk::Pool.new(Pgtk::Wire::Yaml.new('config.yml'))
119
- pgsql.start!(5) # Start it with five simultaneous connections
118
+ pgsql = Pgtk::Pool.new(Pgtk::Wire::Yaml.new('config.yml'), max: 5)
119
+ pgsql.start! # Start it with five simultaneous connections
120
120
  ```
121
121
 
122
122
  You can also let it pick the connection parameters from the environment
@@ -19,8 +19,8 @@ require_relative '../pgtk'
19
19
  # Basic usage:
20
20
  #
21
21
  # # Create and configure a regular pool
22
- # pool = Pgtk::Pool.new(wire)
23
- # pool.start!(4)
22
+ # pool = Pgtk::Pool.new(wire, max: 4)
23
+ # pool.start!
24
24
  #
25
25
  # # Wrap the pool in an impatient decorator with a 2-second timeout
26
26
  # impatient = Pgtk::Impatient.new(pool, 2)
@@ -69,8 +69,8 @@ class Pgtk::Impatient
69
69
  end
70
70
 
71
71
  # Start a new connection pool with the given arguments.
72
- def start!(*)
73
- @pool.start!(*)
72
+ def start!
73
+ @pool.start!
74
74
  end
75
75
 
76
76
  # Get the version of PostgreSQL server.
data/lib/pgtk/pool.rb CHANGED
@@ -29,8 +29,8 @@ require_relative 'wire'
29
29
  # )
30
30
  #
31
31
  # # Create and start a connection pool with 4 connections
32
- # pool = Pgtk::Pool.new(wire)
33
- # pool.start!(4)
32
+ # pool = Pgtk::Pool.new(wire, max: 4)
33
+ # pool.start!
34
34
  #
35
35
  # # Execute a simple query
36
36
  # pool.exec('SELECT * FROM users')
@@ -51,11 +51,14 @@ class Pgtk::Pool
51
51
  # Constructor.
52
52
  #
53
53
  # @param [Pgtk::Wire] wire The wire
54
+ # @param [Integer] max Total amount of PostgreSQL connections in the pool
54
55
  # @param [Object] log The log
55
- def initialize(wire, log: Loog::NULL)
56
+ def initialize(wire, max: 8, log: Loog::NULL)
56
57
  @wire = wire
58
+ @max = max
57
59
  @log = log
58
- @pool = IterableQueue.new
60
+ @pool = IterableQueue.new(max)
61
+ @started = false
59
62
  end
60
63
 
61
64
  # Get the version of PostgreSQL server.
@@ -120,13 +123,13 @@ class Pgtk::Pool
120
123
  # keep in mind that not all servers will allow you to have many connections
121
124
  # open at the same time. For example, Heroku free PostgreSQL database
122
125
  # allows only one connection open.
123
- #
124
- # @param [Integer] max Total amount of PostgreSQL connections in the pool
125
- def start!(max = 8)
126
- max.times do
126
+ def start!
127
+ return if @started
128
+ @max.times do
127
129
  @pool << @wire.connection
128
130
  end
129
- @log.debug("PostgreSQL pool started with #{max} connections")
131
+ @started = true
132
+ @log.debug("PostgreSQL pool started with #{@max} connections")
130
133
  end
131
134
 
132
135
  # Make a query and return the result as an array of hashes. For example,
@@ -219,24 +222,43 @@ class Pgtk::Pool
219
222
  #
220
223
  # This class is used internally by Pool to store database connections
221
224
  # and provide the ability to iterate over them for inspection purposes.
225
+ #
226
+ # The queue is bounded by size. When an item is taken out, it remains in
227
+ # the internal array but is marked as "taken". When returned, it's placed
228
+ # back in its original slot and marked as available.
222
229
  class IterableQueue
223
- def initialize
230
+ def initialize(size)
231
+ @size = size
224
232
  @items = []
233
+ @taken = []
225
234
  @mutex = Mutex.new
226
235
  @condition = ConditionVariable.new
227
236
  end
228
237
 
229
238
  def <<(item)
230
239
  @mutex.synchronize do
231
- @items << item
240
+ if @items.size < @size
241
+ @items << item
242
+ @taken << false
243
+ else
244
+ index = @items.index(item)
245
+ if index.nil?
246
+ index = @taken.index(true)
247
+ raise 'No taken slot found' if index.nil?
248
+ @items[index] = item
249
+ end
250
+ @taken[index] = false
251
+ end
232
252
  @condition.signal
233
253
  end
234
254
  end
235
255
 
236
256
  def pop
237
257
  @mutex.synchronize do
238
- @condition.wait(@mutex) while @items.empty?
239
- @items.shift
258
+ @condition.wait(@mutex) while @taken.all? || @items.empty?
259
+ index = @taken.index(false)
260
+ @taken[index] = true
261
+ @items[index]
240
262
  end
241
263
  end
242
264
 
data/lib/pgtk/retry.rb CHANGED
@@ -16,8 +16,8 @@ require_relative '../pgtk'
16
16
  # Basic usage:
17
17
  #
18
18
  # # Create and configure a regular pool
19
- # pool = Pgtk::Pool.new(wire)
20
- # pool.start!(4)
19
+ # pool = Pgtk::Pool.new(wire, max: 4)
20
+ # pool.start!
21
21
  #
22
22
  # # Wrap the pool in a retry decorator with 3 attempts
23
23
  # retry_pool = Pgtk::Retry.new(pool, attempts: 3)
@@ -58,8 +58,8 @@ class Pgtk::Retry
58
58
  end
59
59
 
60
60
  # Start a new connection pool with the given arguments.
61
- def start!(*)
62
- @pool.start!(*)
61
+ def start!
62
+ @pool.start!
63
63
  end
64
64
 
65
65
  # Get the version of PostgreSQL server.
data/lib/pgtk/spy.rb CHANGED
@@ -19,8 +19,8 @@ require_relative 'wire'
19
19
  # Basic usage:
20
20
  #
21
21
  # # Create and configure a regular pool
22
- # pool = Pgtk::Pool.new(wire)
23
- # pool.start!(4)
22
+ # pool = Pgtk::Pool.new(wire, max: 4)
23
+ # pool.start!
24
24
  #
25
25
  # # Wrap the pool in a spy that tracks all executed queries
26
26
  # queries = []
@@ -57,8 +57,8 @@ class Pgtk::Spy
57
57
  end
58
58
 
59
59
  # Start a new connection pool with the given arguments.
60
- def start!(*)
61
- @pool.start!(*)
60
+ def start!
61
+ @pool.start!
62
62
  end
63
63
 
64
64
  # Get the version of PostgreSQL server.
data/lib/pgtk/stash.rb CHANGED
@@ -64,9 +64,9 @@ class Pgtk::Stash
64
64
  end
65
65
 
66
66
  # Start a new connection pool with the given arguments.
67
- def start!(*)
67
+ def start!
68
68
  launch!
69
- @pool.start!(*)
69
+ @pool.start!
70
70
  end
71
71
 
72
72
  # Get the PostgreSQL server version.
@@ -136,7 +136,7 @@ class Pgtk::Stash
136
136
  @stash[:tables][t].append(pure).uniq!
137
137
  end
138
138
  @stash[:queries][pure] ||= {}
139
- @stash[:queries][pure][key] = { ret:, params:, result: }
139
+ @stash[:queries][pure][key] = { ret:, params:, result:, used: Time.now }
140
140
  end
141
141
  end
142
142
  end
@@ -144,6 +144,7 @@ class Pgtk::Stash
144
144
  @entrance.with_write_lock do
145
145
  @stash[:queries][pure][key][:popularity] ||= 0
146
146
  @stash[:queries][pure][key][:popularity] += 1
147
+ @stash[:queries][pure][key][:used] = Time.now
147
148
  end
148
149
  end
149
150
  end
@@ -175,12 +176,11 @@ class Pgtk::Stash
175
176
 
176
177
  def launch!
177
178
  raise 'Cannot launch multiple times on same cache data' unless @launched.make_true
178
- Concurrent::TimerTask.execute(execution_interval: 60 * 60, executor: @tpool) do
179
+ retire = 60 * 60
180
+ Concurrent::TimerTask.execute(execution_interval: retire, executor: @tpool) do
179
181
  @entrance.with_write_lock do
180
182
  @stash[:queries].each_key do |q|
181
- @stash[:queries][q].each_key do |k|
182
- @stash[:queries][q][k][:popularity] = 0
183
- end
183
+ @stash[:queries][q].delete_if { |_, h| h[:used] < Time.now - retire }
184
184
  end
185
185
  end
186
186
  end
data/lib/pgtk/version.rb CHANGED
@@ -11,5 +11,5 @@ require_relative '../pgtk'
11
11
  # License:: MIT
12
12
  module Pgtk
13
13
  # Current version of the library.
14
- VERSION = '0.22.1'
14
+ VERSION = '0.23.0'
15
15
  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.22.1
4
+ version: 0.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko