From 807f79ef5d03098f5f0f70e4937779c98b498700 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Sat, 3 Oct 2020 17:54:33 +0800 Subject: [PATCH] feat: support for Redis sentinel (#1301) closes: #925 --- .env.example | 5 +++++ config/initializers/redis.rb | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 281cd64d1..5a6ab50ba 100644 --- a/.env.example +++ b/.env.example @@ -22,6 +22,11 @@ REDIS_URL=redis://redis:6379 # which will be the password for the redis service running inside the docker-compose # to make it secure REDIS_PASSWORD= +# Redis Sentinel can be used by passing list of sentinel host and ports e,g. sentinel_host1:port1,sentinel_host2:port2 +REDIS_SENTINELS= +# Redis sentinel master name is required when using sentinel, default value is "mymaster". +# You can find list of master using "SENTINEL masters" command +REDIS_SENTINEL_MASTER_NAME= # Postgres Database config variables POSTGRES_HOST=postgres diff --git a/config/initializers/redis.rb b/config/initializers/redis.rb index 423e0500f..35739a1ab 100644 --- a/config/initializers/redis.rb +++ b/config/initializers/redis.rb @@ -2,8 +2,23 @@ app_redis_config = { url: URI.parse(ENV.fetch('REDIS_URL', 'redis://127.0.0.1:6379')), password: ENV.fetch('REDIS_PASSWORD', nil).presence } -redis = Rails.env.test? ? MockRedis.new : Redis.new(app_redis_config) +if ENV['REDIS_SENTINELS'].presence + default_sentinel_port = '26379' + # expected format for REDIS_SENTINELS url string is host1:port1, host2:port2 + sentinels = ENV['REDIS_SENTINELS'].split(',').map do |sentinel_url| + host, port = sentinel_url.split(':').map(&:strip) + { host: host, port: port || default_sentinel_port, password: app_redis_config[:password] } + end + + master_name = ENV.fetch('REDIS_SENTINEL_MASTER_NAME', 'mymaster') + # over-write redis url as redis://:@/ when using sentinel + # more at https://github.com/redis/redis-rb/issues/531#issuecomment-263501322 + app_redis_config[:url] = URI.parse("redis://#{master_name}") + app_redis_config[:sentinels] = sentinels +end + +redis = Rails.env.test? ? MockRedis.new : Redis.new(app_redis_config) # Alfred # Add here as you use it for more features # Used for Round Robin, Conversation Emails & Online Presence