pgtk 0.31.5 → 0.31.6
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 +19 -6
- data/lib/pgtk/retry.rb +12 -5
- 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: 117996e543551102eebf83be228996818dbfdbfc324f7f45b5d346a57d39fc1a
|
|
4
|
+
data.tar.gz: 22bd0738c88e60ed29ad3310a87755709dc4512d6b92932c88442253c29847f3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0ae1185f452400e2f2f984e5266bdb9c6b8dc7d92b798eb714d18903949f230d8707911b562431984f4fa52178dc52198bfeac5b24d2aaeff918ee0b36804541
|
|
7
|
+
data.tar.gz: 4b8fc6853d556967173556f0067a6c611b4a93be0440c31659b0892f7d944606fbc19f15e27b1361b06cacea99e268bb81008893dfcc910e56103c10d0b9c42c
|
data/lib/pgtk/pool.rb
CHANGED
|
@@ -209,6 +209,7 @@ class Pgtk::Pool
|
|
|
209
209
|
@timeout = timeout
|
|
210
210
|
@items = []
|
|
211
211
|
@taken = []
|
|
212
|
+
@free = []
|
|
212
213
|
@mutex = Mutex.new
|
|
213
214
|
@condition = ConditionVariable.new
|
|
214
215
|
end
|
|
@@ -218,6 +219,7 @@ class Pgtk::Pool
|
|
|
218
219
|
if @items.size < @size
|
|
219
220
|
@items << item
|
|
220
221
|
@taken << false
|
|
222
|
+
@free << (@items.size - 1)
|
|
221
223
|
else
|
|
222
224
|
index = @items.index(item)
|
|
223
225
|
if index.nil?
|
|
@@ -226,6 +228,7 @@ class Pgtk::Pool
|
|
|
226
228
|
@items[index] = item
|
|
227
229
|
end
|
|
228
230
|
@taken[index] = false
|
|
231
|
+
@free << index
|
|
229
232
|
end
|
|
230
233
|
@condition.signal
|
|
231
234
|
end
|
|
@@ -234,12 +237,12 @@ class Pgtk::Pool
|
|
|
234
237
|
def pop
|
|
235
238
|
@mutex.synchronize do
|
|
236
239
|
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout
|
|
237
|
-
while @
|
|
240
|
+
while @free.empty?
|
|
238
241
|
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
239
242
|
raise(Busy, "No free connection appeared in the pool after #{@timeout}s of waiting") if remaining <= 0
|
|
240
243
|
@condition.wait(@mutex, remaining)
|
|
241
244
|
end
|
|
242
|
-
index = @
|
|
245
|
+
index = @free.shift
|
|
243
246
|
@taken[index] = true
|
|
244
247
|
@items[index]
|
|
245
248
|
end
|
|
@@ -274,6 +277,7 @@ class Pgtk::Pool
|
|
|
274
277
|
start = Time.now
|
|
275
278
|
sql = query.is_a?(Array) ? query.join(' ') : query
|
|
276
279
|
@conn.instance_variable_set(:@pgtk_last_query, sql)
|
|
280
|
+
@conn.instance_variable_set(:@pgtk_started_at, start)
|
|
277
281
|
begin
|
|
278
282
|
out =
|
|
279
283
|
if args.empty?
|
|
@@ -351,6 +355,7 @@ class Pgtk::Pool
|
|
|
351
355
|
transactions = { PG::Constants::PQTRANS_IDLE => 'IDLE', PG::Constants::PQTRANS_ACTIVE => 'ACTIVE',
|
|
352
356
|
PG::Constants::PQTRANS_INTRANS => 'INTRANS', PG::Constants::PQTRANS_INERROR => 'INERROR',
|
|
353
357
|
PG::Constants::PQTRANS_UNKNOWN => 'UNKNOWN' }
|
|
358
|
+
conn.instance_variable_set(:@pgtk_pid, conn.backend_pid)
|
|
354
359
|
parts = [
|
|
355
360
|
' ',
|
|
356
361
|
"##{conn.backend_pid}",
|
|
@@ -358,27 +363,35 @@ class Pgtk::Pool
|
|
|
358
363
|
statuses.fetch(conn.status, "status=#{conn.status}"),
|
|
359
364
|
transactions.fetch(conn.transaction_status, "transaction_status=#{conn.transaction_status}")
|
|
360
365
|
]
|
|
366
|
+
if conn.transaction_status != PG::Constants::PQTRANS_IDLE
|
|
367
|
+
started = conn.instance_variable_get(:@pgtk_started_at)
|
|
368
|
+
parts << started.ago if started
|
|
369
|
+
end
|
|
361
370
|
if conn.transaction_status == PG::Constants::PQTRANS_ACTIVE
|
|
362
371
|
running = conn.instance_variable_get(:@pgtk_last_query)
|
|
363
372
|
parts << "running: #{running.gsub(/\s+/, ' ').strip.ellipsized(60)}" if running
|
|
364
373
|
end
|
|
365
374
|
parts.join(' ')
|
|
366
375
|
rescue PG::ConnectionBad => e
|
|
367
|
-
|
|
376
|
+
pid = conn.instance_variable_get(:@pgtk_pid)
|
|
377
|
+
parts = [' ']
|
|
378
|
+
parts << (pid ? "##{pid}" : '#?')
|
|
379
|
+
parts << e.message.gsub(/\s+/, ' ').strip
|
|
368
380
|
closed = conn.instance_variable_get(:@pgtk_closed_at)
|
|
369
381
|
parts << "#{closed.ago} ago" if closed
|
|
370
382
|
reason = conn.instance_variable_get(:@pgtk_closed_reason)
|
|
371
|
-
parts << "because: #{reason}" if reason
|
|
383
|
+
parts << "because: #{reason.gsub(/\s+/, ' ').strip}" if reason
|
|
372
384
|
last = conn.instance_variable_get(:@pgtk_last_query)
|
|
373
385
|
parts << "last query: #{last.gsub(/\s+/, ' ').strip.ellipsized(60)}" if last
|
|
374
|
-
parts.join(', ')
|
|
386
|
+
"#{parts.shift} #{parts.shift} #{parts.join(', ')}"
|
|
375
387
|
end
|
|
376
388
|
|
|
377
389
|
def renew(conn, reason)
|
|
378
390
|
begin
|
|
379
391
|
unless conn.finished?
|
|
392
|
+
conn.instance_variable_set(:@pgtk_pid, conn.backend_pid)
|
|
380
393
|
conn.instance_variable_set(:@pgtk_closed_at, Time.now)
|
|
381
|
-
conn.instance_variable_set(:@pgtk_closed_reason, reason)
|
|
394
|
+
conn.instance_variable_set(:@pgtk_closed_reason, reason.gsub(/\s+/, ' ').strip)
|
|
382
395
|
conn.close
|
|
383
396
|
end
|
|
384
397
|
rescue StandardError => e
|
data/lib/pgtk/retry.rb
CHANGED
|
@@ -85,11 +85,14 @@ class Pgtk::Retry
|
|
|
85
85
|
].join("\n")
|
|
86
86
|
end
|
|
87
87
|
|
|
88
|
-
# Execute a SQL query with automatic retry
|
|
89
|
-
#
|
|
90
|
-
#
|
|
91
|
-
#
|
|
92
|
-
#
|
|
88
|
+
# Execute a SQL query with automatic retry on transient failures.
|
|
89
|
+
# SELECT queries are retried on any error, since reads are idempotent.
|
|
90
|
+
# Non-SELECT queries are retried only on PG::ConnectionBad, since by
|
|
91
|
+
# definition the query never reached the server, so retrying cannot
|
|
92
|
+
# duplicate a write. Other errors on writes propagate immediately,
|
|
93
|
+
# because a failure may occur after the server received the query but
|
|
94
|
+
# before the acknowledgement reached the client, and retrying a
|
|
95
|
+
# non-idempotent write could duplicate it.
|
|
93
96
|
#
|
|
94
97
|
# @param [String] sql The SQL query with params inside (possibly)
|
|
95
98
|
# @return [Array] Result rows
|
|
@@ -98,6 +101,10 @@ class Pgtk::Retry
|
|
|
98
101
|
attempt = 0
|
|
99
102
|
begin
|
|
100
103
|
@pool.exec(sql, *)
|
|
104
|
+
rescue PG::ConnectionBad => e
|
|
105
|
+
attempt += 1
|
|
106
|
+
raise(Exhausted, "Retry gave up after #{@attempts} attempts: #{e.message}") if attempt >= @attempts
|
|
107
|
+
retry
|
|
101
108
|
rescue StandardError, Pgtk::Impatient::TooSlow => e
|
|
102
109
|
raise(e) unless query.strip.upcase.start_with?('SELECT')
|
|
103
110
|
attempt += 1
|
data/lib/pgtk/version.rb
CHANGED