Class: NeverBlock::DB::FiberedMysqlConnection

Inherits:
Mysql
  • Object
show all
Includes:
FiberedDBConnection
Defined in:
lib/never_block/db/fibered_mysql_connection.rb

Overview

A modified mysql 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 Method Summary collapse

Methods included from FiberedDBConnection

#register_with_event_loop, #resume_command, #unregister_from_event_loop

Instance Method Details

#connectObject

reconnect and attach to the event loop



49
50
51
52
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 49

def connect
  super
  register_with_event_loop(@loop)    
end

#query(sql) ⇒ Object Also known as: exec

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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 22

def query(sql)
  begin
    if Fiber.respond_to? :current and Fiber.current[:neverblock]          
      send_query sql
      @fiber = Fiber.current          
      Fiber.yield
      get_result 
    else          
      super(sql)
    end
  rescue Exception => e
    if error = ['not connected', 'gone away', 'Lost connection'].detect{|msg| e.message.include? msg}
      stop
      connect
    end
    raise e
  end   
end

#stopObject

stop the connection and deattach from the event loop



44
45
46
# File 'lib/never_block/db/fibered_mysql_connection.rb', line 44

def stop
  unregister_from_event_loop
end