Class: NeverBlock::DB::FiberedMysqlConnection
- Inherits:
-
Mysql
- Object
- Mysql
- NeverBlock::DB::FiberedMysqlConnection
- Defined in:
- lib/never_block/db/fibered_mysql_connection.rb
Overview
A modified postgres connection driver builds on the original pg driver. This driver is able to register the socket at a certain backend (EM or Rev) and then whenever the query is executed within the scope of a friendly fiber it will be done in async mode and the fiber will yield
Instance Attribute Summary collapse
-
#fd ⇒ Object
readonly
needed to access the sockect by the event loop.
-
#io ⇒ Object
readonly
needed to access the sockect by the event loop.
Instance Method Summary collapse
-
#initialize(*args) ⇒ FiberedMysqlConnection
constructor
Creates a new postgresql connection, sets it to nonblocking and wraps the descriptor in an IO object.
-
#process_command ⇒ Object
The callback, this is called whenever there is data available at the socket.
-
#query(sql) ⇒ Object
Assuming the use of NeverBlock fiber extensions and that the exec is run in the context of a fiber.
-
#register_with_event_loop(loop) ⇒ Object
Attaches the connection socket to an event loop.
-
#unregister_from_event_loop ⇒ Object
Unattaches the connection socket from the event loop.
Constructor Details
#initialize(*args) ⇒ FiberedMysqlConnection
Creates a new postgresql connection, sets it to nonblocking and wraps the descriptor in an IO object.
21 22 23 24 25 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 21 def initialize(*args) #super(*args) @fd = socket @io = IO.new(socket) end |
Instance Attribute Details
#fd ⇒ Object (readonly)
needed to access the sockect by the event loop
16 17 18 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 16 def fd @fd end |
#io ⇒ Object (readonly)
needed to access the sockect by the event loop
16 17 18 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 16 def io @io end |
Instance Method Details
#process_command ⇒ Object
The callback, this is called whenever there is data available at the socket
75 76 77 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 75 def process_command @fiber.resume get_result end |
#query(sql) ⇒ Object
Assuming the use of NeverBlock fiber extensions and that the exec is run in the context of a fiber. One that have the value :neverblock set to true. All neverblock IO classes check this value, setting it to false will force the execution in a blocking way.
33 34 35 36 37 38 39 40 41 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 33 def query(sql) if Fiber.respond_to? :current and Fiber.current[:neverblock] send_query sql @fiber = Fiber.current Fiber.yield else super(sql) end end |
#register_with_event_loop(loop) ⇒ Object
Attaches the connection socket to an event loop. Currently only supports EM, but Rev support will be completed soon.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 46 def register_with_event_loop(loop) if loop == :em unless EM.respond_to?(:attach) puts "invalide EM version, please download the modified gem from: (http://github.com/riham/eventmachine)" exit end if EM.reactor_running? @em_connection = EM::attach(@io,EMConnectionHandler,self) else raise "REACTOR NOT RUNNING YA ZALAMA" end elsif loop.class.name == "REV::Loop" loop.attach(RevConnectionHandler.new(socket)) else raise "could not register with the event loop" end end |
#unregister_from_event_loop ⇒ Object
Unattaches the connection socket from the event loop
65 66 67 68 69 70 71 |
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 65 def unregister_from_event_loop if @loop == :em @em_connection.unattach(false) else raise NotImplementedError.new("unregister_from_event_loop not implemented for #{@loop}") end end |