From 20fdecd1e1b34a49d76b8e384e5a6ad29e093e84 Mon Sep 17 00:00:00 2001 From: Sylvester Chin <schin@gitlab.com> Date: Sat, 17 Feb 2024 03:22:16 +0000 Subject: [PATCH] Apply redis sentinel connection closing patch to RedisClient --- config/initializers/7_redis.rb | 1 + .../patch/redis_client_sentinel_config.rb | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/gitlab/patch/redis_client_sentinel_config.rb diff --git a/config/initializers/7_redis.rb b/config/initializers/7_redis.rb index fbd2cbaabad6..72f5574334d7 100644 --- a/config/initializers/7_redis.rb +++ b/config/initializers/7_redis.rb @@ -30,6 +30,7 @@ # this only instruments `RedisClient` used in `Sidekiq.redis` RedisClient.register(Gitlab::Instrumentation::RedisClientMiddleware) RedisClient.prepend(Gitlab::Patch::RedisClient) +RedisClient::SentinelConfig.prepend(Gitlab::Patch::RedisClientSentinelConfig) if Gitlab::Redis::Workhorse.params[:cluster].present? raise "Do not configure workhorse with a Redis Cluster as pub/sub commands are not cluster-compatible." diff --git a/lib/gitlab/patch/redis_client_sentinel_config.rb b/lib/gitlab/patch/redis_client_sentinel_config.rb new file mode 100644 index 000000000000..f1a6a02214e4 --- /dev/null +++ b/lib/gitlab/patch/redis_client_sentinel_config.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +if Gem::Version.new(RedisClient::VERSION) != Gem::Version.new('0.20.0') + raise 'New version of redis-client detected, please remove or update this patch' +end + +module Gitlab + module Patch + module RedisClientSentinelConfig + # we manually apply the fix in + # https://github.com/redis-rb/redis-client/commit/26d355441f5b455294de887397ed8bea2e2c7275 + # until a new tag is released + def each_sentinel + last_error = nil + # rubocop:disable Gitlab/ModuleWithInstanceVariables -- Directly references code in redis-client gem + @sentinel_configs.dup.each do |sentinel_config| + sentinel_client = sentinel_client(sentinel_config) + success = true + begin + yield sentinel_client + rescue RedisClient::Error => error + last_error = error + success = false + sleep SENTINEL_DELAY + ensure + @sentinel_configs.unshift(@sentinel_configs.delete(sentinel_config)) if success + # Redis Sentinels may be configured to have a lower maxclients setting than + # the Redis nodes. Close the connection to the Sentinel node to avoid using + # a connection. + sentinel_client.close + # rubocop:enable Gitlab/ModuleWithInstanceVariables + end + end + end + end + end +end -- GitLab