Skip to content

Commit

Permalink
handle edge case where all fiber db connections are busy and the main…
Browse files Browse the repository at this point in the history
… thread asks to run a db query; clean up FiberedConnectionPool#acquire
  • Loading branch information
tmm1 committed Jan 30, 2009
1 parent bf02db6 commit 8fb71fb
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/never_block/pool/fibered_connection_pool.rb
Expand Up @@ -74,20 +74,20 @@ def all_connections
# Can we create one?
# Wait in the queue then
def acquire(fiber)
# A special case for rails when doing ActiveRecord stuff when not yet
# running in the context of a request (fiber) like in the case of AR
# queries in environment.rb (Root Fiber)
return @connections.first unless fiber[:callbacks]
# Special case for when the database is accessed outside the fiber pool, for example
# ActiveRecord running queries in environment.rb (with the Root Fiber). It's possible all
# connnections might be busy when this happens, so we create an extra connection if needed.
return @connections.first || (@extra_connection ||= @connection_proc.call) unless fiber[:callbacks]

fiber[:current_pool_key] = connection_pool_key
return fiber[connection_pool_key] if fiber[connection_pool_key]
conn = if !@connections.empty?
@connections.shift
elsif (@connections.length + @busy_connections.length) < @size
@connection_proc.call
else
Fiber.yield @queue << fiber
end
@connections.shift
elsif (@connections.length + @busy_connections.length) < @size
@connection_proc.call
else
Fiber.yield @queue << fiber
end

# They're called in reverse order i.e. release then process_queue
fiber[:callbacks] << self.method(:process_queue)
Expand Down

0 comments on commit 8fb71fb

Please sign in to comment.