简体   繁体   中英

Share Redis pool between Sidekiq & Rails

What is the best practice to share a Redis connection pool between Rails & Sidekiq ?

I've done that in an initializer :

Sidekiq.configure_client do |config|
  pool = ConnectionPool.new(size: 1, timeout: 5) { Redis.new(host: redis_config['host'], port: redis_config['port'], db: redis_config['database']) }
  config.redis  = pool
  Redis.current = pool
end

Sidekiq.configure_server do |config|
  pool = ConnectionPool.new(size: 10, timeout: 5) { Redis.new(host: redis_config['host'], port: redis_config['port'], db: redis_config['database']) }
  config.redis  = pool
  Redis.current = pool
  config.server_middleware do |chain|
    chain.add Kiqstand::Middleware
  end
end

But setting the Rails Redis pool in the Sidekiq block is not very clean... Any ideas ?

After a look into Sidekiq sources, Sidekiq.server? method should be a better option. I've change the initializer code for this :

# Redis config
Redis.current = ConnectionPool.new(size: (Sidekiq.server? ? 15 : 1), timeout: 5) do 
  Redis.new host: redis_config['host'], port: redis_config['port'], db: redis_config['database']
end

# Sidekiq config
Sidekiq.configure_client do |config|
  config.redis = Redis.current
end

Sidekiq.configure_server do |config|
  config.redis = Redis.current
  config.server_middleware do |chain|
    chain.add Kiqstand::Middleware
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM