Skip to content

Commit

Permalink
Fix clear_active_connections! with no argument deprecation warning
Browse files Browse the repository at this point in the history
We're using gruf v2.20.1, and still experience the deprecation warning
for `clear_active_connections!`.

```
DEPRECATION WARNING: `clear_active_connections!` currently only applies to connection pools in the current role (`reading`).
In Rails 7.2, this method will apply to all known pools, regardless of role. To affect only those connections belonging to a specific role, pass the role name as an argument.
To switch to the new behavior, pass `:all` as the role name. (called from block in call at gruf/lib/gruf/interceptors/active_record/connection_reset.rb:32)>
```

This is caused by rails/rails#45924.

~~If you're still supporting Rails < 7.1, then we should change this to
use `ActiveRecord::Base.current_role`, but I thought your goal was to
clear any and all active connections -- please correct me if I'm
wrong.~~

EDIT: I think supporting Rails 7.0 is a good idea, but I think you lose
the ability to clear those connections in the other pools.

Due to Gruf::Controllers::Base rewriting the error message, actually the
backtrace location was confusing.

Maybe `fail!` could be fixed to include it in the message, but that is
beyond the scope of this PR.
  • Loading branch information
zzak committed Jan 21, 2025
1 parent db34fee commit 53ba018
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/gruf/interceptors/active_record/connection_reset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConnectionReset < ::Gruf::Interceptors::ServerInterceptor
def call
yield
ensure
target_classes.each { |klass| klass.connection_handler.clear_active_connections! } if enabled?
target_classes.each { |klass| klass.connection_handler.clear_active_connections!(::ActiveRecord::Base.current_role) } if enabled?
end

private
Expand Down
6 changes: 5 additions & 1 deletion spec/support/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def self.connection_handler
def self.connected?
true
end

def self.current_role
:writing
end
end

class Connection
Expand All @@ -45,7 +49,7 @@ def active?
end

class ConnectionHandler
def clear_active_connections!
def clear_active_connections!(role)
true
end
end
Expand Down

0 comments on commit 53ba018

Please sign in to comment.