mperham / connection_pool

Generic connection pooling for Ruby
MIT License
1.63k stars 143 forks source link

Ok to use #checkout / #checkin in a Rails controller? #88

Closed rmosolgo closed 8 years ago

rmosolgo commented 8 years ago

Hi!

TL;DR, I'm wondering if this is fair game:

class ThingsController < ApplicationController 
  before_action :checkout_connection 
  after_action :checkin_connection 

  def index 
     # do stuff with @connection 
  end 

  private 

  def checkout_connection 
    @connection = CONNECTION_POOL.checkout 
  end 
  def checkin_connection 
    CONNECTION_POOL.checkin 
  end 
end 

I see that #checkout and #checkin are public methods, but I don't see them in the documentation, so I thought I would ask if that is proper usage. Does it seem ok?

Thanks!

mperham commented 8 years ago

I would use an around_filter:

def with_connection
  CONNECTION_POOL.with do |conn|
    @connection = conn
    begin
      yield
    ensure
      @connection = nil
    end
  end
end
rmosolgo commented 8 years ago

fair 'nuf, I was about to say "In this case I can't use an around_action", but then I realized I could, and it would make the API better anyways :)

(Real use case: https://github.com/reactjs/react-rails/pull/559)