Skip to content
This repository has been archived by the owner on Jun 25, 2018. It is now read-only.

Commit

Permalink
Added support for accessing multiple databases at once
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Sykes authored and Stephen Sykes committed Dec 4, 2008
1 parent 9200f42 commit 0a09a7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
14 changes: 9 additions & 5 deletions lib/never_block/db/fibered_db_connection.rb
Expand Up @@ -11,8 +11,8 @@ def register_with_event_loop
@fiber = Fiber.current
#puts ">>>>>register_with_event_loop fiber #{@fiber.inspect}"
# When there's no previous em_connection
unless @fiber[:em_connection]
@fiber[:em_connection] = EM::attach(socket,EMConnectionHandler,self)
unless @fiber[em_connection_with_pool_key]
@fiber[em_connection_with_pool_key] = EM::attach(socket,EMConnectionHandler,self)
@fiber[:callbacks] << self.method(:unregister_from_event_loop)
end
else
Expand All @@ -23,9 +23,9 @@ def register_with_event_loop
# Unattaches the connection socket from the event loop
def unregister_from_event_loop
#puts ">>>>>unregister_from_event_loop #{self.inspect} #{@fiber.inspect}"
if em_c = @fiber[:em_connection]
if em_c = @fiber[em_connection_with_pool_key]
em_c.detach
@fiber[:em_connection] = nil
@fiber[em_connection_with_pool_key] = nil
true
else
false
Expand All @@ -41,7 +41,7 @@ def remove_unregister_from_event_loop_callbacks

# Closes the connection using event loop
def event_loop_connection_close
@fiber[:em_connection].close_connection if @fiber[:em_connection]
@fiber[em_connection_with_pool_key].close_connection if @fiber[em_connection_with_pool_key]
end

# The callback, this is called whenever
Expand All @@ -50,6 +50,10 @@ def resume_command
@fiber.resume if @fiber
end

private
def em_connection_with_pool_key
"em_#{@fiber[:current_pool_key]}".intern
end
end

module EMConnectionHandler
Expand Down
2 changes: 1 addition & 1 deletion lib/never_block/db/fibered_mysql_connection.rb
Expand Up @@ -32,7 +32,7 @@ def real_connect(*args)
# the execution in a blocking way.
def query(sql)
if NB.event_loop_available? && NB.neverblocking?
raise ::NB::NBError.new("FiberedMysqlConnection: The running fiber is attached to a connection other than the current one") if (c = Fiber.current[:connection]) && c != self
raise ::NB::NBError.new("FiberedMysqlConnection: The running fiber is attached to a connection other than the current one") if (c = Fiber.current[Fiber.current[:current_pool_key]]) && c != self
begin
send_query sql
Fiber.yield register_with_event_loop
Expand Down
17 changes: 11 additions & 6 deletions lib/never_block/pool/fibered_connection_pool.rb
Expand Up @@ -53,7 +53,7 @@ def replace_acquired_connection
fiber = Fiber.current
conn = @connection_proc.call
@busy_connections[fiber] = conn
fiber[:connection] = conn
fiber[connection_pool_key] = conn
end

# If a connection is available, pass it to the block, otherwise pass
Expand All @@ -79,7 +79,8 @@ def acquire(fiber)
# queries in environment.rb (Root Fiber)
return @connections.first unless fiber[:callbacks]

return fiber[:connection] if fiber[:connection]
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
Expand All @@ -93,16 +94,16 @@ def acquire(fiber)
fiber[:callbacks] << self.method(:release)

@busy_connections[fiber] = conn
fiber[:connection] = conn
fiber[connection_pool_key] = conn
end

# Give the fiber's connection back to the pool
def release()
fiber = Fiber.current
if fiber[:connection]
if fiber[connection_pool_key]
@busy_connections.delete(fiber)
@connections << fiber[:connection]
fiber[:connection] = nil
@connections << fiber[connection_pool_key]
fiber[connection_pool_key] = nil
end
end

Expand All @@ -118,6 +119,10 @@ def process_queue
end
end

def connection_pool_key
@connection_pool_key ||= "connection_pool_#{object_id}".intern
end

end #FiberedConnectionPool

end #Pool
Expand Down

0 comments on commit 0a09a7e

Please sign in to comment.