Skip to content
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

Add support for redis client option #4

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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
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
2 changes: 2 additions & 0 deletions lib/potlock/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def retry_delay
end

def redis
return Potlock.configuration.redis unless Potlock.configuration.redis.nil?

@redis ||= Redis.new(
host: Potlock.configuration.redis_host,
db: Potlock.configuration.redis_db,
Expand Down
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
Loading