fibered_mysql2 0.4.0 → 0.5.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: a650c8b928698f83012667d045f865125f14d945386e479f266dfaf6bfa6758a
4
- data.tar.gz: abb22b1f34fde162c33fc58166015fe144e3f860505bf3041cfbf91410ba1dd3
3
+ metadata.gz: 700465a391223abb977137a4f889cb6dd1a8769ea696a3beada2622adc72c32e
4
+ data.tar.gz: fe3504eaa987b4d982a581f6eb56e4b5fe24fef15011f30664cb2128d6d4131d
5
5
  SHA512:
6
- metadata.gz: a6b4efbdea49aaac1fb9ff515b3615cbbfaf7e126b609032c372081c26b6fa89f74b47f65217e6987613cd12b1a1157dd75cb143730a7d8f70f3c5b7fb2b21bb
7
- data.tar.gz: 45938b8becc0110038709167ce6f2f2624dab108b173a4daee7e5ea81deb891455a132111843d0fe3a1d6de7097a39f93a4769c6e6674a5dcfc6c81dc4f0d256
6
+ metadata.gz: fe609887755fc0a41ac4d2dedf3a8df9586e720b6537a8183e612652a482c3200bd073451aa143daed6008e787280214af783d314f56b16f00fa6d2ba9a19d62
7
+ data.tar.gz: a77181a69718dc8392554c68b242958b26b21d94ac0459d19a3e8c9d6ad8ceb2bdb6f8da9ff32293c2b2648dd390740d18df86dbd039fc888b13ba6132a46463
data/CHANGELOG.md CHANGED
@@ -4,6 +4,35 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
5
  Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.5.0] - 2026-11-18
8
+ ### Changed
9
+ - On the EventMachine reactor thread, `FiberedMysql2Adapter.new_client` now connects on a separate thread and yields the
10
+ calling fiber until the connect finishes, so a database that does not answer no longer stops the reactor for
11
+ `connect_timeout` on every connect attempt, including each Rails 7.1 `connection_retries` attempt. Off the reactor
12
+ thread it connects inline as before. On the reactor's root fiber, which cannot yield, the connect runs on the separate
13
+ thread but the root fiber blocks until it finishes, so the reactor stops for the connect as before. Errors map as
14
+ before: 1049 raises `ActiveRecord::NoDatabaseError` and any other `Mysql2::Error` raises
15
+ `ActiveRecord::ConnectionNotEstablished`.
16
+ - Each connect in progress uses one thread, which ends when the connect does (bounded by `connect_timeout`; mysql2
17
+ uses 120 s when it is not set).
18
+ - If the reactor stops during a connect, the waiting fiber is not resumed in a later reactor, and the client is closed.
19
+ If the waiting fiber is unwound before the connect finishes, the client is closed too.
20
+ - Other errors raised while connecting keep the caller's frames in their backtrace. A connect thread that ends
21
+ without a client raises `ActiveRecord::ConnectionNotEstablished`.
22
+ - `FiberedDatabaseConnectionPool#connection` no longer holds the pool monitor around `checkout` (as in Rails, each cache
23
+ key belongs to one fiber). Other fibers can check connections in and out while a new connection is connecting, and
24
+ `checkout_timeout` applies to them.
25
+
26
+ ### Notes
27
+ - Any fiber other than the root fiber on the reactor thread now yields while a connection connects, including fibers not
28
+ driven by EventMachine (for example an `Enumerator`'s). The connection's first queries already yielded the same way.
29
+ - `Timeout.timeout` around a connect is not supported under the reactor: the timeout fires while the fiber is waiting
30
+ and the exception escapes the reactor instead of reaching the caller (as it already did for queries). Use an
31
+ EventMachine timer instead.
32
+ - Rails 7.1's `reconnect!` waits between attempts with `Kernel#sleep` (`backoff`). That blocks the reactor unless the app
33
+ routes `sleep` on the reactor to a fiber-aware sleep (for example exceptional_synchrony's
34
+ `Kernel.em_synchrony_sleep_hook` set to `EM::Synchrony.sleep`).
35
+
7
36
  ## [0.4.0] - 2025-08-21
8
37
  ### Added
9
38
  - Support for Rails 7.1.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fibered_mysql2 (0.4.0)
4
+ fibered_mysql2 (0.5.0)
5
5
  em-synchrony (~> 1.0)
6
6
  rails (>= 7.0, < 7.2)
7
7
 
@@ -92,7 +92,7 @@ module FiberedMysql2
92
92
  class << self
93
93
  # Copied from Mysql2Adapter, except with the EM Mysql2 client
94
94
  def new_client(config)
95
- Mysql2::EM::Client.new(config)
95
+ connect_client(config)
96
96
  rescue Mysql2::Error => error
97
97
  if error.error_number == 1049
98
98
  raise ActiveRecord::NoDatabaseError.new, error.message
@@ -100,6 +100,94 @@ module FiberedMysql2
100
100
  raise ActiveRecord::ConnectionNotEstablished, error.message
101
101
  end
102
102
  end
103
+
104
+ private
105
+
106
+ # Mysql2::Client#initialize connects synchronously (mysql_real_connect), so on the reactor thread it would
107
+ # stop EventMachine for up to connect_timeout. There, connect on a separate thread (the native connect releases
108
+ # the GVL) and yield the calling fiber until it is done. Off the reactor thread, connect inline as before.
109
+ # On the reactor's root fiber, which cannot yield, the connect still runs on the separate thread, but the root
110
+ # fiber blocks until it is done, so the reactor stops for the connect as it did before.
111
+ def connect_client(config)
112
+ if EM.reactor_running? && EM.reactor_thread?
113
+ connect_client_off_reactor(config)
114
+ else
115
+ Mysql2::EM::Client.new(config)
116
+ end
117
+ end
118
+
119
+ def connect_client_off_reactor(config)
120
+ fiber = Fiber.current
121
+ reactor_run = current_reactor_run
122
+ waiting = false
123
+ done = false
124
+ abandoned = false
125
+
126
+ connect_thread = Thread.new do
127
+ Thread.current.report_on_exception = false
128
+ Mysql2::EM::Client.new(config)
129
+ ensure
130
+ # EM.next_tick is safe to call from another thread; the block runs on the reactor thread.
131
+ EM.next_tick do
132
+ done = true
133
+ if waiting && fiber.alive? && reactor_run.equal?(@current_reactor_run)
134
+ fiber.resume
135
+ elsif waiting || abandoned
136
+ # Nobody will read the result: the reactor this connect started on has stopped (this tick is being
137
+ # flushed by a later EM.run), or the waiting fiber was unwound. Close the client instead of leaking it.
138
+ close_orphaned_client(connect_thread)
139
+ end
140
+ end
141
+ end
142
+
143
+ finished = false
144
+ begin
145
+ begin
146
+ waiting = true
147
+ Fiber.yield until done
148
+ rescue FiberError
149
+ # The root fiber cannot yield: block on the connect thread below, which stops the reactor for the connect
150
+ # as an inline connect did.
151
+ ensure
152
+ waiting = false
153
+ end
154
+
155
+ client = connect_thread_result(connect_thread)
156
+ finished = true
157
+ client
158
+ ensure
159
+ # Unwound while waiting (for example by an exception raised into this fiber): let the tick close the client.
160
+ abandoned = true unless finished || done
161
+ end
162
+ end
163
+
164
+ # A token for the current EM.run, cleared when that reactor stops, so a connect that outlives its reactor
165
+ # never resumes its fiber inside a later one. The shutdown hook that clears it is registered only when the token
166
+ # is created, so once per reactor run; later connects in the same run reuse the token and register nothing.
167
+ def current_reactor_run
168
+ return @current_reactor_run if @current_reactor_run
169
+
170
+ reactor_run = Object.new
171
+ EM.add_shutdown_hook { @current_reactor_run = nil if @current_reactor_run.equal?(reactor_run) }
172
+ @current_reactor_run = reactor_run
173
+ end
174
+
175
+ def connect_thread_result(connect_thread)
176
+ connect_thread.value or
177
+ raise ActiveRecord::ConnectionNotEstablished, "fibered_mysql2 connect thread ended without returning a client"
178
+ rescue Mysql2::Error, ActiveRecord::ConnectionNotEstablished
179
+ raise
180
+ rescue StandardError => ex
181
+ # Thread#value re-raises with the connect thread's backtrace only; add the caller's frames.
182
+ ex.set_backtrace(Array(ex.backtrace) + caller)
183
+ raise ex
184
+ end
185
+
186
+ def close_orphaned_client(connect_thread)
187
+ connect_thread.value&.close
188
+ rescue StandardError
189
+ nil
190
+ end
103
191
  end
104
192
  end
105
193
  end
@@ -224,20 +224,11 @@ module FiberedMysql2
224
224
  @reaper = nil # no need to keep a reference to this since it does nothing in this sub-class
225
225
  end
226
226
 
227
+ # As in Rails: each cache key belongs to a single fiber, so no lock is needed here. Holding the pool monitor around
228
+ # checkout would also make every other fiber wait for it while a new connection connects (the connect yields the
229
+ # fiber), including fibers that only want to check a connection in.
227
230
  def connection
228
- # this is correctly done double-checked locking
229
- # (ThreadSafe::Cache's lookups have volatile semantics)
230
- if (result = cached_connections[current_connection_id])
231
- result
232
- else
233
- synchronize do
234
- if (result = cached_connections[current_connection_id])
235
- result
236
- else
237
- cached_connections[current_connection_id] = checkout
238
- end
239
- end
240
- end
231
+ cached_connections[current_connection_id] ||= checkout
241
232
  end
242
233
 
243
234
  # Invoca Patch - Reap connections so we re-use any orphaned connections instead of creating a new connection.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FiberedMysql2
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fibered_mysql2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2025-08-21 00:00:00.000000000 Z
10
+ date: 2026-11-19 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: em-synchrony
@@ -44,7 +43,6 @@ dependencies:
44
43
  - - "<"
45
44
  - !ruby/object:Gem::Version
46
45
  version: '7.2'
47
- description:
48
46
  email:
49
47
  - development@invoca.com
50
48
  executables: []
@@ -80,7 +78,6 @@ licenses: []
80
78
  metadata:
81
79
  allowed_push_host: https://rubygems.org
82
80
  homepage_uri: https://github.com/Invoca/fibered_mysql2
83
- post_install_message:
84
81
  rdoc_options: []
85
82
  require_paths:
86
83
  - lib
@@ -95,8 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
92
  - !ruby/object:Gem::Version
96
93
  version: '0'
97
94
  requirements: []
98
- rubygems_version: 3.3.27
99
- signing_key:
95
+ rubygems_version: 3.6.3
100
96
  specification_version: 4
101
97
  summary: An adapter for fibered mysql2
102
98
  test_files: []