Skip to content

Commit

Permalink
DEVOPS-481 Add support for redis client option
Browse files Browse the repository at this point in the history
  • Loading branch information
TiuSh committed Oct 5, 2023
1 parent 878e933 commit 5729428
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ Potlock.configure do |config|
config.redis_host = "localhost"
config.redis_port = "6379"
config.redis_db = "1"
# or
# config.redis = Redis.new(host: "localhost", port: 6379, db: 1)
end
```

Expand Down
14 changes: 9 additions & 5 deletions lib/potlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ def retry_delay
end

def redis
@redis ||= Redis.new(
host: Potlock.configuration.redis_host,
db: Potlock.configuration.redis_db,
port: Potlock.configuration.redis_port,
)
@redis ||= begin
return Potlock.configuration.redis unless Potlock.configuration.redis.nil?

Redis.new(
host: Potlock.configuration.redis_host,
db: Potlock.configuration.redis_db,
port: Potlock.configuration.redis_port,
)
end
end
end
end
9 changes: 5 additions & 4 deletions lib/potlock/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Potlock
class Configuration
# Redis connection information
attr_accessor :redis_host, :redis_port, :redis_db
attr_accessor :redis, :redis_host, :redis_port, :redis_db

# How many times it'll try to lock a resource
attr_accessor :retry_count
Expand All @@ -12,9 +12,10 @@ class Configuration
attr_accessor :retry_delay

def initialize
@redis_host = "localhost"
@redis_port = "6379"
@redis_db = "1"
@redis = nil
@redis_host = "localhost"
@redis_port = "6379"
@redis_db = "1"
@retry_count = 25
@retry_delay = 200
end
Expand Down
8 changes: 8 additions & 0 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
RSpec.describe Potlock::Configuration do
subject { described_class.new }

describe "redis" do
context "when no redis is specified" do
it "defaults to nil" do
expect(subject.redis).to be_nil
end
end
end

describe "redis_host" do
context "when no redis_host is specified" do
it "defaults to Redis hostname" do
Expand Down
4 changes: 4 additions & 0 deletions spec/potlock_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# frozen_string_literal: true

require "mock_redis"

RSpec.describe Potlock do
describe "#configure" do
it "allows Potlock configuration" do
Potlock.configure do |config|
config.redis = MockRedis.new
config.redis_host = "redis"
config.redis_port = "6381"
config.redis_db = "2"
config.retry_count = 10
config.retry_delay = 100
end
expect(Potlock.configuration.redis).to be_an_instance_of(MockRedis)
expect(Potlock.configuration.redis_host).to eq("redis")
expect(Potlock.configuration.redis_port).to eq("6381")
expect(Potlock.configuration.redis_db).to eq("2")
Expand Down

0 comments on commit 5729428

Please sign in to comment.