Fix single Redis connection being used across all threads (#18135)
* Fix single Redis connection being used across all Sidekiq threads * Fix teststh-downstream
parent
57e23df5b1
commit
553889bc7c
@ -0,0 +1,47 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class RedisConfiguration
|
||||
class << self
|
||||
def with
|
||||
pool.with { |redis| yield redis }
|
||||
end
|
||||
|
||||
def pool
|
||||
@pool ||= ConnectionPool.new(size: pool_size) { new.connection }
|
||||
end
|
||||
|
||||
def pool_size
|
||||
if Sidekiq.server?
|
||||
Sidekiq.options[:concurrency]
|
||||
else
|
||||
ENV['MAX_THREADS'] || 5
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def connection
|
||||
if namespace?
|
||||
Redis::Namespace.new(namespace, redis: raw_connection)
|
||||
else
|
||||
raw_connection
|
||||
end
|
||||
end
|
||||
|
||||
def namespace?
|
||||
namespace.present?
|
||||
end
|
||||
|
||||
def namespace
|
||||
ENV.fetch('REDIS_NAMESPACE', nil)
|
||||
end
|
||||
|
||||
def url
|
||||
ENV['REDIS_URL']
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def raw_connection
|
||||
Redis.new(url: url, driver: :hiredis)
|
||||
end
|
||||
end
|
@ -1,9 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class PrecomputeFeedService < BaseService
|
||||
include Redisable
|
||||
|
||||
def call(account)
|
||||
FeedManager.instance.populate_home(account)
|
||||
ensure
|
||||
Redis.current.del("account:#{account.id}:regeneration")
|
||||
redis.del("account:#{account.id}:regeneration")
|
||||
end
|
||||
end
|
||||
|
@ -1,14 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
redis_connection = Redis.new(
|
||||
url: ENV['REDIS_URL'],
|
||||
driver: :hiredis
|
||||
)
|
||||
|
||||
namespace = ENV.fetch('REDIS_NAMESPACE') { nil }
|
||||
|
||||
if namespace
|
||||
Redis.current = Redis::Namespace.new(namespace, redis: redis_connection)
|
||||
else
|
||||
Redis.current = redis_connection
|
||||
end
|
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class Mastodon::RackMiddleware
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
||||
def call(env)
|
||||
@app.call(env)
|
||||
ensure
|
||||
clean_up_sockets!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clean_up_sockets!
|
||||
clean_up_redis_socket!
|
||||
clean_up_statsd_socket!
|
||||
end
|
||||
|
||||
def clean_up_redis_socket!
|
||||
Thread.current[:redis]&.close
|
||||
Thread.current[:redis] = nil
|
||||
end
|
||||
|
||||
def clean_up_statsd_socket!
|
||||
Thread.current[:statsd_socket]&.close
|
||||
Thread.current[:statsd_socket] = nil
|
||||
end
|
||||
end
|
Loading…
Reference in new issue