Last active
November 10, 2015 15:40
-
-
Save sloops77/dc5dfe7eec3c9424844e to your computer and use it in GitHub Desktop.
Allows you to share a base connection pool, and reuse the pool in different namespaced use cases. (Caution Heroku biased)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/application.rb | |
... | |
#sets the DB_POOL env variable so that we can use it when setting up redis and db in the worker server | |
if Sidekiq.server? | |
concurrency = ENV['WORKER_CONCURRENCY'] || Sidekiq.options[:concurrency] | |
sidekiq_admin_threads = 2 | |
poolSize = concurrency + sidekiq_admin_threads | |
p "setting ENV['DB_POOL']=#{poolSize}" | |
ENV['DB_POOL'] = poolSize.to_s | |
end | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/initializers/database_connection.rb | |
Rails.application.config.after_initialize do | |
ActiveRecord::Base.connection_pool.disconnect! | |
ActiveSupport.on_load(:active_record) do | |
if Rails.application.config.database_configuration | |
config = Rails.application.config.database_configuration[Rails.env] | |
config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 15 # seconds | |
config['pool'] = ENV['DB_POOL'] || ENV['MAX_THREADS'] || 7 | |
puts "Running database_connection.rb: #{config}" | |
ActiveRecord::Base.establish_connection(config) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConnectionPool::DecoratedConnectionPool < ::ConnectionPool | |
def initialize(parent_pool, &block) | |
super({}) {nil} | |
@pool = parent_pool | |
@checkout_block = block | |
@decorated = {} | |
end | |
def checkout(options = {}) | |
raw = @pool.checkout | |
@decorated[raw.object_id] ||= @checkout_block.call(raw) | |
@decorated[raw.object_id] | |
end | |
def checkin | |
@pool.checkin | |
nil | |
end | |
def shutdown | |
@pool.shutdown | |
@decorated.clear | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/initializers/redis.rb | |
url = URI.parse(ENV["REDIS_URL"] || 'redis://localhost:6379/1') | |
poolSize = ENV['DB_POOL'] || ENV['MAX_THREADS'] || 7 | |
p "Redis PoolSize: #{poolSize}" | |
$redis = ConnectionPool.new(size: poolSize, timeout: 3) {Redis.new(url: url)} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/initializers/sidekiq.rb | |
Sidekiq.configure_client do |config| | |
config.redis = ConnectionPool::DecoratedConnectionPool.new($redis) {|c| Redis::Namespace.new('ns', :redis => c)} | |
end | |
Sidekiq.configure_server do |config| | |
config.redis = ConnectionPool::DecoratedConnectionPool.new($redis) {|c| Redis::Namespace.new('ns', :redis => c)} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment