tina4ruby 3.13.167 → 3.13.169
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/CHANGELOG.md +35 -0
- data/lib/tina4/cli.rb +204 -51
- data/lib/tina4/database.rb +324 -43
- data/lib/tina4/database_adapter.rb +8 -0
- data/lib/tina4/database_url.rb +118 -9
- data/lib/tina4/dispatch_pipeline.rb +9 -3
- data/lib/tina4/drivers/firebird_driver.rb +6 -0
- data/lib/tina4/drivers/mssql_driver.rb +17 -1
- data/lib/tina4/drivers/mysql_driver.rb +23 -0
- data/lib/tina4/drivers/postgres_driver.rb +62 -0
- data/lib/tina4/drivers/sqlite_driver.rb +9 -1
- data/lib/tina4/sso.rb +62 -11
- data/lib/tina4/sso_jwks.rb +184 -0
- data/lib/tina4/version.rb +1 -1
- data/lib/tina4/websocket.rb +71 -16
- data/lib/tina4.rb +42 -13
- metadata +3 -2
data/lib/tina4/database.rb
CHANGED
|
@@ -22,46 +22,85 @@ module Tina4
|
|
|
22
22
|
|
|
23
23
|
# Thread-safe connection pool with round-robin rotation.
|
|
24
24
|
# Connections are created lazily on first use.
|
|
25
|
+
# Every connection stayed lent out for longer than TINA4_DB_POOL_TIMEOUT
|
|
26
|
+
# (ADR-0074). The message names TINA4_DB_POOL, so the fix travels with the
|
|
27
|
+
# failure. A RuntimeError, so existing `rescue RuntimeError` still catches it.
|
|
28
|
+
class DatabasePoolExhausted < RuntimeError; end
|
|
29
|
+
|
|
25
30
|
class ConnectionPool
|
|
26
31
|
attr_reader :size
|
|
27
32
|
|
|
28
|
-
|
|
33
|
+
#: ADR-0074, parity with Python: a borrower waits this long for a connection.
|
|
34
|
+
DEFAULT_TIMEOUT_SECONDS = 30.0
|
|
35
|
+
|
|
36
|
+
# Seconds a checkout may wait: TINA4_DB_POOL_TIMEOUT, default 30; 0 or less
|
|
37
|
+
# waits forever (nil).
|
|
38
|
+
def self.timeout_seconds
|
|
39
|
+
raw = ENV["TINA4_DB_POOL_TIMEOUT"].to_s.strip
|
|
40
|
+
return DEFAULT_TIMEOUT_SECONDS if raw.empty?
|
|
41
|
+
|
|
42
|
+
seconds = Float(raw, exception: false)
|
|
43
|
+
if seconds.nil?
|
|
44
|
+
Tina4::Log.warning("TINA4_DB_POOL_TIMEOUT=#{raw.inspect} is not a number of seconds - using #{DEFAULT_TIMEOUT_SECONDS}")
|
|
45
|
+
return DEFAULT_TIMEOUT_SECONDS
|
|
46
|
+
end
|
|
47
|
+
seconds <= 0 ? nil : seconds
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# `source:` lends the driver a block returns instead of opening connections -
|
|
51
|
+
# the single-connection mode (pool 0) is a pool of exactly that one driver,
|
|
52
|
+
# LENT like any other (ADR-0074). It used to be shared, so a concurrent
|
|
53
|
+
# request's statement ran inside another request's open transaction and its
|
|
54
|
+
# auto-commit committed that transaction (tina4-nodejs#231). With no factory
|
|
55
|
+
# a discarded driver is kept: there is nothing to reopen it with.
|
|
56
|
+
# `seed:` is a driver already open (the Database's first connection), lent
|
|
57
|
+
# from slot 0; the other slots open only when every open one is lent out.
|
|
58
|
+
# `connect_hooks:` (ADR-0110) run on every driver this pool connects itself.
|
|
59
|
+
def initialize(pool_size, driver_factory:, connection_string:, username: nil, password: nil, source: nil,
|
|
60
|
+
seed: nil, connect_hooks: [])
|
|
29
61
|
@pool_size = pool_size
|
|
62
|
+
@connect_hooks = connect_hooks
|
|
30
63
|
@driver_factory = driver_factory
|
|
31
64
|
@connection_string = connection_string
|
|
32
65
|
@username = username
|
|
33
66
|
@password = password
|
|
67
|
+
@source = source
|
|
34
68
|
@drivers = Array.new(pool_size) # nil slots — lazy creation
|
|
69
|
+
@drivers[0] = seed if seed
|
|
35
70
|
@index = 0
|
|
36
71
|
@owners = {}
|
|
37
72
|
@mutex = Mutex.new
|
|
73
|
+
@returned = ConditionVariable.new
|
|
38
74
|
end
|
|
39
75
|
|
|
40
76
|
# A lease belongs to one thread until checkin; rotating never exposes a
|
|
41
|
-
# connection with another caller's uncommitted work.
|
|
42
|
-
#
|
|
43
|
-
|
|
77
|
+
# connection with another caller's uncommitted work. When every connection
|
|
78
|
+
# is lent out the borrower WAITS for one to come back, up to
|
|
79
|
+
# TINA4_DB_POOL_TIMEOUT, then raises DatabasePoolExhausted (ADR-0074, parity
|
|
80
|
+
# with Python). A thread's own transaction never waits on itself: inside one,
|
|
81
|
+
# every call reuses the pinned driver without a checkout. `wait: false` is the
|
|
82
|
+
# non-blocking form for peeks.
|
|
83
|
+
def checkout(wait: true)
|
|
84
|
+
timeout = wait ? self.class.timeout_seconds : 0
|
|
85
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
44
86
|
@mutex.synchronize do
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
@owners[idx] = Thread.current
|
|
55
|
-
return @drivers[idx]
|
|
87
|
+
loop do
|
|
88
|
+
driver = take_locked
|
|
89
|
+
return driver if driver
|
|
90
|
+
|
|
91
|
+
waited = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
92
|
+
remaining = timeout.nil? ? nil : timeout - waited
|
|
93
|
+
raise exhausted(waited, timeout) if remaining && remaining <= 0
|
|
94
|
+
|
|
95
|
+
@returned.wait(@mutex, remaining)
|
|
56
96
|
end
|
|
57
|
-
raise "Database connection pool exhausted"
|
|
58
97
|
end
|
|
59
98
|
end
|
|
60
99
|
|
|
61
100
|
# Compatibility getter for adapter metadata. It never exposes a leased
|
|
62
101
|
# connection; callers performing driver I/O directly must checkout/checkin.
|
|
63
102
|
def peek
|
|
64
|
-
driver = checkout
|
|
103
|
+
driver = checkout(wait: false)
|
|
65
104
|
checkin(driver)
|
|
66
105
|
driver
|
|
67
106
|
end
|
|
@@ -70,11 +109,12 @@ module Tina4
|
|
|
70
109
|
@mutex.synchronize do
|
|
71
110
|
idx = @drivers.index { |candidate| candidate.equal?(driver) }
|
|
72
111
|
raise ArgumentError, "Driver is not leased by this thread" unless idx && @owners[idx].equal?(Thread.current)
|
|
73
|
-
if discard
|
|
112
|
+
if discard && @driver_factory
|
|
74
113
|
driver.close rescue nil
|
|
75
114
|
@drivers[idx] = nil
|
|
76
115
|
end
|
|
77
116
|
@owners.delete(idx)
|
|
117
|
+
@returned.signal
|
|
78
118
|
end
|
|
79
119
|
end
|
|
80
120
|
|
|
@@ -91,6 +131,11 @@ module Tina4
|
|
|
91
131
|
end
|
|
92
132
|
end
|
|
93
133
|
|
|
134
|
+
# Every connection opened so far (lent or idle).
|
|
135
|
+
def open_drivers
|
|
136
|
+
@mutex.synchronize { @drivers.compact }
|
|
137
|
+
end
|
|
138
|
+
|
|
94
139
|
# Number of connections that have been created.
|
|
95
140
|
def active_count
|
|
96
141
|
@mutex.synchronize do
|
|
@@ -101,6 +146,73 @@ module Tina4
|
|
|
101
146
|
def size
|
|
102
147
|
@pool_size
|
|
103
148
|
end
|
|
149
|
+
|
|
150
|
+
private
|
|
151
|
+
|
|
152
|
+
# A free driver lent to this thread, or nil. Caller holds @mutex.
|
|
153
|
+
# An open, free driver first (round-robin); a new connection only when every
|
|
154
|
+
# open one is lent out - so a pool of 10 serving one request at a time holds
|
|
155
|
+
# one connection, not ten.
|
|
156
|
+
def take_locked
|
|
157
|
+
# The single connection is whatever driver the Database holds now.
|
|
158
|
+
@drivers[0] = @source.call if @source
|
|
159
|
+
@pool_size.times do
|
|
160
|
+
idx = (@index + _1) % @pool_size
|
|
161
|
+
next if @owners.key?(idx) || @drivers[idx].nil?
|
|
162
|
+
|
|
163
|
+
@index = (idx + 1) % @pool_size
|
|
164
|
+
@owners[idx] = Thread.current
|
|
165
|
+
return @drivers[idx]
|
|
166
|
+
end
|
|
167
|
+
return nil unless @driver_factory
|
|
168
|
+
|
|
169
|
+
idx = @drivers.index(nil)
|
|
170
|
+
return nil unless idx
|
|
171
|
+
|
|
172
|
+
driver = @driver_factory.call
|
|
173
|
+
# A factory without a connection string connects (and hooks) for itself.
|
|
174
|
+
if @connection_string
|
|
175
|
+
driver.connect(@connection_string, username: @username, password: @password)
|
|
176
|
+
Tina4::Database.attach_connect_hooks(driver, @connect_hooks)
|
|
177
|
+
end
|
|
178
|
+
@drivers[idx] = driver
|
|
179
|
+
@owners[idx] = Thread.current
|
|
180
|
+
driver
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
public
|
|
184
|
+
|
|
185
|
+
# In a forked child: never use a driver the parent opened (ADR-0074). Each
|
|
186
|
+
# is LEFT - neutered so freeing it says nothing on the parent's session, and
|
|
187
|
+
# held for the rest of the process - and its slot reopens on first use. The
|
|
188
|
+
# mutex and condition variable start fresh: one held by a thread that did
|
|
189
|
+
# not survive the fork would never be released. `keep: true` keeps the
|
|
190
|
+
# drivers (an in-memory SQLite database exists only in its handle).
|
|
191
|
+
def leave_inherited_connections!(keep: false)
|
|
192
|
+
@mutex = Mutex.new
|
|
193
|
+
@returned = ConditionVariable.new
|
|
194
|
+
@owners = {}
|
|
195
|
+
@index = 0
|
|
196
|
+
return if keep
|
|
197
|
+
|
|
198
|
+
@drivers.each_with_index do |driver, idx|
|
|
199
|
+
next unless driver
|
|
200
|
+
|
|
201
|
+
Tina4::Database.leave_inherited(driver)
|
|
202
|
+
@drivers[idx] = nil
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
private
|
|
207
|
+
|
|
208
|
+
def exhausted(waited, timeout)
|
|
209
|
+
DatabasePoolExhausted.new(
|
|
210
|
+
"Database pool exhausted: all #{@pool_size} connection(s) stayed in use for " \
|
|
211
|
+
"#{format('%.1f', waited)}s (TINA4_DB_POOL=#{@driver_factory ? @pool_size : 0}, " \
|
|
212
|
+
"TINA4_DB_POOL_TIMEOUT=#{timeout.nil? ? 'none' : timeout}s). Raise TINA4_DB_POOL, " \
|
|
213
|
+
"commit or roll back long transactions sooner, or raise TINA4_DB_POOL_TIMEOUT."
|
|
214
|
+
)
|
|
215
|
+
end
|
|
104
216
|
end
|
|
105
217
|
|
|
106
218
|
class Database
|
|
@@ -114,7 +226,54 @@ module Tina4
|
|
|
114
226
|
@instances = []
|
|
115
227
|
@instances_mutex = Mutex.new
|
|
116
228
|
|
|
229
|
+
#: ADR-0074: the default pool size, the same in every framework.
|
|
230
|
+
DEFAULT_POOL_SIZE = 10
|
|
231
|
+
|
|
232
|
+
#: Drivers a forked child inherited and left: never used, never freed.
|
|
233
|
+
INHERITED_CONNECTIONS = []
|
|
234
|
+
|
|
117
235
|
class << self
|
|
236
|
+
# The pool size: explicit pool:, else TINA4_DB_POOL, else 10 (ADR-0074).
|
|
237
|
+
# A negative number is the single connection (as in Python); a value that
|
|
238
|
+
# is not a whole number warns and uses the default.
|
|
239
|
+
def resolve_pool_size(pool)
|
|
240
|
+
return [pool.to_i, 0].max unless pool.nil?
|
|
241
|
+
|
|
242
|
+
raw = ENV["TINA4_DB_POOL"].to_s.strip
|
|
243
|
+
return DEFAULT_POOL_SIZE if raw.empty?
|
|
244
|
+
|
|
245
|
+
value = Integer(raw, exception: false)
|
|
246
|
+
if value.nil?
|
|
247
|
+
Tina4::Log.warning("TINA4_DB_POOL=#{raw.inspect} is not a whole number - using #{DEFAULT_POOL_SIZE}")
|
|
248
|
+
return DEFAULT_POOL_SIZE
|
|
249
|
+
end
|
|
250
|
+
[value, 0].max
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
# Leave a driver a forked child inherited: neuter it so freeing it never
|
|
254
|
+
# speaks on the parent's session (pg's terminate, mysql2's COM_QUIT), then
|
|
255
|
+
# hold it for the rest of the process.
|
|
256
|
+
def leave_inherited(driver)
|
|
257
|
+
driver.leave_after_fork! if driver.respond_to?(:leave_after_fork!)
|
|
258
|
+
rescue StandardError
|
|
259
|
+
nil
|
|
260
|
+
ensure
|
|
261
|
+
INHERITED_CONNECTIONS << driver
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# Process._fork hook, in the child: every live Database leaves the
|
|
265
|
+
# connections it inherited. The registry's mutex may be held by a thread
|
|
266
|
+
# that did not survive the fork, so it starts fresh.
|
|
267
|
+
def after_fork_in_child
|
|
268
|
+
@instances_mutex = Mutex.new
|
|
269
|
+
@instances.each do |ref|
|
|
270
|
+
db = ref.__getobj__
|
|
271
|
+
db.leave_inherited_connections!
|
|
272
|
+
rescue WeakRef::RefError
|
|
273
|
+
next
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
118
277
|
# Register a live connection in the class-level WeakRef registry.
|
|
119
278
|
def register_instance(db)
|
|
120
279
|
@instances_mutex.synchronize do
|
|
@@ -406,14 +565,13 @@ module Tina4
|
|
|
406
565
|
@username = username || ENV["TINA4_DATABASE_USERNAME"]
|
|
407
566
|
@password = password || ENV["TINA4_DATABASE_PASSWORD"]
|
|
408
567
|
@driver_name = driver_name || detect_driver(@connection_string)
|
|
409
|
-
# TINA4_DB_POOL
|
|
410
|
-
#
|
|
411
|
-
@pool_size =
|
|
412
|
-
|
|
413
|
-
else
|
|
414
|
-
pool
|
|
415
|
-
end
|
|
568
|
+
# ADR-0074: pool: > TINA4_DB_POOL > 10. 0 is the single connection, lent
|
|
569
|
+
# to one thread at a time; N > 0 opens up to N, only as concurrency needs.
|
|
570
|
+
@pool_size = self.class.resolve_pool_size(pool)
|
|
571
|
+
@pool_size = 0 if single_connection_only?
|
|
416
572
|
@connected = false
|
|
573
|
+
# Connect hooks (ADR-0110), shared with every driver this database opens.
|
|
574
|
+
@connect_hooks = []
|
|
417
575
|
# Set by #connect when connecting fails; re-raised with context by
|
|
418
576
|
# #current_driver so the first real call says what went wrong.
|
|
419
577
|
@connect_error = nil
|
|
@@ -499,27 +657,32 @@ module Tina4
|
|
|
499
657
|
Tina4::Database.register_instance(self)
|
|
500
658
|
|
|
501
659
|
if @pool_size > 0
|
|
502
|
-
# Pooled mode
|
|
660
|
+
# Pooled mode. The first connection opens now, exactly as the single
|
|
661
|
+
# mode's does (it names the target in the log and surfaces a connect
|
|
662
|
+
# failure); the rest open only when every open one is lent out.
|
|
663
|
+
@driver = create_driver
|
|
664
|
+
connect
|
|
503
665
|
@pool = ConnectionPool.new(
|
|
504
666
|
@pool_size,
|
|
505
|
-
driver_factory: method(:
|
|
506
|
-
connection_string:
|
|
507
|
-
|
|
508
|
-
|
|
667
|
+
driver_factory: method(:open_pooled_driver),
|
|
668
|
+
connection_string: nil, # the factory connects and attaches the hooks
|
|
669
|
+
seed: (@connect_error ? nil : @driver),
|
|
670
|
+
connect_hooks: @connect_hooks
|
|
509
671
|
)
|
|
510
|
-
@driver = nil
|
|
511
|
-
@connected = true
|
|
512
672
|
else
|
|
513
|
-
# Single-connection mode
|
|
673
|
+
# Single-connection mode: one driver, lent exclusively (ADR-0074).
|
|
514
674
|
@pool = nil
|
|
515
675
|
@driver = create_driver
|
|
516
676
|
connect
|
|
677
|
+
@single_lease = ConnectionPool.new(1, driver_factory: nil, connection_string: nil, source: -> { @driver })
|
|
517
678
|
end
|
|
518
679
|
end
|
|
519
680
|
|
|
520
681
|
def connect
|
|
521
682
|
@driver.connect(driver_connection_string, username: @username, password: @password)
|
|
683
|
+
Tina4::Database.attach_connect_hooks(@driver, @connect_hooks)
|
|
522
684
|
@connected = true
|
|
685
|
+
@closed = false
|
|
523
686
|
|
|
524
687
|
# Push the resolved autocommit setting down to the driver when it exposes a
|
|
525
688
|
# native toggle (default ON — see @autocommit in #initialize). The
|
|
@@ -550,6 +713,7 @@ module Tina4
|
|
|
550
713
|
|
|
551
714
|
def close
|
|
552
715
|
if @pool
|
|
716
|
+
@closed = true
|
|
553
717
|
@pool.close_all
|
|
554
718
|
elsif @driver && @connected
|
|
555
719
|
@driver.close
|
|
@@ -572,10 +736,12 @@ module Tina4
|
|
|
572
736
|
# Fail with the REAL reason, at the point of use. Without this the caller
|
|
573
737
|
# gets a nil dereference from deep inside the driver and has to guess.
|
|
574
738
|
raise Tina4::DatabaseConnectionError, connect_error_message if @connect_error
|
|
739
|
+
raise_if_closed
|
|
575
740
|
|
|
576
741
|
if @pool
|
|
577
742
|
@pool.peek
|
|
578
743
|
else
|
|
744
|
+
reopen_after_fork
|
|
579
745
|
@driver
|
|
580
746
|
end
|
|
581
747
|
end
|
|
@@ -1238,7 +1404,7 @@ module Tina4
|
|
|
1238
1404
|
Thread.current[@tx_depth_key] = depth + 1
|
|
1239
1405
|
return
|
|
1240
1406
|
end
|
|
1241
|
-
drv = Thread.current[@operation_driver_key] ||
|
|
1407
|
+
drv = Thread.current[@operation_driver_key] || lend_driver
|
|
1242
1408
|
Thread.current[@tx_pin_key] = drv
|
|
1243
1409
|
Thread.current[@tx_depth_key] = 1
|
|
1244
1410
|
begin
|
|
@@ -1355,16 +1521,17 @@ module Tina4
|
|
|
1355
1521
|
end
|
|
1356
1522
|
end
|
|
1357
1523
|
|
|
1358
|
-
# Check out a driver from the pool (or
|
|
1524
|
+
# Check out a driver from the pool (or the single connection), lent to this
|
|
1525
|
+
# thread until #checkin.
|
|
1359
1526
|
def checkout
|
|
1360
|
-
|
|
1527
|
+
lend_driver
|
|
1361
1528
|
end
|
|
1362
1529
|
|
|
1363
1530
|
# Explicit borrowers must return their own lease. Transaction leases are
|
|
1364
1531
|
# released only by commit/rollback, never by an unrelated checkin.
|
|
1365
1532
|
def checkin(driver)
|
|
1366
1533
|
raise ArgumentError, "Cannot check in an active transaction" if driver.equal?(Thread.current[@tx_pin_key])
|
|
1367
|
-
|
|
1534
|
+
lender.checkin(driver)
|
|
1368
1535
|
end
|
|
1369
1536
|
|
|
1370
1537
|
# Close all pooled connections (or the single connection).
|
|
@@ -1450,9 +1617,8 @@ module Tina4
|
|
|
1450
1617
|
# Keep the entire public operation (including nested metadata/query calls)
|
|
1451
1618
|
# on one lease. A transaction may adopt it and keep it beyond this scope.
|
|
1452
1619
|
def with_operation_driver
|
|
1453
|
-
return yield unless @pool
|
|
1454
1620
|
return yield if Thread.current[@tx_pin_key] || Thread.current[@operation_driver_key]
|
|
1455
|
-
driver =
|
|
1621
|
+
driver = lend_driver
|
|
1456
1622
|
Thread.current[@operation_driver_key] = driver
|
|
1457
1623
|
discard = false
|
|
1458
1624
|
begin
|
|
@@ -1470,7 +1636,7 @@ module Tina4
|
|
|
1470
1636
|
raise
|
|
1471
1637
|
ensure
|
|
1472
1638
|
Thread.current[@operation_driver_key] = nil
|
|
1473
|
-
|
|
1639
|
+
lender.checkin(driver, discard: discard) unless Thread.current[@tx_pin_key].equal?(driver)
|
|
1474
1640
|
end
|
|
1475
1641
|
end
|
|
1476
1642
|
|
|
@@ -1478,11 +1644,115 @@ module Tina4
|
|
|
1478
1644
|
driver = Thread.current[@tx_pin_key]
|
|
1479
1645
|
Thread.current[@tx_pin_key] = nil
|
|
1480
1646
|
Thread.current[@tx_depth_key] = nil
|
|
1481
|
-
if
|
|
1482
|
-
|
|
1647
|
+
if driver && !Thread.current[@operation_driver_key].equal?(driver)
|
|
1648
|
+
lender.checkin(driver, discard: discard)
|
|
1483
1649
|
end
|
|
1484
1650
|
end
|
|
1485
|
-
|
|
1651
|
+
# Share +hooks+ with a driver that can replay them after its own reconnect,
|
|
1652
|
+
# and run them on it now, before any statement does (ADR-0110).
|
|
1653
|
+
def self.attach_connect_hooks(driver, hooks)
|
|
1654
|
+
driver.connect_hooks = hooks if driver.respond_to?(:connect_hooks=)
|
|
1655
|
+
hooks.each { |hook| hook.call(driver) }
|
|
1656
|
+
end
|
|
1657
|
+
|
|
1658
|
+
# Run +hook+ (a callable, or the block) with the driver on every open
|
|
1659
|
+
# connection, on each new one, and on each connection a driver re-opens
|
|
1660
|
+
# after the server dropped it - before any statement runs on it (ADR-0110,
|
|
1661
|
+
# tina4-python#337). Use it to restore session state a reconnect would
|
|
1662
|
+
# otherwise lose: SET search_path, set_config(..., false). State that
|
|
1663
|
+
# changes per request cannot be a hook: set TINA4_DB_RECONNECT_RETRY=false
|
|
1664
|
+
# and restore it yourself.
|
|
1665
|
+
def add_connect_hook(hook = nil, &block)
|
|
1666
|
+
hook ||= block
|
|
1667
|
+
raise ArgumentError, "add_connect_hook needs a callable or a block" unless hook.respond_to?(:call)
|
|
1668
|
+
|
|
1669
|
+
@connect_hooks << hook
|
|
1670
|
+
open = @pool ? @pool.open_drivers : [(@driver if @connected)].compact
|
|
1671
|
+
open.each { |driver| hook.call(driver) }
|
|
1672
|
+
hook
|
|
1673
|
+
end
|
|
1674
|
+
|
|
1675
|
+
# What lends this database's connections: the pool, or the single connection.
|
|
1676
|
+
def lender
|
|
1677
|
+
@pool || @single_lease
|
|
1678
|
+
end
|
|
1679
|
+
|
|
1680
|
+
# Borrow a connection for this thread, waiting for one if all are lent out.
|
|
1681
|
+
# A single connection that failed to open raises its real cause first.
|
|
1682
|
+
def lend_driver
|
|
1683
|
+
raise Tina4::DatabaseConnectionError, connect_error_message if @connect_error
|
|
1684
|
+
|
|
1685
|
+
raise_if_closed
|
|
1686
|
+
reopen_after_fork unless @pool
|
|
1687
|
+
lender.checkout
|
|
1688
|
+
end
|
|
1689
|
+
|
|
1690
|
+
# A closed pool stays closed: it never quietly reopens connections a
|
|
1691
|
+
# shutdown just closed (the single connection's closed driver refuses too).
|
|
1692
|
+
def raise_if_closed
|
|
1693
|
+
raise Tina4::DatabaseConnectionError, "Database is closed (#{@driver_name})" if @closed && @pool
|
|
1694
|
+
end
|
|
1695
|
+
|
|
1696
|
+
# A pooled connection opened after the first: connected through the same
|
|
1697
|
+
# path as the first, so a failure is logged and raised without credentials.
|
|
1698
|
+
def open_pooled_driver
|
|
1699
|
+
driver = create_driver
|
|
1700
|
+
driver.connect(driver_connection_string, username: @username, password: @password)
|
|
1701
|
+
Tina4::Database.attach_connect_hooks(driver, @connect_hooks)
|
|
1702
|
+
driver.autocommit = @autocommit if driver_implements?(driver, :autocommit=)
|
|
1703
|
+
driver
|
|
1704
|
+
rescue Tina4::DatabaseConnectionError
|
|
1705
|
+
raise
|
|
1706
|
+
rescue StandardError => e
|
|
1707
|
+
raise Tina4::DatabaseConnectionError, "Database connection failed (#{@driver_name}): " "#{e.class}: #{scrub_connection_string(e.message)}"
|
|
1708
|
+
end
|
|
1709
|
+
|
|
1710
|
+
# The single connection a forked child left is reopened on first use.
|
|
1711
|
+
def reopen_after_fork
|
|
1712
|
+
return unless @driver.nil? && @reopen_after_fork
|
|
1713
|
+
|
|
1714
|
+
@reopen_after_fork = false
|
|
1715
|
+
@driver = create_driver
|
|
1716
|
+
connect
|
|
1717
|
+
end
|
|
1718
|
+
|
|
1719
|
+
# SQLite cases that must stay ONE connection whatever TINA4_DB_POOL says:
|
|
1720
|
+
# an in-memory database (a second connection is a different, empty
|
|
1721
|
+
# database), and a sqlite3 gem without the busy handler that releases the
|
|
1722
|
+
# GVL - with it, a thread waiting on another connection's write lock
|
|
1723
|
+
# blocks every thread, the lock holder included, for the whole 5s timeout.
|
|
1724
|
+
def single_connection_only?
|
|
1725
|
+
return false unless @driver_name == "sqlite"
|
|
1726
|
+
return true if Tina4::DatabaseUrl::SQLITE_MEMORY_FORMS.include?(@connection_string.to_s.strip)
|
|
1727
|
+
|
|
1728
|
+
Tina4.require_sqlite3!
|
|
1729
|
+
!SQLite3::Database.method_defined?(:busy_handler_timeout=)
|
|
1730
|
+
rescue LoadError
|
|
1731
|
+
true
|
|
1732
|
+
end
|
|
1733
|
+
|
|
1734
|
+
public
|
|
1735
|
+
|
|
1736
|
+
# In a forked child (ADR-0074): a pre-fork server (Puma cluster with
|
|
1737
|
+
# preload_app!) or any Process.fork copies this Database, sockets included,
|
|
1738
|
+
# so its drivers are the PARENT's server sessions. Leave them and open this
|
|
1739
|
+
# process's own on first use. This thread's pins came through the fork too.
|
|
1740
|
+
def leave_inherited_connections!
|
|
1741
|
+
Thread.current[@tx_pin_key] = nil
|
|
1742
|
+
Thread.current[@operation_driver_key] = nil
|
|
1743
|
+
Thread.current[@tx_depth_key] = nil
|
|
1744
|
+
memory = @driver_name == "sqlite" && Tina4::DatabaseUrl::SQLITE_MEMORY_FORMS.include?(@connection_string.to_s.strip)
|
|
1745
|
+
@pool&.leave_inherited_connections!(keep: memory)
|
|
1746
|
+
@single_lease&.leave_inherited_connections!(keep: true)
|
|
1747
|
+
return if memory
|
|
1748
|
+
|
|
1749
|
+
self.class.leave_inherited(@driver) if @driver && !@pool
|
|
1750
|
+
@driver = nil
|
|
1751
|
+
@reopen_after_fork = !@pool
|
|
1752
|
+
end
|
|
1753
|
+
|
|
1754
|
+
private :with_operation_driver, :release_transaction_driver, :lender, :lend_driver,
|
|
1755
|
+
:reopen_after_fork, :single_connection_only?, :raise_if_closed, :open_pooled_driver
|
|
1486
1756
|
|
|
1487
1757
|
private
|
|
1488
1758
|
|
|
@@ -2072,6 +2342,17 @@ module Tina4
|
|
|
2072
2342
|
raise "Driver #{klass_name} not loaded. Install the required gem."
|
|
2073
2343
|
end
|
|
2074
2344
|
end
|
|
2345
|
+
# Runs Tina4::Database.after_fork_in_child in every forked child (Ruby 3.1+
|
|
2346
|
+
# routes Kernel#fork, Process.fork and IO.popen("-") through Process._fork).
|
|
2347
|
+
module DatabaseForkSafety
|
|
2348
|
+
def _fork
|
|
2349
|
+
pid = super
|
|
2350
|
+
Tina4::Database.after_fork_in_child if pid.zero?
|
|
2351
|
+
pid
|
|
2352
|
+
end
|
|
2353
|
+
end
|
|
2354
|
+
Process.singleton_class.prepend(DatabaseForkSafety) if Process.respond_to?(:_fork)
|
|
2355
|
+
|
|
2075
2356
|
module DatabaseOperationLeases
|
|
2076
2357
|
%i[fetch_all fetch fetch_one insert primary_key update delete truncate
|
|
2077
2358
|
get_last_id execute tables columns table_exists? get_next_id commit rollback].each do |operation|
|
|
@@ -286,6 +286,14 @@ module Tina4
|
|
|
286
286
|
# whole or fractional, at a cost of at most one extra second on a path that
|
|
287
287
|
# has already failed. (libpq also reads connect_timeout=0 as "wait forever",
|
|
288
288
|
# so the +1 doubles as the guard against rounding a sub-second bound to 0.)
|
|
289
|
+
# Whether a statement that hit a dropped connection is run again, once, on
|
|
290
|
+
# the new one. TINA4_DB_RECONNECT_RETRY=false turns the silent re-run off:
|
|
291
|
+
# the connection still heals, but the caller sees the error, so an app that
|
|
292
|
+
# keeps per-request session state can restore it first (ADR-0110).
|
|
293
|
+
def self.reconnect_retry?
|
|
294
|
+
Tina4::Env.is_truthy(ENV.fetch("TINA4_DB_RECONNECT_RETRY", "true"))
|
|
295
|
+
end
|
|
296
|
+
|
|
289
297
|
def self.connect_timeout_whole_seconds
|
|
290
298
|
seconds = connect_timeout_seconds
|
|
291
299
|
seconds && [seconds.floor + 1, 1].max
|