Module: Familia::Connection::Middleware
- Included in:
- Familia::Connection
- Defined in:
- lib/familia/connection/middleware.rb
Instance Attribute Summary collapse
-
#enable_database_counter ⇒ Boolean
Whether Database command counter is enabled.
-
#enable_database_logging ⇒ Boolean
Whether Database command logging is enabled.
Instance Method Summary collapse
-
#clear_fiber_connection! ⇒ Object
Clears the fiber-local connection.
-
#fiber_connection=(connection) ⇒ Object
Sets a versioned fiber-local connection.
-
#increment_middleware_version! ⇒ Object
Increments the middleware version, invalidating all cached connections.
-
#middleware_version ⇒ Integer
Current middleware version for cache invalidation.
-
#reconnect! ⇒ Object
Reconnects with fresh middleware registration.
Instance Attribute Details
#enable_database_counter ⇒ Boolean
Returns Whether Database command counter is enabled.
15 16 17 |
# File 'lib/familia/connection/middleware.rb', line 15 def enable_database_counter @enable_database_counter end |
#enable_database_logging ⇒ Boolean
Returns Whether Database command logging is enabled.
12 13 14 |
# File 'lib/familia/connection/middleware.rb', line 12 def enable_database_logging @enable_database_logging end |
Instance Method Details
#clear_fiber_connection! ⇒ Object
Clears the fiber-local connection
36 37 38 39 |
# File 'lib/familia/connection/middleware.rb', line 36 def clear_fiber_connection! Fiber[:familia_connection] = nil Familia.trace :FIBER_CONNECTION, nil, 'Cleared' if Familia.debug? end |
#fiber_connection=(connection) ⇒ Object
Sets a versioned fiber-local connection
29 30 31 32 33 |
# File 'lib/familia/connection/middleware.rb', line 29 def fiber_connection=(connection) current_version = middleware_version Fiber[:familia_connection] = [connection, current_version] Familia.trace :FIBER_CONNECTION, nil, "Set with version #{current_version}" end |
#increment_middleware_version! ⇒ Object
Increments the middleware version, invalidating all cached connections
23 24 25 26 |
# File 'lib/familia/connection/middleware.rb', line 23 def increment_middleware_version! new_version = @middleware_version.increment Familia.trace :MIDDLEWARE_VERSION, nil, "Incremented to #{new_version}" end |
#middleware_version ⇒ Integer
Returns Current middleware version for cache invalidation.
18 19 20 |
# File 'lib/familia/connection/middleware.rb', line 18 def middleware_version @middleware_version.value end |
#reconnect! ⇒ Object
If no middleware is enabled, this method safely clears connection state but won't register any middleware until it's enabled.
Reconnects with fresh middleware registration
This method is useful when middleware needs to be applied to connection pools that were created before middleware was enabled. It:
- Clears the middleware registration flag to allow re-registration
- Re-runs the middleware registration logic
- Clears connection chain to force rebuild
- Increments middleware version to invalidate cached connections
- Clears fiber-local connections
The next connection request will use the updated middleware configuration. Existing connection pools will naturally create new connections with middleware as old connections are cycled out.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/familia/connection/middleware.rb', line 87 def reconnect! # Thread-safe: Use same mutex as dbclient to protect @connection_chain @connection_chain_mutex.synchronize do # Allow middleware to be re-registered by resetting all flags @middleware_registered = false @logger_registered = false @counter_registered = false register_middleware_once # Clear connection chain to force rebuild @connection_chain = nil # Increment version to invalidate all cached connections increment_middleware_version! # Clear fiber-local connections clear_fiber_connection! end Familia.trace :RECONNECT, nil, 'Connection chain cleared, will rebuild with current middleware on next use' end |