Skip to content
Open
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
25 changes: 13 additions & 12 deletions lib/redic/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ class Redic::Pool
def initialize(url, options = {})
@url = url
@pool = ConnectionPool.new(size: options.fetch(:size, 10)) { Redic.new(url) }
@buffer = Hash.new { |h, k| h[k] = [] }
end

def buffer
@buffer[Thread.current.object_id]
end

@id = "redic-pool-#{object_id}"
def reset
@buffer.delete(Thread.current.object_id)
end

def call(*args)
Expand All @@ -21,21 +28,15 @@ def call(*args)
end

def queue(*args)
Thread.current[@id] || (Thread.current[@id] = [])
Thread.current[@id] << args
buffer << args
end

def commit
@pool.with do |client|
Thread.current[@id].each do |args|
client.queue(*args)
end

result = client.commit

Thread.current[@id].clear

result
client.buffer.concat(buffer)
client.commit
end
ensure
reset
end
end