Skip to content

Commit

Permalink
Merge pull request #2273 from sul-dlss/redis_pool
Browse files Browse the repository at this point in the history
Adds connection pool for redis. Increases timeout for redis.
  • Loading branch information
justinlittman authored Oct 16, 2023
2 parents 8874c29 + ad29f11 commit 73fdf73
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source 'https://rubygems.org'
# general Ruby/Rails gems
gem 'aws-sdk-s3', '~> 1.17'
gem 'committee' # Validates HTTP requests/responses per OpenAPI specification
gem 'connection_pool' # Used for redis
gem 'config' # Settings to manage configs on different instances
gem 'honeybadger' # for error reporting / tracking / notifications
gem 'jbuilder', '~> 2.5' # Build JSON APIs with ease.
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ DEPENDENCIES
capybara
committee
config
connection_pool
debug
dlss-capistrano
dor-event-client (~> 1.0)
Expand Down
10 changes: 2 additions & 8 deletions app/jobs/concerns/unique_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@ def lock_timeout
3600
end

def redis_connection
Redis.new(url: Settings.redis_url).tap do |conn|
yield conn if block_given?
end
end

# @return [String] the key for locking this job/payload combination, e.g. 'lock:MySpecificJob-bt821jk7040;1'
def queue_lock_key(*args)
# Changes in ActiveModel object don't result in new lock (which they do when just calling to_s).
Expand All @@ -90,7 +84,7 @@ def before_enqueue_lock(*)
now = Time.now.to_i
new_expiry_time = now + lock_timeout + 1

redis_connection do |conn|
REDIS.with do |conn|
# return true if we successfully acquired the lock
# "Set key to hold string value if key does not exist" (otherwise no-op) -- https://redis.io/commands/setnx
if conn.setnx(key, new_expiry_time)
Expand Down Expand Up @@ -123,7 +117,7 @@ def before_enqueue_lock(*)
def clear_lock(*)
key = queue_lock_key(*)
Rails.logger.info("clearing lock for #{key}...")
redis_connection do |conn|
REDIS.with do |conn|
conn.del(key).tap do |del_result|
Rails.logger.info("...cleared lock for #{key} (del_result=#{del_result})")
end
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

pool_size = ENV.fetch('RAILS_MAX_THREADS', 5)

REDIS = ConnectionPool.new(size: pool_size) do
Redis.new(url: Settings.redis_url, timeout: Settings.redis_timeout)
end
7 changes: 5 additions & 2 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# frozen_string_literal: true

# See separate initializer for redis used by unique job.

# Note the increased timeouts to try to address Redis timeouts
Sidekiq.configure_server do |config|
config.redis = { url: Settings.redis_url }
config.redis = { url: Settings.redis_url, network_timeout: Settings.redis_timeout, pool_timeout: Settings.redis_timeout }
# For Sidekiq Pro
config.super_fetch!
end

Sidekiq.configure_client do |config|
config.redis = { url: Settings.redis_url }
config.redis = { url: Settings.redis_url, network_timeout: Settings.redis_timeout, pool_timeout: Settings.redis_timeout }
end
Sidekiq::Client.reliable_push! unless Rails.env.test?
4 changes: 3 additions & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ rabbitmq:
password: guest

redis_url: redis://localhost:6379/
redis_timeout: 5 # seconds

honeybadger_checkins:
moab_to_catalog: xyzzy
Expand All @@ -68,4 +69,5 @@ honeybadger_checkins:

slow_queries:
enable: false
threshold: 500
threshold: 500

0 comments on commit 73fdf73

Please sign in to comment.