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

Fixed a bug that ConnectionPool holds the same Redis client. #79

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
10 changes: 8 additions & 2 deletions lib/redis/rack/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def pooled?
end

def pool
@pool ||= ConnectionPool.new(pool_options) { store } if pooled?
@pool ||= ConnectionPool.new(pool_options) { build_store } if pooled?
end

def store
@store ||= Redis::Store::Factory.create(@options[:redis_server])
@store ||= build_store
end

def pool_options
Expand All @@ -45,6 +45,12 @@ def pool_options
timeout: @options[:pool_timeout]
}.reject { |key, value| value.nil? }.to_h
end

private

def build_store
Redis::Store::Factory.create(@options[:redis_server])
end
end
end
end
26 changes: 26 additions & 0 deletions test/redis/rack/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ def setup
end
end

it "can establish a new connection pool with the provided Redis server" do
pool_size = 5
conn = Connection.new(redis_server: 'redis://127.0.0.1:6380/1', pool_size: pool_size)

clients = []
mutex = Mutex.new
threads = Array.new(pool_size) do |i|
Thread.new do
conn.with do |connection|
mutex.synchronize do
clients[i] = connection
end

# Wait for all clients to be established
loop do
break if clients.size == pool_size && clients.all?
sleep(0.01)
end
end
end
end.each(&:join)

conn.pooled?.must_equal true
clients.map(&:object_id).uniq.size.must_equal pool_size
end

it "can use a supplied pool" do
pool = ConnectionPool.new size: 1, timeout: 1 do
::Redis::Store::Factory.create('redis://127.0.0.1:6380/1')
Expand Down