Skip to content

Allow setting redis options #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ In order to prevent getting the RedisMutex::LockError error when having a large
# config/initializers/gush.rb
Gush.configure do |config|
config.redis_url = "redis://localhost:6379"
# NB: you can also pass redis options as a hash:
# config.redis = { host: "localhost", port: 6379, db: 1 }
config.concurrency = 5
config.locking_duration = 2 # how long you want to wait for the lock to be released, in seconds
config.polling_interval = 0.3 # how long the polling interval should be, in seconds
Expand All @@ -516,6 +518,8 @@ Running `NotifyWorkflow.create` inserts multiple keys into Redis every time it i
# config/initializers/gush.rb
Gush.configure do |config|
config.redis_url = "redis://localhost:6379"
# NB: you can also pass redis options as a hash:
# config.redis = { host: "localhost", port: 6379, db: 1 }
config.concurrency = 5
config.ttl = 3600*24*7
end
Expand Down
8 changes: 4 additions & 4 deletions lib/gush/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ class Client
@@redis_connection = Concurrent::ThreadLocalVar.new(nil)

def self.redis_connection(config)
cached = (@@redis_connection.value ||= { url: config.redis_url, connection: nil })
return cached[:connection] if !cached[:connection].nil? && config.redis_url == cached[:url]
cached = (@@redis_connection.value ||= config.redis.merge(connection: nil))
return cached[:connection] if !cached[:connection].nil? && config.redis == cached.except(:connection)

Redis.new(url: config.redis_url).tap do |instance|
Redis.new(config.redis).tap do |instance|
RedisClassy.redis = instance
@@redis_connection.value = { url: config.redis_url, connection: instance }
@@redis_connection.value = config.redis.merge(connection: instance)
end
end

Expand Down
15 changes: 13 additions & 2 deletions lib/gush/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module Gush
class Configuration
attr_accessor :concurrency, :namespace, :redis_url, :ttl, :locking_duration, :polling_interval
attr_accessor :concurrency, :namespace, :redis, :ttl, :locking_duration, :polling_interval

def self.from_json(json)
new(Gush::JSON.decode(json, symbolize_keys: true))
end

def initialize(hash = {})
raise ArgumentError, "You can't set both redis_url and redis" if hash.key?(:redis_url) && hash.key?(:redis)

self.concurrency = hash.fetch(:concurrency, 5)
self.namespace = hash.fetch(:namespace, 'gush')
self.redis_url = hash.fetch(:redis_url, 'redis://localhost:6379')
self.redis = hash.key?(:redis) ? hash.fetch(:redis) : { url: hash.fetch(:redis_url, 'redis://localhost:6379') }
self.gushfile = hash.fetch(:gushfile, 'Gushfile')
self.ttl = hash.fetch(:ttl, -1)
self.locking_duration = hash.fetch(:locking_duration, 2) # how long you want to wait for the lock to be released, in seconds
Expand All @@ -24,11 +26,20 @@ def gushfile
@gushfile.realpath if @gushfile.exist?
end

def redis_url=(url)
redis[:url] = url
end

def redis_url
redis[:url]
end

def to_hash
{
concurrency: concurrency,
namespace: namespace,
redis_url: redis_url,
redis: redis,
ttl: ttl,
locking_duration: locking_duration,
polling_interval: polling_interval
Expand Down
16 changes: 16 additions & 0 deletions spec/gush/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
end

expect(Gush.configuration.redis_url).to eq("redis://localhost")
expect(Gush.configuration.redis).to eq({ url: "redis://localhost" })
expect(Gush.configuration.concurrency).to eq(25)
expect(Gush.configuration.locking_duration).to eq(5)
expect(Gush.configuration.polling_interval).to eq(0.5)
end

it "allows setting redis opptions" do
Gush.configure do |config|
config.redis = { host: "localhost", port: 6379, db: 1 }
config.concurrency = 25
config.locking_duration = 5
config.polling_interval = 0.5
end

expect(Gush.configuration.redis).to eq({ host: "localhost", port: 6379, db: 1 })
expect(Gush.configuration.redis_url).to eq(nil)
expect(Gush.configuration.concurrency).to eq(25)
expect(Gush.configuration.locking_duration).to eq(5)
expect(Gush.configuration.polling_interval).to eq(0.5)
Expand Down